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

Support loading different config files from the command line

Closes #84. Logs, PID files, and relay DBs will name themselves differently for every instance, to prevent conflicts. The default is always config.yml.
This commit is contained in:
James Lu 2015-08-03 19:27:19 -07:00
parent e5eb58ee73
commit f786242730
5 changed files with 25 additions and 7 deletions

4
.gitignore vendored
View File

@ -1,4 +1,4 @@
config.yml
*.yml
build/
__pycache__/
*.py[cod]
@ -6,4 +6,4 @@ __pycache__/
*~
*.save*
*.db
pylink.pid
*.pid

16
conf.py
View File

@ -1,5 +1,19 @@
import yaml
import sys
with open("config.yml", 'r') as f:
global confname
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'
with open(fname, 'r') as f:
global conf
conf = yaml.load(f)

4
log.py
View File

@ -2,7 +2,7 @@ import logging
import sys
import os
from conf import conf
from conf import conf, confname
level = conf['bot'].get('loglevel') or 'DEBUG'
try:
@ -21,7 +21,7 @@ logging.basicConfig(level=level, format=_format)
# Set log file to $CURDIR/log/pylink
logformat = logging.Formatter(_format)
logfile = logging.FileHandler(os.path.join(logdir, 'pylink.log'), mode='w')
logfile = logging.FileHandler(os.path.join(logdir, '%s.log' % confname), mode='w')
logfile.setFormatter(logformat)
global log

View File

@ -205,7 +205,7 @@ if __name__ == '__main__':
protocols_folder = [os.path.join(os.getcwd(), 'protocols')]
# Write a PID file.
with open('pylink.pid', 'w') as f:
with open('%s.pid' % conf.confname, 'w') as f:
f.write(str(os.getpid()))
# Import plugins first globally, because they can listen for events

View File

@ -10,8 +10,12 @@ from collections import defaultdict
import utils
from log import log
from conf import confname
dbname = "pylinkrelay.db"
dbname = "pylinkrelay"
if confname != 'pylink':
dbname += '-%s' % confname
dbname += '.db'
relayusers = defaultdict(dict)
def relayWhoisHandlers(irc, target):