forked from sausame/coupon
/
outputter.py
executable file
·77 lines (52 loc) · 1.64 KB
/
outputter.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
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import os
import sys
import time
import traceback
from base import SpecialFormatter
from datetime import datetime
from db import Database
from evaluation import Evaluation
from utils import getchar, getProperty, reprDict, runCommand, OutputPath, ThreadWritableObject
def run(configfile, name):
OutputPath.init(configFile)
thread = ThreadWritableObject(configFile, name)
thread.start()
sys.stdout = thread
sys.errout = thread # XXX: Actually, it does NOT work
try:
db = Database(configFile, 'specials')
db.initialize()
evaluation = Evaluation(configFile, db)
evaluation.updateOverdue()
path = OutputPath.getSharePath()
sharePath = getProperty(configFile, 'output-share-file')
cmd = '/bin/rm -f {1} && /bin/ln -s {0} {1}'.format(path, sharePath)
runCommand(cmd)
data = evaluation.output()
with open(path, 'w') as fp:
fp.write(reprDict(data))
except KeyboardInterrupt:
pass
except Exception, e:
print 'Error occurs at', datetime.now().strftime('%Y-%m-%d %H:%M:%S')
traceback.print_exc(file=sys.stdout)
finally:
try:
db.quit()
except:
pass
thread.quit()
thread.join()
if __name__ == '__main__':
reload(sys)
sys.setdefaultencoding('utf-8')
if len(sys.argv) < 2:
print 'Usage:\n\t', sys.argv[0], 'config-file\n'
exit()
os.environ['TZ'] = 'Asia/Shanghai'
time.tzset()
name = os.path.basename(sys.argv[0])[:-3] # Remove ".py"
configFile = sys.argv[1]
run(configFile, name)