Skip to content

Commit

Permalink
commander, plugins/quote: Isolate database configuration.
Browse files Browse the repository at this point in the history
Not sure if this works for quotes, since that plugin appears to not register
commands correctly. What is up with that?

Refs hamperbot#7.
  • Loading branch information
MostAwesomeDude committed May 26, 2013
1 parent 1eab708 commit 689d19e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
22 changes: 15 additions & 7 deletions hamper/commander.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from collections import deque
from collections import deque, namedtuple
import logging
import re
import traceback
Expand Down Expand Up @@ -40,7 +40,7 @@ def nickname(self):

@property
def db(self):
return self.factory.db
return self.factory.loader.db

##### Twisted events #####

Expand Down Expand Up @@ -114,7 +114,7 @@ def privmsg(self, raw_user, channel, raw_message):

def connectionLost(self, reason):
"""Called when the connection is lost to the server."""
self.factory.db.commit()
self.factory.loader.db.session.commit()
if reactor.running:
reactor.stop()

Expand Down Expand Up @@ -192,12 +192,14 @@ def __init__(self, config):

if 'db' in config:
print('Loading db from config: ' + config['db'])
self.db_engine = sqlalchemy.create_engine(config['db'])
db_engine = sqlalchemy.create_engine(config['db'])
else:
print('Using in-memory db')
self.db_engine = sqlalchemy.create_engine('sqlite:///:memory:')
DBSession = orm.sessionmaker(self.db_engine)
self.db = DBSession()
db_engine = sqlalchemy.create_engine('sqlite:///:memory:')
DBSession = orm.sessionmaker(db_engine)
session = DBSession()

self.loader.db = DB(db_engine, session)

# Load all plugins mentioned in the configuration. Allow globbing.
plugins = retrieve_named_plugins(IPlugin, config['plugins'], 'hamper.plugins')
Expand All @@ -220,6 +222,12 @@ def registerPlugin(self, plugin):
self.loader.registerPlugin(plugin)


class DB(namedtuple("DB", "engine, session")):
"""
A small data structure that stores database information.
"""


class PluginLoader(object):
"""
I am a repository for plugins.
Expand Down
10 changes: 5 additions & 5 deletions hamper/plugins/quote.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ class Quotes(ChatCommandPlugin):
name = 'quotes'
priority = 0

def setup(self, factory):
SQLAlchemyBase.metadata.create_all(factory.db_engine)
def setup(self, loader):
SQLAlchemyBase.metadata.create_all(loader.db.engine)

class DeliverQuote(Command):
"""Deliver a quote."""
Expand All @@ -31,7 +31,7 @@ class DeliverQuote(Command):

def command(self, bot, comm, groups):
index = random.randrange(0, bot.db.query(Quote).count() + 1)
quote = bot.factory.db.query(Quote)[index]
quote = bot.factory.loader.db.session.query(Quote)[index]
# Lame twisted irc doesn't support unicode.
bot.reply(comm, str(quote.text))
return True
Expand All @@ -43,15 +43,15 @@ class AddQuote(Command):
def command(self, bot, comm, groups):
text = groups[0]
quote = Quote(text, comm['user'])
bot.factory.db.add(quote)
bot.factory.loader.db.session.add(quote)
bot.reply(comm, 'Succesfully added quote.')

class CountQuotes(Command):
"""Count how many quotes the bot knows."""
regex = r'^quotes? --count$'

def command(self, bot, comm, groups):
count = bot.db.query(Quote).count()
count = bot.factory.loader.db.session.query(Quote).count()
bot.reply(comm, 'I know {0} quotes.'.format(count))


Expand Down

0 comments on commit 689d19e

Please sign in to comment.