Commits

Jason R. Coombs committed 913ed5c

Moved all quotes functionality (including the command) into the quotes module

Comments (0)

Files changed (6)

  - The Karma store must now be referenced as `pmxbot.karma:Karma.store` (a
    class attribute). It is no longer available as `pmxbot.pmxbot:karma` nor
    `pmxbot.util:karma` nor `pmxbot.karma.karma`.
+ - Similarly, the Quotes store must now be referenced as
+   `pmxbot.quotes:Quotes.store` (a class attribute).

pmxbot/botbase.py

 import irc.bot
 
 import pmxbot.itertools
-from . import quotes
 from .logging import init_logger
 
 class WarnHistory(dict):
 			db_uri = os.path.join(db_uri, "pmxbot.sqlite")
 		self.db_uri = db_uri
 		globals().update(logger=init_logger(db_uri))
-		quotes.init_quotes(db_uri)
 		self._nickname = nickname
 		self.__use_ssl = use_ssl
 		self.warn_history = WarnHistory()
 		text = '%s %s' % (who, action)
 	return text
 
-#Added quotes
-@command('quote', aliases=('q',), doc='If passed with nothing then get a random quote. If passed with some string then search for that. If prepended with "add:" then add it to the db, eg "!quote add: drivers: I only work here because of pmxbot!"')
-def quote(client, event, channel, nick, rest):
-	rest = rest.strip()
-	if rest.startswith('add: ') or rest.startswith('add '):
-		quoteToAdd = rest.split(' ', 1)[1]
-		quotes.quotes.quoteAdd(quoteToAdd)
-		qt = False
-		return 'Quote added!'
-	qt, i, n = quotes.quotes.quoteLookupWNum(rest)
-	if not qt: return
-	return '(%s/%s): %s' % (i, n, qt)
-
 @command('zinger', aliases=('zing',), doc='ZING!')
 def zinger(client, event, channel, nick, rest):
 	name = 'you'
 		golfclap, nastygram, curse, bless, job, hire, oregontrail,
 		chain, tinytear, blame, panic, rubberstamp, dance, annoy, klingon,
 		storytime, murphy]
-	quote_functions = [quote, pq.zoidberg, pq.simpsons, pq.bender,
+	quote_functions = [quotes.quote, pq.zoidberg, pq.simpsons, pq.bender,
 		pq.hal, pq.grail, pq.R, pq.anchorman, pq.hangover]
 	func = random.choice(normal_functions + quote_functions)
 	nick = nick if func in normal_functions else ''
 	word_factory = functools.partial(
 		saysomethinglib.words_from_logger_and_quotes,
 		botbase.logger,
-		quotes.quotes,
+		quotes.Quotes.store,
 	)
 	sayer = saysomethinglib.FastSayer(word_factory)
 	if rest:
 
 _finalizers = [
 	botbase.LoggingCommandBot._finalize_logger,
-	quotes.Quotes.finalize,
 ]
 
 def _cleanup():
 import importlib
 
 from . import storage
-
-def init_quotes(uri):
-	Quotes.store = Quotes.from_URI(uri)
-	# for backward compatibility
-	globals().update(quotes = Quotes.store)
-	importlib.import_module('pmxbot.util').quotes = Quotes.store
+from .botbase import command
 
 class Quotes(storage.SelectableStorage):
 	lib = 'pmx'
 
 	@classmethod
+	def initialize(cls):
+		pmxbot = importlib.import_module('pmxbot.pmxbot')
+		cls.store = cls.from_URI(pmxbot.config.database)
+		pmxbot._finalizers.append(cls.finalize)
+
+	@classmethod
 	def finalize(cls):
 		del cls.store
-		del globals()['quotes']
-		del importlib.import_module('pmxbot.util').quotes
 
 class SQLiteQuotes(Quotes, storage.SQLiteStorage):
 	def init_tables(self):
 		if log_id is not None:
 			quote['log_id'] = log_id
 		self.db.insert(quote)
+
+@command('quote', aliases=('q',), doc='If passed with nothing then get a '
+	'random quote. If passed with some string then search for that. If '
+	'prepended with "add:" then add it to the db, eg "!quote add: drivers: I '
+	'only work here because of pmxbot!"')
+def quote(client, event, channel, nick, rest):
+	rest = rest.strip()
+	if rest.startswith('add: ') or rest.startswith('add '):
+		quoteToAdd = rest.split(' ', 1)[1]
+		Quotes.store.quoteAdd(quoteToAdd)
+		qt = False
+		return 'Quote added!'
+	qt, i, n = Quotes.store.quoteLookupWNum(rest)
+	if not qt: return
+	return '(%s/%s): %s' % (i, n, qt)
 		],
 		pmxbot_handlers = [
 			'pmxbot karma = pmxbot.karma:Karma.initialize',
+			'pmxbot quotes = pmxbot.quotes:Quotes.initialize',
 			'pmxbot notifier = pmxbot.notify:Notify.init',
 			'pmxbot feedparser = pmxbot.rss:RSSFeeds',
 		],

tests/unit/test_commands.py

 
 from pmxbot import pmxbot
 from pmxbot import karma
+from pmxbot import quotes
 
 def pytest_generate_tests(metafunc):
 	# any test that takes the iter_ parameter should be executed 100 times
 		Try adding a quote
 		"""
 		quote = "And then she said %s" % str(uuid.uuid4())
-		res = pmxbot.quote(c, e, "#test", "testrunner", "add %s" % quote)
+		res = quotes.quote(c, e, "#test", "testrunner", "add %s" % quote)
 		assert res == "Quote added!"
 		cursor = pmxbot.botbase.logger.db.cursor()
-		cursor.execute("select count(*) from quotes where library = 'pmx' and quote = ?", (quote,))
+		cursor.execute("select count(*) from quotes where library = 'pmx' "
+			"and quote = ?", (quote,))
 		numquotes = cursor.fetchone()[0]
 		assert numquotes == 1
 
 		"""
 		id = str(uuid.uuid4())
 		quote = "So I says to Mabel, I says, %s" % id
-		res = pmxbot.quote(c, e, "#test", "testrunner", "add %s" % quote)
+		res = quotes.quote(c, e, "#test", "testrunner", "add %s" % quote)
 		assert res == "Quote added!"
 		cursor = pmxbot.botbase.logger.db.cursor()
-		cursor.execute("select count(*) from quotes where library = 'pmx' and quote = ?", (quote,))
+		cursor.execute("select count(*) from quotes where library = 'pmx' "
+			"and quote = ?", (quote,))
 		numquotes = cursor.fetchone()[0]
 		assert numquotes == 1
 
-		res = pmxbot.quote(c, e, "#test", "testrunner", id)
+		res = quotes.quote(c, e, "#test", "testrunner", id)
 		assert res == "(1/1): %s" % quote
 
 	def test_roll(self):