3
0
mirror of https://github.com/jlu5/PyLink.git synced 2024-11-23 19:19:31 +01:00

core: add some rudimentary config file validation

This commit is contained in:
James Lu 2015-09-28 11:30:51 -07:00
parent 0d3a7a5ce0
commit 630aa83084
2 changed files with 27 additions and 9 deletions

33
conf.py
View File

@ -31,6 +31,32 @@ testconf = {'bot':
'sidrange': '8##' 'sidrange': '8##'
}) })
} }
def validateConf(conf):
"""Validates a parsed configuration dict."""
assert type(conf) == dict, "Invalid configuration given: should be type dict, not %s." % type(conf).__name__
for section in ('bot', 'servers', 'login'):
assert conf.get(section), "Missing %r section in config." % section
for netname, serverblock in conf['servers'].items():
for section in ('ip', 'port', 'recvpass', 'sendpass', 'hostname',
'sid', 'sidrange', 'channels', 'protocol', 'maxnicklen'):
assert serverblock.get(section), "Missing %r in server block for %r." % (section, netname)
assert type(serverblock['channels']) == list, "'channels' option in " \
"server block for %s must be a list, not %s." % (netname, type(serverblock['channels']).__name__)
assert type(conf['login'].get('password')) == type(conf['login'].get('user')) == str and \
conf['login']['password'] != "changeme", "You have not set the login details correctly!"
return conf
def loadConf(fname):
"""Loads a PyLink configuration file from the filename given."""
with open(fname, 'r') as f:
try:
conf = yaml.load(f)
except Exception as e:
print('ERROR: Failed to load config from %r: %s: %s' % (fname, type(e).__name__, e))
sys.exit(4)
return conf
if world.testing: if world.testing:
conf = testconf conf = testconf
confname = 'testconf' confname = 'testconf'
@ -46,9 +72,4 @@ else:
# we load. # we load.
confname = 'pylink' confname = 'pylink'
fname = 'config.yml' fname = 'config.yml'
with open(fname, 'r') as f: conf = validateConf(loadConf(fname))
try:
conf = yaml.load(f)
except Exception as e:
print('ERROR: Failed to load config from %r: %s: %s' % (fname, type(e).__name__, e))
sys.exit(4)

3
pylink
View File

@ -16,9 +16,6 @@ import coreplugin
if __name__ == '__main__': if __name__ == '__main__':
log.info('PyLink %s starting...', world.version) log.info('PyLink %s starting...', world.version)
if conf.conf['login']['password'] == 'changeme':
log.critical("You have not set the login details correctly! Exiting...")
sys.exit(2)
protocols_folder = [os.path.join(os.getcwd(), 'protocols')] protocols_folder = [os.path.join(os.getcwd(), 'protocols')]
# Write a PID file. # Write a PID file.