2015-12-06 17:40:13 -08:00
|
|
|
"""
|
|
|
|
conf.py - PyLink configuration core.
|
|
|
|
|
2016-06-21 10:56:53 -07:00
|
|
|
This module is used to access the configuration of the current PyLink instance.
|
|
|
|
It provides simple checks for validating and loading YAML-format configurations from arbitrary files.
|
2015-12-06 17:40:13 -08:00
|
|
|
"""
|
|
|
|
|
2015-05-31 12:20:09 -07:00
|
|
|
import yaml
|
2015-08-03 19:27:19 -07:00
|
|
|
import sys
|
2015-08-28 19:27:38 -07:00
|
|
|
from collections import defaultdict
|
|
|
|
|
2016-06-20 18:18:54 -07:00
|
|
|
from . import world
|
2015-05-31 12:20:09 -07:00
|
|
|
|
2015-09-28 11:30:51 -07:00
|
|
|
def validateConf(conf):
|
|
|
|
"""Validates a parsed configuration dict."""
|
|
|
|
assert type(conf) == dict, "Invalid configuration given: should be type dict, not %s." % type(conf).__name__
|
2016-02-07 18:01:12 -08:00
|
|
|
|
|
|
|
for section in ('bot', 'servers', 'login', 'logging'):
|
2015-09-28 11:30:51 -07:00
|
|
|
assert conf.get(section), "Missing %r section in config." % section
|
2016-02-07 18:01:12 -08:00
|
|
|
|
2015-09-28 11:30:51 -07:00
|
|
|
for netname, serverblock in conf['servers'].items():
|
|
|
|
for section in ('ip', 'port', 'recvpass', 'sendpass', 'hostname',
|
2015-09-28 19:22:19 -07:00
|
|
|
'sid', 'sidrange', 'protocol', 'maxnicklen'):
|
2015-09-28 11:30:51 -07:00
|
|
|
assert serverblock.get(section), "Missing %r in server block for %r." % (section, netname)
|
2016-02-07 18:01:12 -08:00
|
|
|
|
2015-09-28 19:22:19 -07:00
|
|
|
assert type(serverblock.get('channels')) == list, "'channels' option in " \
|
2015-09-28 11:30:51 -07:00
|
|
|
"server block for %s must be a list, not %s." % (netname, type(serverblock['channels']).__name__)
|
2016-02-07 18:01:12 -08:00
|
|
|
|
2015-09-28 11:30:51 -07:00
|
|
|
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!"
|
2016-02-07 18:01:12 -08:00
|
|
|
|
2015-09-28 11:30:51 -07:00
|
|
|
return conf
|
|
|
|
|
2016-06-21 11:31:39 -07:00
|
|
|
def loadConf(filename, errors_fatal=True):
|
2015-09-28 11:30:51 -07:00
|
|
|
"""Loads a PyLink configuration file from the filename given."""
|
2016-06-21 11:31:39 -07:00
|
|
|
global confname, conf, fname
|
|
|
|
fname = filename
|
|
|
|
confname = filename.split('.', 1)[0]
|
|
|
|
with open(filename, 'r') as f:
|
2015-09-28 11:30:51 -07:00
|
|
|
try:
|
|
|
|
conf = yaml.load(f)
|
2016-06-21 10:55:42 -07:00
|
|
|
conf = validateConf(conf)
|
2015-09-28 11:30:51 -07:00
|
|
|
except Exception as e:
|
2016-06-21 11:31:39 -07:00
|
|
|
print('ERROR: Failed to load config from %r: %s: %s' % (filename, type(e).__name__, e))
|
2015-11-11 18:55:51 -08:00
|
|
|
if errors_fatal:
|
|
|
|
sys.exit(4)
|
|
|
|
raise
|
|
|
|
else:
|
|
|
|
return conf
|