what am I doing
This commit is contained in:
parent
bdcd4c9ae5
commit
74b6c1f9a7
3 changed files with 39 additions and 9 deletions
|
@ -5,6 +5,9 @@ import os
|
||||||
import Queue
|
import Queue
|
||||||
import collections
|
import collections
|
||||||
|
|
||||||
|
from sqlalchemy.orm import scoped_session, sessionmaker
|
||||||
|
from sqlalchemy import create_engine
|
||||||
|
|
||||||
from core import config, irc, main, loader
|
from core import config, irc, main, loader
|
||||||
|
|
||||||
|
|
||||||
|
@ -97,6 +100,11 @@ class Bot(object):
|
||||||
self.config = config.Config(self.logger)
|
self.config = config.Config(self.logger)
|
||||||
self.logger.debug("Config object created.")
|
self.logger.debug("Config object created.")
|
||||||
|
|
||||||
|
# db
|
||||||
|
engine = create_engine('sqlite:///cloudbot.db')
|
||||||
|
db_factory = sessionmaker(bind=engine)
|
||||||
|
self.db_session = scoped_session(db_factory)
|
||||||
|
|
||||||
|
|
||||||
def connect(self):
|
def connect(self):
|
||||||
"""connect to all the networks defined in the bot config"""
|
"""connect to all the networks defined in the bot config"""
|
||||||
|
|
20
core/main.py
20
core/main.py
|
@ -3,6 +3,7 @@ import traceback
|
||||||
import Queue
|
import Queue
|
||||||
import re
|
import re
|
||||||
|
|
||||||
|
from sqlalchemy.orm import scoped_session
|
||||||
|
|
||||||
thread.stack_size(1024 * 512) # reduce vm size
|
thread.stack_size(1024 * 512) # reduce vm size
|
||||||
|
|
||||||
|
@ -55,12 +56,15 @@ class Input(dict):
|
||||||
def run(func, input):
|
def run(func, input):
|
||||||
args = func._args
|
args = func._args
|
||||||
|
|
||||||
|
uses_db = 'db' in args and 'db' not in input
|
||||||
|
|
||||||
if 'inp' not in input:
|
if 'inp' not in input:
|
||||||
input.inp = input.paraml
|
input.inp = input.paraml
|
||||||
|
|
||||||
if args:
|
if args:
|
||||||
if 'db' in args and 'db' not in input:
|
if uses_db:
|
||||||
input.db = get_db_connection(input.conn)
|
# create SQLAlchemy session
|
||||||
|
input.db = input.bot.db_session()
|
||||||
if 'input' in args:
|
if 'input' in args:
|
||||||
input.input = input
|
input.input = input
|
||||||
if 0 in args:
|
if 0 in args:
|
||||||
|
@ -73,6 +77,10 @@ def run(func, input):
|
||||||
if out is not None:
|
if out is not None:
|
||||||
input.reply(unicode(out))
|
input.reply(unicode(out))
|
||||||
|
|
||||||
|
if uses_db:
|
||||||
|
# close SQLAlchemy session
|
||||||
|
input.db.close()
|
||||||
|
|
||||||
|
|
||||||
def do_sieve(sieve, bot, input, func, type, args):
|
def do_sieve(sieve, bot, input, func, type, args):
|
||||||
try:
|
try:
|
||||||
|
@ -94,7 +102,6 @@ class Handler(object):
|
||||||
|
|
||||||
def start(self):
|
def start(self):
|
||||||
uses_db = 'db' in self.func._args
|
uses_db = 'db' in self.func._args
|
||||||
db_conns = {}
|
|
||||||
while True:
|
while True:
|
||||||
input = self.input_queue.get()
|
input = self.input_queue.get()
|
||||||
|
|
||||||
|
@ -102,11 +109,7 @@ class Handler(object):
|
||||||
break
|
break
|
||||||
|
|
||||||
if uses_db:
|
if uses_db:
|
||||||
db = db_conns.get(input.conn)
|
input.db = self.bot.db
|
||||||
if db is None:
|
|
||||||
db = self.bot.get_db_connection(input.conn)
|
|
||||||
db_conns[input.conn] = db
|
|
||||||
input.db = db
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
run(self.func, input)
|
run(self.func, input)
|
||||||
|
@ -165,7 +168,6 @@ def main(bot, conn, out):
|
||||||
prefix = '^(?:[{}]?|'.format(command_prefix)
|
prefix = '^(?:[{}]?|'.format(command_prefix)
|
||||||
else:
|
else:
|
||||||
prefix = '^(?:[{}]|'.format(command_prefix)
|
prefix = '^(?:[{}]|'.format(command_prefix)
|
||||||
|
|
||||||
command_re = prefix + inp.conn.nick
|
command_re = prefix + inp.conn.nick
|
||||||
command_re += r'[,;:]+\s+)(\w+)(?:$|\s+)(.*)'
|
command_re += r'[,;:]+\s+)(\w+)(?:$|\s+)(.*)'
|
||||||
|
|
||||||
|
|
20
plugins/dbtest.py
Normal file
20
plugins/dbtest.py
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
from util import hook
|
||||||
|
|
||||||
|
from sqlalchemy import Table, Column, Integer, String, MetaData, ForeignKey
|
||||||
|
from sqlalchemy.orm import scoped_session, sessionmaker
|
||||||
|
|
||||||
|
def create_tables(metadata):
|
||||||
|
users_table = Table('users', metadata,
|
||||||
|
Column('id', Integer, primary_key=True),
|
||||||
|
Column('name', String),
|
||||||
|
Column('fullname', String),
|
||||||
|
Column('password', String)
|
||||||
|
)
|
||||||
|
users_table.create()
|
||||||
|
|
||||||
|
|
||||||
|
@hook.command
|
||||||
|
def dbtest(inp, db=None):
|
||||||
|
metadata = MetaData(db)
|
||||||
|
create_tables(metadata)
|
||||||
|
print metadata
|
Reference in a new issue