-
Notifications
You must be signed in to change notification settings - Fork 0
/
sddsdce_import.py
123 lines (93 loc) · 3.59 KB
/
sddsdce_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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/python3
"""
"""
import sys
import datetime
import csv
import yaml
import mysql.connector
from mysql.connector import Error
from argparse import ArgumentParser
def config():
with open("config.yml", "r") as f:
return yaml.load(f)
# Function to covert string to datetime
def convert(date_time):
format = '%m/%d/%Y' # The format
return datetime.datetime.strptime(date_time, format)
def _printcitation(date,number,type,appname,status,desc):
print("-------")
print("Citation :",number)
print("Date :",date)
print("Type :",type)
print("App Name :",appname)
print("Status :",status)
print("Description:",desc, "\n")
def getopts():
parser = ArgumentParser()
parser.add_argument("-f", "--file", required=True, help="CSV file to process.")
parser.add_argument("-u", "--update", action="store_true", help="Update database.")
args = parser.parse_args()
file = args.file;
return args.update, file
def main():
update_records, datafile = getopts()
NEWRECORDS = 0
cfg = config()
try:
connection = mysql.connector.connect(user=cfg["mysql"]["user"],
password=cfg["mysql"]["password"],
host=cfg["mysql"]["host"],
database=cfg["mysql"]["db"])
cursor = connection.cursor()
with open(datafile) as csvfile:
cits = csv.reader(csvfile, delimiter=',')
itercits = iter(cits) # skip header
next(itercits)
for cit in itercits:
cdate = cit[0];
cnumber = cit[1];
ctype = cit[2];
cappname = cit[3];
cstatus = cit[4];
cdesc = cit[5];
q = """SELECT * FROM sddsdce WHERE `Record Number` = '%s'""" % cnumber
cursor.execute(q, cnumber)
record = cursor.fetchone()
if cursor.rowcount == -1:
print ("New Record.")
_printcitation(cdate,cnumber,ctype,cappname,cstatus,cdesc)
if update_records:
q = """INSERT INTO sddsdce
(`Date`, `Record Number`, `Record Type`, `Application Name`, `Status`, `Description`, `Code`)
VALUES
('%s','%s','%s','%s','%s','%s', '%s')""" % (convert(cdate),cnumber,ctype,cappname,cstatus,cdesc.replace('"','_').replace('\'',""), 'NEW')
print ("Creating Citation Record!", "\n========\n")
cursor.execute(q)
connection.commit()
NEWRECORDS += 1
else:
RECORDCHANGE = False
if ctype != record[2]: RECORDCHANGE = True
if cappname != record[3]: RECORDCHANGE = True
if cstatus != record[4]: RECORDCHANGE = True
if RECORDCHANGE:
_printcitation(cdate,cnumber,ctype,cappname,cstatus,cdesc)
_printcitation(record[0],record[1],record[2],record[3],record[4],record[5])
if args.update:
q = """UPDATE sddsdce SET
`Date` = '%s', `Record Type` = '%s', `Application Name` = '%s', `Status` = '%s',
`Description` = '%s', `Code` = '%s' WHERE `Record Number` = '%s'""" % (convert(cdate),ctype,cappname,cstatus,cdesc.replace('"','_').replace('\'',""), 'CITUPDATED',cnumber)
print ("Updating Citation Record!", "\n========\n")
cursor.execute(q)
connection.commit()
print ("Records Inserted: ", NEWRECORDS)
except Error as e:
print("Error reading data from MySQL table", e)
finally:
if connection is not None and connection.is_connected():
connection.close()
cursor.close()
if __name__ == "__main__":
main()
#END