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:
parent
42ec6f2502
commit
2f188dc60d
52
conf.py
52
conf.py
@ -19,37 +19,6 @@ from collections import defaultdict
|
|||||||
|
|
||||||
from . import world
|
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):
|
def validateConf(conf):
|
||||||
"""Validates a parsed configuration dict."""
|
"""Validates a parsed configuration dict."""
|
||||||
assert type(conf) == dict, "Invalid configuration given: should be type dict, not %s." % type(conf).__name__
|
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):
|
def loadConf(fname, errors_fatal=True):
|
||||||
"""Loads a PyLink configuration file from the filename given."""
|
"""Loads a PyLink configuration file from the filename given."""
|
||||||
|
global confname, conf
|
||||||
|
confname = fname.split('.', 1)[0]
|
||||||
with open(fname, 'r') as f:
|
with open(fname, 'r') as f:
|
||||||
try:
|
try:
|
||||||
conf = yaml.load(f)
|
conf = yaml.load(f)
|
||||||
|
conf = validateConf(conf)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print('ERROR: Failed to load config from %r: %s: %s' % (fname, type(e).__name__, e))
|
print('ERROR: Failed to load config from %r: %s: %s' % (fname, type(e).__name__, e))
|
||||||
if errors_fatal:
|
if errors_fatal:
|
||||||
@ -82,21 +54,3 @@ def loadConf(fname, errors_fatal=True):
|
|||||||
raise
|
raise
|
||||||
else:
|
else:
|
||||||
return conf
|
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
22
pylink
@ -2,20 +2,28 @@
|
|||||||
|
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
# This must be done before conf imports, so we get the real conf instead of testing one.
|
|
||||||
try:
|
try:
|
||||||
from pylinkirc import world
|
from pylinkirc import conf
|
||||||
except ImportError:
|
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 "
|
"running PyLink from source, please install PyLink first using 'python3 "
|
||||||
"setup.py install [--user]'\n")
|
"setup.py install [--user]'\n")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
world.testing = False
|
|
||||||
|
|
||||||
from pylinkirc import conf, classes, utils, coreplugin
|
|
||||||
from pylinkirc.log import log
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
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)
|
log.info('PyLink %s starting...', world.version)
|
||||||
|
|
||||||
# Write a PID file.
|
# Write a PID file.
|
||||||
|
Loading…
Reference in New Issue
Block a user