forked from MerlijnWajer/TInance
-
Notifications
You must be signed in to change notification settings - Fork 0
/
import.py
77 lines (54 loc) · 1.86 KB
/
import.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
#!/usr/bin/env python2
# encoding: utf-8
import os, sys, argparse, re
import datetime, time
import tidb
from tidb.sql import *
import json
NAME = 'Technologia Incognita'
def pretty_print(m, form, human):
print m.format(form, human_read=human).encode('utf-8')
parser = argparse.ArgumentParser(description='%s Import tool' % NAME,
epilog='')
parser.add_argument('-f', '--file', type=str, default='')
parser.add_argument('-i', '--really-import', action='store_true', default=False)
parser.add_argument('-C', '--no-check-hashes', action='store_true', default=False)
parser.add_argument('-R', '--reject-file', type=str, default='')
args = parser.parse_args()
if args.file == '':
parser.print_help()
sys.exit(1)
fd = open(args.file)
payments = json.loads(fd.read())
if args.reject_file:
rfd = open(args.reject_file)
rejects = map(lambda x: x.rstrip('\n'), rfd.readlines())
fp = []
for p in payments:
if p['hash'] not in rejects:
fp.append(p)
else:
print 'Rejecting:', p['hash']
payments = list(fp)
if not args.no_check_hashes:
fp = []
for p in payments:
q = Session.query(Payment).filter(Payment.payment_sha256 ==
p['hash']).first()
if q is not None:
print p['hash'], 'already in database!'
else:
fp.append(p)
payments = list(fp)
for p in payments:
memb = Session.query(Member).filter(Member.nick.like(p['nick'])).first()
if memb is None:
print >>sys.stderr, p['nick'], 'not known!'
raise Exception(p['nick'])
d = datetime.date(*map(int, p['date'].split('-')))
pay = Payment(member_id=memb.id, date=d, amount=p['amount'], comment=p['desc'],
months=p['months'], payment_sha256=p['hash'])
if args.really_import:
Session.add(pay)
if args.really_import:
Session.commit()