From 2f188dc60d24d6a2df2d1c87ca017664913b30b1 Mon Sep 17 00:00:00 2001 From: James Lu Date: Tue, 21 Jun 2016 10:55:42 -0700 Subject: [PATCH] core: Remove load-conf-on-import, implement basic command line options via argparse Ref #242. --- conf.py | 52 +++------------------------------------------------- pylink | 22 +++++++++++++++------- 2 files changed, 18 insertions(+), 56 deletions(-) diff --git a/conf.py b/conf.py index 4487123..920ac1c 100644 --- a/conf.py +++ b/conf.py @@ -19,37 +19,6 @@ from collections import defaultdict from . import world -global testconf -testconf = {'bot': - { - 'nick': 'PyLink', - 'user': 'pylink', - 'realname': 'PyLink Service Client', - 'serverdesc': 'PyLink unit tests' - }, - 'logging': - { - # Suppress logging in the test output for the most part. - 'stdout': 'CRITICAL' - }, - 'servers': - # Wildcard defaultdict! This means that - # any network name you try will work and return - # this basic template: - defaultdict(lambda: { - 'ip': '0.0.0.0', - 'port': 7000, - 'recvpass': "abcd", - 'sendpass': "chucknorris", - 'protocol': "null", - 'hostname': "pylink.unittest", - 'sid': "9PY", - 'channels': ["#pylink"], - 'maxnicklen': 20, - '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__ @@ -72,9 +41,12 @@ def validateConf(conf): def loadConf(fname, errors_fatal=True): """Loads a PyLink configuration file from the filename given.""" + global confname, conf + confname = fname.split('.', 1)[0] with open(fname, 'r') as f: try: conf = yaml.load(f) + conf = validateConf(conf) except Exception as e: print('ERROR: Failed to load config from %r: %s: %s' % (fname, type(e).__name__, e)) if errors_fatal: @@ -82,21 +54,3 @@ def loadConf(fname, errors_fatal=True): raise else: return conf - -if world.testing: - conf = testconf - confname = 'testconf' - fname = None -else: - try: - # Get the config name from the command line, falling back to config.yml - # if not given. - fname = sys.argv[1] - confname = fname.split('.', 1)[0] - except IndexError: - # confname is used for logging and PID writing, so that each - # instance uses its own files. fname is the actual name of the file - # we load. - confname = 'pylink' - fname = 'config.yml' - conf = validateConf(loadConf(fname)) diff --git a/pylink b/pylink index b34fa88..40e2747 100755 --- a/pylink +++ b/pylink @@ -2,20 +2,28 @@ import os import sys -# This must be done before conf imports, so we get the real conf instead of testing one. try: - from pylinkirc import world + from pylinkirc import conf except ImportError: - sys.stderr.write("ERROR: Failed to import PyLink main module (pylinkirc.world).\n\nIf you are " + sys.stderr.write("ERROR: Failed to import PyLink main module (pylinkirc.conf).\n\nIf you are " "running PyLink from source, please install PyLink first using 'python3 " "setup.py install [--user]'\n") sys.exit(1) -world.testing = False - -from pylinkirc import conf, classes, utils, coreplugin -from pylinkirc.log import log if __name__ == '__main__': + import argparse + + parser = argparse.ArgumentParser(description='Starts an instance of PyLink IRC Services.') + parser.add_argument('config', help='specifies the path to the config file (defaults to pylink.yml)', nargs='?', default='pylink.yml') + parser.add_argument("-v", "--version", help="displays the program version and exits", action='store_true') + parser.add_argument("-n", "--no-pid", help="skips generating PID files", action='store_true') + args = parser.parse_args() + + # Load the config + conf.loadConf(args.config) + + from pylinkirc.log import log + from pylinkirc import classes, utils, coreplugin, world log.info('PyLink %s starting...', world.version) # Write a PID file.