-
Notifications
You must be signed in to change notification settings - Fork 0
/
import.py
61 lines (48 loc) · 1.62 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
import config
import logging
import argparse
import sys
import csv
from db import Vouchers, connect_db
from sqlalchemy.orm import sessionmaker
db=connect_db() #establish connection
Session = sessionmaker(bind=db)
session = Session()
def importCSV(file, time):
bulkInsert=[]
keep={}
csv.register_dialect('myDialect',
skipinitialspace=True
)
with open(file) as csvfile:
reader = csv.reader(csvfile, dialect="myDialect")
for row in reader:
if row[0].startswith("#"):
continue
keep['vid'] = row[0].lstrip(' ')
keep['time'] = time
bulkInsert.append(Vouchers(**keep))
if bool(bulkInsert):
session.bulk_save_objects(bulkInsert)
session.commit()
logging.info(f"Inserted {len(bulkInsert)} vouchers")
else:
logging.error(f"Failed to parse {file}")
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--debug", help="enable debug output", action="store_true")
parser.add_argument("--file", help="Provide the path to the CSV", type=str)
parser.add_argument("--minutes", help="How many minutes are assigned to this roll", type=int)
args, unknown_args = parser.parse_known_args()
logging_level = logging.INFO
if args.debug:
config.debug = True
config.db_debug = True
logging_level = logging.DEBUG
logging.getLogger()
logging.basicConfig(format='%(asctime)s %(message)s', level=logging_level)
logging.info("args: {}".format(args))
if not args.file or not args.minutes:
logging.error("Both --minutes and --file are requred input for importing")
sys.exit(1)
importCSV(args.file, args.minutes)