-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
74 lines (59 loc) · 2.45 KB
/
main.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
#!/usr/bin/env python
# coding: utf-8
import psycopg2
from emf2py import emf_to_py
from utils import *
import argparse
import sys
parser = argparse.ArgumentParser()
parser.add_argument("-p", "--code-path", dest="code_path", type=str, metavar='<str>', default="E:/OneDrive - stevens.edu/Stevens BIA/CS562/Proj/EMF2PY/")
parser.add_argument("-i", "--input-file", dest="input_file", type=str, metavar='<str>', default="Input_files/input1.txt")
parser.add_argument("-o", "--output-file", dest="output_file", type=str, metavar='<str>', default="Output_files/out1.py")
args = parser.parse_args()
# print(args.input_file)
# print(args.output_file)
# input_path = "E:/OneDrive - stevens.edu/Stevens BIA/CS562/Proj/EMF2PY/" + "Input_files/"
# output_path = "E:/OneDrive - stevens.edu/Stevens BIA/CS562/Proj/EMF2PY/" + "Output_files/"
with open(args.code_path + args.input_file, 'r', encoding='utf') as f:
txt = f.read().strip().split('\n')
########################################
# connect DB login ==> get schema & log info
########################################
table = "sales"
dbname = "postgres"
host = "localhost"
user = "postgres"
password = "00000000"
port = "5388"
try:
connection = psycopg2.connect(user=user, password=password, host=host, port=port, database=dbname)
query = "SELECT column_name, data_type FROM information_schema.columns WHERE table_name = '" + table + "';"
cursor = connection.cursor()
cursor.execute(query)
if cursor.rowcount == 0:
print("Connected, but no data in table: %s " % table)
sys.exit(0)
schema = cursor.fetchall()
except(Exception, psycopg2.Error) as error:
print("Error connecting to PostgreSQL database ==>", error)
sys.exit(0)
print("=" * 40)
print("In <<'%s'>> table" % format(table))
print("=" * 40)
print("ATTRIBUTES".ljust(14, " "), "DTYPE".ljust(15, " "))
for i in schema:
print("*", i[0].ljust(12, " "), i[1].ljust(15, " "))
print("=" * 40)
PG_log_info = {'user': user, 'password': password, 'host': host, 'port': port, 'database': dbname}
########################################
# Parsing input txt
########################################
S, n, V, F, C, G = txt_parsing(txt)
########################################
# Processing and Save result
########################################
res = emf_to_py(PG_log_info, S,n,V,F,C,G, schema)
to_file = args.code_path + args.output_file
with open(to_file, "w", encoding='utf-8') as f:
f.write(res)
print("The file has been saved in Output_files:", './Output_files/'+args.output_file)