-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
81 lines (59 loc) · 2.31 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/python
# -*- coding: utf-8 -*-
from todo.lingrbot import ToDoBot, Lingrman, Spool
from flask import Flask, request
from flask import json
import os
import logging
from logging.handlers import RotatingFileHandler
class Flasky(ToDoBot):
def lingrbot(self):
if request.method == 'POST':
array = json.loads(request.data)
return ''.join([self.postman.deliver(self.on_json(event)) for event in array['events']])
else:
return 'hello! ' #self.handle_about(None, None, None, None, None)
def one_arg(self, cmd, username):
spool = Spool(None)
got = self.handle(cmd, spool, username)
return "<br/>".join([r.prnformat() for r in got.rows])
app = Flask(__name__)
formatter = logging.Formatter(
'%(asctime)s %(levelname)s: %(message)s '
'[in %(pathname)s:%(lineno)d]'
)
debug_log = os.path.join(app.root_path, 'logs/debug.log')
debug_file_handler = RotatingFileHandler(
debug_log, maxBytes=100000, backupCount=10
)
debug_file_handler.setLevel(logging.INFO)
debug_file_handler.setFormatter(formatter)
app.logger.addHandler(debug_file_handler)
error_log = os.path.join(app.root_path, 'logs/error.log')
error_file_handler = RotatingFileHandler(
error_log, maxBytes=100000, backupCount=10
)
error_file_handler.setLevel(logging.ERROR)
error_file_handler.setFormatter(formatter)
app.logger.addHandler(error_file_handler)
if __name__ == '__main__':
import sys
from sqlalchemy import create_engine
from sqlalchemy.pool import QueuePool
# maybe poolclass=SingletonThreadPool
from sqlalchemy.orm import sessionmaker, scoped_session
from todo import models
if len(sys.argv) > 1 and sys.argv[1] == 'debug':
app.debug = True
else:
app.logger.setLevel(logging.INFO)
engine = create_engine('sqlite:///./todo.sqlite', poolclass=QueuePool)
lingr = Lingrman(b'todo2', bot_secret=open('todo.txt', mode='rb').read().rstrip())
models.get_session = scoped_session(sessionmaker(bind=engine))
bot = Flasky(lingr) #UGH! Fixme
app.route('/lingrbot', methods=['GET', 'POST'])(bot.lingrbot)
#app.route('/<cmd>/<username>', methods=['GET'])(bot.one_arg)
if app.debug:
app.run()
else:
app.run(host='0.0.0.0', port=11001)