From 561af2e037d4d73535d62c0a5b4a31617fa75b50 Mon Sep 17 00:00:00 2001 From: James Lu Date: Sun, 6 Dec 2015 17:13:47 -0800 Subject: [PATCH] utils/relay: add and use getDatabaseName to fetch an instance-specific DB name --- plugins/relay.py | 9 +++------ utils.py | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/plugins/relay.py b/plugins/relay.py index 1439cb5..27bbc80 100644 --- a/plugins/relay.py +++ b/plugins/relay.py @@ -12,14 +12,9 @@ from expiringdict import ExpiringDict import utils from log import log -from conf import confname import world -dbname = "pylinkrelay" -if confname != 'pylink': - dbname += '-%s' % confname -dbname += '.db' - +### GLOBAL (statekeeping) VARIABLES relayusers = defaultdict(dict) relayservers = defaultdict(dict) spawnlocks = defaultdict(threading.RLock) @@ -28,6 +23,8 @@ savecache = ExpiringDict(max_len=5, max_age_seconds=10) killcache = ExpiringDict(max_len=5, max_age_seconds=10) relay_started = True +dbname = utils.getDatabaseName('pylinkrelay') + ### INTERNAL FUNCTIONS def initializeAll(irc): diff --git a/utils.py b/utils.py index e2476e3..7df6511 100644 --- a/utils.py +++ b/utils.py @@ -6,6 +6,7 @@ import os from log import log import world +import conf class NotAuthenticatedError(Exception): pass @@ -514,3 +515,17 @@ def loadModuleFromFolder(name, folder): def getProtoModule(protoname): """Imports and returns the protocol module requested.""" return loadModuleFromFolder(protoname, world.protocols_folder) + +def getDatabaseName(dbname): + """ + Returns a database filename with the given base DB name appropriate for the + current PyLink instance. + + This returns '.db' if the running config name is PyLink's default + (config.yml), and '-.db' for anything else. For example, + if this is called from an instance running as './pylink testing.yml', it + would return '-testing.db'.""" + if conf.confname != 'pylink': + dbname += '-%s' % conf.confname + dbname += '.db' + return dbname