Skip to content

Commit

Permalink
Merge branch 'db' into develop
Browse files Browse the repository at this point in the history
Conflicts:
	hamper/plugins/friendly.py
  • Loading branch information
mythmon committed Jul 24, 2011
2 parents 9da3c46 + ccabf81 commit 45b50f3
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 9 deletions.
21 changes: 16 additions & 5 deletions hamper/commander.py
Expand Up @@ -5,7 +5,8 @@

from twisted.words.protocols import irc
from twisted.internet import protocol, reactor

import sqlalchemy
from sqlalchemy import orm
from bravo.plugin import retrieve_plugins

from hamper.interfaces import IPlugin
Expand Down Expand Up @@ -110,6 +111,7 @@ def privmsg(self, user, channel, msg):
self.factory.registerPlugin(p)

def connectionLost(self, reason):
self.factory.db.commit()
reactor.stop()

def say(self, msg):
Expand All @@ -131,19 +133,27 @@ class CommanderFactory(protocol.ClientFactory):

protocol = CommanderProtocol

def __init__(self, channel, nickname):
self.channel = channel
self.nickname = nickname
def __init__(self, config):
self.channel = config['channel']
self.nickname = config['nickname']

self.history = {}

self.plugins = []
# These are so plugins can be added/removed at run time. The
# addition/removal will happen at a time when the list isn't being
# iterated, so nothing breaks.
self.pluginsToAdd = []
self.pluginsToRemove = []

if 'db' in config:
print('Loading db from config: ' + config['db'])
self.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()

for _, plugin in retrieve_plugins(IPlugin, 'hamper.plugins').items():
self.registerPlugin(plugin)

Expand All @@ -157,6 +167,7 @@ def registerPlugin(self, plugin):
"""
Registers a plugin.
"""
plugin.setup(self)
self.plugins.append(plugin)
self.plugins.sort()
print 'registered plugin', plugin.name
10 changes: 9 additions & 1 deletion hamper/interfaces.py
Expand Up @@ -9,6 +9,11 @@ class IPlugin(Interface):
name = Attribute('Human readable name for the plugin.')
priority = Attribute('Priority of plugins. High numbers are called first')

def setup(factory):
"""
Called when the factory loads the plugin.
"""

def process(bot, comm):
"""
Called when a matching message comes in to the bot.
Expand All @@ -34,6 +39,9 @@ def __init__(self):
opts = 0 if self.caseSensitive else re.I
self.regex = re.compile(self.regex, opts)

def setup(self, factory):
pass

def process(self, bot, comm):
if self.onlyDirected and not comm['directed']:
return
Expand All @@ -42,5 +50,5 @@ def process(self, bot, comm):
self.command(bot, comm, match.groups())
return True

def command(self, comm, groups):
def command(self, bot, comm, groups):
pass
5 changes: 3 additions & 2 deletions hamper/plugins/friendly.py
Expand Up @@ -14,7 +14,7 @@ class Friendly(object):
name = 'friendly'
priority = 2

def __init__(self):
def setup(self, factory):
self.greetings = ['hi', 'hello', 'hey']

def process(self, bot, comm):
Expand All @@ -36,8 +36,9 @@ class OmgPonies(object):

cooldown = 30 #seconds

def __init__(self):
def setup(self, factory):
self.last_pony_time = datetime.now()
pass

def process(self, bot, comm):
if re.match(r'.*pon(y|ies).*', comm['message'], re.I):
Expand Down
67 changes: 67 additions & 0 deletions hamper/plugins/quote.py
@@ -0,0 +1,67 @@
from datetime import datetime
import random

from zope.interface import implements
from sqlalchemy import Integer, String, Date, Column
from sqlalchemy.ext.declarative import declarative_base
import sqlalchemy

from hamper.interfaces import Command


SQLAlchemyBase = declarative_base()

class Quotes(Command):
'''Remember quotes, and recall on demand.'''

name = 'quotes'
priority = 0
regex = r'^quotes?(?: +(.*))?$'

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

def command(self, bot, comm, groups):
if groups[0]:
args = groups[0].split(' ')
args = [a.strip() for a in args if a.strip()]
else:
args = []

if len(args) == 0:
# Deliver a quote
index = random.randrange(0, bot.db.query(Quote).count() + 1)
quote = bot.factory.db.query(Quote)[index]
# Lame twisted irc doesn't support unicode.
bot.say(str(quote.text))
elif args[0] == '--args':
# Add a quote
text = ' '.join(args[1:])
quote = Quote(text, comm['user'])
bot.factory.db.add(quote)
bot.say('Succesfully added quote.')
elif args[0] == '--count':
bot.say('I know {0} quotes.'.format(bot.db.query(Quote).count()))
else:
bot.say('Wait, what?')


class Quote(SQLAlchemyBase):
'''The object that will get persisted by the database.'''

__tablename__ = 'quotes'

id = Column(Integer, primary_key=True)
text = Column(String)
adder = Column(String)
added = Column(Date)

def __init__(self, text, adder, added=None):
if not added:
added = datetime.now()

self.text = text
self.adder = adder
self.added = added

quotes = Quotes()
2 changes: 1 addition & 1 deletion scripts/hamper
Expand Up @@ -19,5 +19,5 @@ if __name__ == '__main__':
sys.exit();

reactor.connectTCP(config['server'], config['port'],
CommanderFactory(config['channel'], config['nickname']))
CommanderFactory(config))
reactor.run()

0 comments on commit 45b50f3

Please sign in to comment.