Samuel Sloniker
4d5ce06eb6
* Add support for config file Closes #1 * Support changing config path The path to the config file is accepted as an argument * Remove old code Remove the code for the old backend selection method
32 lines
633 B
Python
32 lines
633 B
Python
import shlex
|
|
|
|
|
|
class ConfigSyntaxError(BaseException):
|
|
pass
|
|
|
|
|
|
def loadl(lines):
|
|
output = {}
|
|
|
|
for line in [ i.strip() for i in lines ]:
|
|
words = shlex.split(line, comments=True)
|
|
|
|
if len(words) == 0:
|
|
pass
|
|
elif len(words) == 2:
|
|
if words[0] in output:
|
|
raise ConfigSyntaxError('keys cannot be redefined')
|
|
output[words[0]] = words[1]
|
|
else:
|
|
raise ConfigSyntaxError('lines must consist of exactly two tokens')
|
|
|
|
return output
|
|
|
|
|
|
def loads(string):
|
|
return loadl(string.split('\n'))
|
|
|
|
|
|
def load(f):
|
|
return loadl(f.readlines())
|