-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathimport.py
72 lines (59 loc) · 2.16 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
"""
Execute command using argument parser
"""
import sys
import argparse
from core import connection, importer
def get_arguments(argv):
"""
Get arguments using ArgumentParser
:param argv:
:return args:
"""
parser = argparse.ArgumentParser(description='CSV, XLS, XLSX data set migration to MySQL database')
parser.add_argument('-t', '--type', help='The dataset type. Spreadsheet or csv')
parser.add_argument('-f', '--file', help='The dataset file to use')
parser.add_argument('-s', '--sheet', help='The sheet name to use')
parser.add_argument('-H', '--host', help='The host name to use')
parser.add_argument('-U', '--user', help='The username to use')
parser.add_argument('-P', '--password', help='The password to use')
parser.add_argument('-D', '--database', help='The database name to use')
args = parser.parse_args()
return args
def main(args):
print("\nImporting...")
# Variables #
type = args.type
file = args.file
sheet = args.sheet
host = args.host
user = args.user
password = args.password
database = args.database
db = connection.connect(user, password, host, database)
if type == 'spreadsheet':
if sheet:
importer.run_spreadsheet_import(sheet, file, db)
else:
print("Missing sheet name!! Importing data from first index on spreadsheet")
importer.run_spreadsheet_import(sheet, file, db)
elif type == 'csv':
importer.run_csv_import(file, db)
if __name__ == '__main__':
arguments = get_arguments(sys.argv)
if sys.version_info < (3, 0):
input = raw_input
proceed = input("Importing data from dataset {}\nProceed (yes/no)? ".format(arguments.file))
valid = ["yes", "y", "no", "n"]
while True:
if proceed.lower() in valid:
if proceed.lower() == "yes" or proceed.lower() == "y":
main(arguments)
print("Done!")
break
else:
print("Goodbye!")
break
else:
print("Please respond with 'yes' or 'no' (or 'y' or 'n').")
proceed = input("\nProceed (yes/no)? ")