Spaces:
Sleeping
Sleeping
File size: 1,901 Bytes
2f2406a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
import csv
# Interprets a string as a boolean. Returns True or False
def parseBool(string, silent=False):
if type(string) == str:
if string.lower() == 'true':
return True
elif string.lower() == 'false':
return False
else:
if not silent:
raise ValueError(f'Invalid value "{string}". Must be "True" or "False"')
elif silent:
return string
elif type(string) == bool:
if string == True:
return True
elif string == False:
return False
else:
raise ValueError('Not a valid boolean string')
def parseConfigSetting(setting):
# Remove any quotes user may have added in config file
setting = setting.strip("\"").strip("\'")
# Check if it is a boolean
if type(parseBool(setting, silent=True)) == bool:
return parseBool(setting, silent=True)
# Check if it is an integer
try:
return int(setting)
except ValueError:
pass
# Otherwise return the string in lower case
return setting.lower()
# Returns a list of dictionaries from a csv file. Where the key is the column name and the value is the value in that column
# The column names are set by the first row of the csv file
def csv_to_dict(csvFilePath):
with open(csvFilePath, "r", encoding='utf-8-sig') as data:
entriesDictsList = []
for line in csv.DictReader(data):
entriesDictsList.append(line)
return entriesDictsList
# Returns a list of strings from a txt file. Ignores empty lines and lines that start with '#'
def txt_to_list(txtFilePath):
with open(txtFilePath, "r", encoding='utf-8-sig') as data:
entriesList = []
for line in data:
if line.strip() != '' and line.strip()[0] != '#':
entriesList.append(line.strip())
return entriesList
|