-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclassrank.py
53 lines (43 loc) · 1.61 KB
/
classrank.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
import argparse
import json
import os
import tornado.ioloop
import tornado.web
import classrank.app as app
from classrank.app import ClassRankApp
from classrank.routing import routes
from classrank.database.wrapper import Query
from classrank.database.wrapper import IntegrityError
def parser():
p = argparse.ArgumentParser(description="ClassRank webserver configuration")
p.add_argument('-p', '--port', default=os.environ.get('port', 8000))
p.add_argument('-s', '--settings', default=os.environ.get('settings', 'config.json.example'))
p.add_argument('-d', '--debug', action='store_true')
p.add_argument('-c', '--connection', default=os.environ.get('connection', None))
return p
if __name__ == "__main__":
parser = parser()
args = parser.parse_args()
settings = {
"static_path": os.path.join(os.path.dirname(__file__), "classrank/static"),
"template_path": os.path.join(os.path.dirname(__file__), "classrank/templates")
}
try:
with open(args.settings) as f:
settings.update(json.loads(f.read()))
except FileNotFoundError:
# no additional settings file so we ignore
pass
settings['debug'] == args.debug
db_config = settings['db_config']
del settings['db_config']
cr = ClassRankApp(args.connection, routes, **settings)
try:
with Query(cr.db) as q:
for table in db_config:
for item in db_config[table]:
q.add(cr.db.__getattribute__(table)(**item))
except IntegrityError as e:
print(e)
cr.listen(args.port)
tornado.ioloop.IOLoop.current().start()