-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloader.py
More file actions
77 lines (63 loc) · 2.21 KB
/
loader.py
File metadata and controls
77 lines (63 loc) · 2.21 KB
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
__author__ = 'sumeetrohatgi'
import db_wrapper
import file_wrapper
import logging
def usage(message=None):
code = 0
if message:
print message
code = 1
print "usage: loader.py [-h] [-f] -d|--datafile <datafile-path> " \
"-t|--table <table_name> -c|--dbconn <db connection>"
sys.exit(code)
def check_arg(var, message):
if var is None:
usage(message)
if __name__ == "__main__":
import getopt
import sys
opts = None
try:
opts, args = getopt.getopt(sys.argv[1:],
"hft:d:c:",
["help", "force", "table=", "datafile=", "dbconn="])
except getopt.GetoptError as err:
usage(err)
filename = None
table_name = None
force = False
dbconn = None
for option, argument in opts:
if option in ('-f', '--force'):
force = True
elif option in ('-d', '--datafile'):
filename = argument
elif option in ('-t', '--table'):
table_name = argument
elif option in ('-c', '--dbconn'):
dbconn = argument
elif option == 'h':
usage()
else:
assert False, "unhandled option"
check_arg(filename, "please specify a file")
check_arg(table_name, "please specify table name")
check_arg(dbconn, "please specify database connection string")
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
filename='run.log',
filemode='a',
level=logging.DEBUG)
logging.info("*"*20)
logging.info("starting to load filename: %s into %s", filename, table_name)
logging.info("force flag = %s", force)
try:
with db_wrapper.DBWrapper(table_name, dbconn=dbconn, force=force) as loader:
# print "loader = ", loader
file_wrapper.parse_file(filename, loader.build_ddl, loader.insert)
loader.process_rows()
except:
logging.exception("unable to process file %s correctly", filename)
sys.exit(2)
finally:
logging.info("finished loading filename: %s into %s", filename, table_name)
logging.info("*"*20)