Jason R. Coombs avatar Jason R. Coombs committed 0bdd971

Moved logging initialization/finalization/instance into logging module

Comments (0)

Files changed (7)

    `pmxbot.util:karma` nor `pmxbot.karma.karma`.
  - Similarly, the Quotes store must now be referenced as
    `pmxbot.quotes:Quotes.store` (a class attribute).
+ - Similarly, the Logger store must now be referenced as
+   `pmxbot.logging:Logger.store` instead of `pmxbot.botbase.logger`.
 
 Other backward-incompatible changes::
 

pmxbot/commands.py

 import pmxbot
 from .core import command, contains, _handler_registry, NoLog
 from . import core
+from . import logging
 from . import util
 from . import karma
 from . import quotes
 def saysomething(client, event, channel, nick, rest):
 	word_factory = functools.partial(
 		saysomethinglib.words_from_logger_and_quotes,
-		core.logger,
+		logging.Logger.store,
 		quotes.Quotes.store,
 	)
 	sayer = saysomethinglib.FastSayer(word_factory)
 			raise StopIteration
 		count = int(rest)
 	try:
-		struck = core.logger.strike(channel, nick, count)
+		struck = logging.Logger.store.strike(channel, nick, count)
 		yield ("Isn't undo great?  Last %d statement%s by %s were stricken from the record." %
 		(struck, 's' if struck > 1 else '', nick))
 	except Exception:
 @command("where", aliases=('last', 'seen', 'lastseen'), doc="When did pmxbot last see <nick> speak?")
 def where(client, event, channel, nick, rest):
 	onick = rest.strip()
-	last = core.logger.last_seen(onick)
+	last = logging.Logger.store.last_seen(onick)
 	if last:
 		tm, chan = last
 		return "I last saw %s speak at %s in channel #%s" % (
 
 import sys
 import datetime
-import os
 import traceback
 import time
 import random
 	def _expired(self, last, now):
 		return now - last > self.warn_every
 
-logger = None
-
 class NoLog(object):
 	@classmethod
 	def secret_items(cls, items):
 		self._channels = channels + nolog_channels
 		self._nolog = set(('#' + c if not c.startswith('#') else c) for c in nolog_channels)
 		self.db_uri = db_uri
-		globals().update(logger=logging.Logger.from_URI(db_uri))
 		self._nickname = nickname
 		self.__use_ssl = use_ssl
 		self.warn_history = WarnHistory()
 
-	@staticmethod
-	def _finalize_logger():
-		globals().update(logger=None)
-
 	def connect(self, *args, **kwargs):
 		kwargs['ssl'] = self.__use_ssl
 		return irc.bot.SingleServerIRCBot.connect(self, *args, **kwargs)
 			log = False
 		func(channel, s)
 		if channel in self._channels and channel not in self._nolog and log:
-			logger.message(channel, self._nickname, s)
+			logging.Logger.core.message(channel, self._nickname, s)
 
 	def _schedule_at(self, name, channel, when, func, args, doc):
 		arguments = self.c, channel, func, args
 		channel = e.target()
 		if msg.strip() > '':
 			if channel not in self._nolog:
-				logger.message(channel, nick, msg)
+				logging.Logger.store.message(channel, nick, msg)
 			self.handle_action(c, e, channel, nick, msg)
 
 	def on_privmsg(self, c, e):

pmxbot/logging.py

 class Logger(storage.SelectableStorage):
 	"Base Logger class"
 
+	@classmethod
+	def initialize(cls):
+		import pmxbot.pmxbot
+		cls.store = cls.from_URI(pmxbot.config.database)
+		pmxbot.pmxbot._finalizers.append(cls.finalize)
+
+	@classmethod
+	def finalize(cls):
+		del cls.store
+
 	def message(self, channel, nick, msg):
 		channel = channel.replace('#', '').lower()
 		self._message(channel, nick, msg)
 		use_ssl=pmxbot.config.use_ssl, password=pmxbot.config.password)
 
 
-_finalizers = [
-	core.LoggingCommandBot._finalize_logger,
-]
+_finalizers = []
 
 def _cleanup():
 	"Delete the various persistence objects"
 			'pmxbotweb=pmxbotweb.pmxbotweb:run',
 		],
 		pmxbot_handlers = [
+			'pmxbot logging = pmxbot.logging:Logger.initialize',
 			'pmxbot karma = pmxbot.karma:Karma.initialize',
 			'pmxbot quotes = pmxbot.quotes:Quotes.initialize',
 			'pmxbot core commands = pmxbot.commands',

tests/unit/test_commands.py

 import popquotes.pmxbot
 
 from pmxbot import core
+from pmxbot import logging
 from pmxbot import pmxbot
 from pmxbot import commands
 from pmxbot import karma
 		configfile = os.path.join(path, 'testconf.yaml')
 		config = pmxbot.dictlib.ConfigDict.from_yaml(configfile)
 		cls.bot = pmxbot.initialize(config)
-		core.logger.message("logged", "testrunner", "some text")
+		logging.Logger.store.message("logged", "testrunner", "some text")
 
 	@classmethod
 	def teardown_class(cls):
 		quote = "And then she said %s" % str(uuid.uuid4())
 		res = quotes.quote(c, e, "#test", "testrunner", "add %s" % quote)
 		assert res == "Quote added!"
-		cursor = core.logger.db.cursor()
+		cursor = logging.Logger.store.db.cursor()
 		cursor.execute("select count(*) from quotes where library = 'pmx' "
 			"and quote = ?", (quote,))
 		numquotes = cursor.fetchone()[0]
 		quote = "So I says to Mabel, I says, %s" % id
 		res = quotes.quote(c, e, "#test", "testrunner", "add %s" % quote)
 		assert res == "Quote added!"
-		cursor = core.logger.db.cursor()
+		cursor = logging.Logger.store.db.cursor()
 		cursor.execute("select count(*) from quotes where library = 'pmx' "
 			"and quote = ?", (quote,))
 		numquotes = cursor.fetchone()[0]
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.