-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.py
91 lines (73 loc) · 2.52 KB
/
cli.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
81
82
83
84
85
86
87
88
89
90
91
# -*- mode: python; encoding: utf-8 -*-
#
# Copyright 2012 Jens Lindström, Opera Software ASA
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
import sys
try: from json import dumps as json_encode, loads as json_decode
except: from cjson import encode as json_encode, decode as json_decode
import dbutils
import review.utils as review_utils
db = None
def init():
global db
db = dbutils.Database()
def finish():
global db
if db:
db.commit()
db.close()
db = None
def abort():
global db
if db:
db.rollback()
db.close()
db = None
try:
if len(sys.argv) > 1:
init()
for command in sys.argv[1:]:
pending_mails = None
if command == "generate-mails-for-batch":
data = json_decode(sys.stdin.readline())
batch_id = data["batch_id"]
was_accepted = data["was_accepted"]
is_accepted = data["is_accepted"]
pending_mails = review_utils.generateMailsForBatch(db, batch_id, was_accepted, is_accepted)
elif command == "generate-mails-for-assignments-transaction":
data = json_decode(sys.stdin.readline())
transaction_id = data["transaction_id"]
pending_mails = review_utils.generateMailsForAssignmentsTransaction(db, transaction_id)
else:
print "unknown command: %s" % command
sys.exit(1)
if pending_mails is not None:
sys.stdout.write(json_encode(pending_mails) + "\n")
finish()
finally:
abort()
def tokenize(tokens, filename="<unknown>"):
line = 1
column = 0
for token in tokens:
if isinstance(token, Token):
yield token
token1 = str(token)
else: yield Token(token, filename, line, column)
linebreaks = token.count("\n")
if linebreaks:
line += linebricks
column = len(token) - 1 - token.rindex("\n")
else:
column += len(token)