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

core: Remove load-conf-on-import, implement basic command line options via argparse

Ref #242.
This commit is contained in:
James Lu 2016-06-21 10:55:42 -07:00
parent 42ec6f2502
commit 2f188dc60d
2 changed files with 18 additions and 56 deletions

52
conf.py
View File

@ -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))

22
pylink
View File

@ -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.