-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser_1.py
More file actions
184 lines (141 loc) · 5.63 KB
/
parser_1.py
File metadata and controls
184 lines (141 loc) · 5.63 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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import csv
from caching import Caching
from const import USAGE_LIMIT
from utils import log, getInt, printProgressBar
import time
from compare import Compare
from limit_handler import LimitHandler
from firebase_admin import firestore
def getFormatted(data: str):
if data.isdecimal():
return int(data)
elif data == '':
return None
else:
return str(data)
def genData(headers: list, row: list):
data = {}
for header in headers:
try:
data[str(header)] = getFormatted(row[headers.index(header)])
except:
data[str(header)] = None
return data
class Parser:
def openCSV(path):
log("Parser - openCSV Called")
file = open(path, encoding="utf8")
log(type(file))
return csv.reader(file)
def parse(path, entry_name):
log("Parser - parse called")
db = firestore.client()
usage = LimitHandler.getCurrentUsage()
print("Current usage: {}".format(usage))
print("Daily writes left: {}".format(USAGE_LIMIT - usage))
file = open(path, encoding="utf8")
csvreader = csv.reader(file)
header = next(csvreader)
rows = []
for row in csvreader:
# log("Prasing row {}".format(row[0]))
rows.append(row)
log("Starting import to Firestore job...")
time.sleep(2)
l = len(rows)
printProgressBar(0, l, prefix = 'Progress:', suffix = 'Complete', length = 50)
doc_ref = db.collection(entry_name)
i = 0
for row in rows:
usage += 1
if(usage > USAGE_LIMIT):
print("Daily usage limit exceeded, rest of the data is cached and will be uploaded in the next cycle.")
Caching.saveCachingIndex(path, i)
exit()
try:
data = genData(header, row)
doc_ref.document(str(row[0])).set(data)
except:
print("Daily usage limit exceeded, rest of the data is cached and will be uploaded in the next cycle. (Server)")
Caching.saveCachingIndex(i)
exit()
i += 1
LimitHandler.incrementUsage()
printProgressBar(i, l, prefix = 'Progress:', suffix = 'Complete ({}/{})'.format(i,l), length = 50)
Caching.saveCachingIndex(path, -1)
def parseUpdated(path, old, entry_name):
log("Parser - parse updated called")
db = firestore.client()
usage = LimitHandler.getCurrentUsage()
print("Current usage: {}".format(usage))
print("Daily writes left: {}".format(USAGE_LIMIT - usage))
file = open(path, encoding="utf8")
csvreader = csv.reader(file)
header = next(csvreader)
file.close()
updated_rows = Compare.compare(path, old)
deletedFields = Compare.getDeletedFields(path, old)
print("Delete: ")
print(deletedFields)
log("Starting import to Firestore job...")
time.sleep(2)
l = len(updated_rows)
if(l == 0):
print("No Updated Field found.")
doc_ref = db.collection(entry_name)
i = 0
for row in updated_rows:
usage += 1
if(usage > USAGE_LIMIT):
print("Daily usage limit exceeded, rest of the data is cached and will be uploaded in the next cycle.")
Caching.saveCachingIndex(path, i)
exit()
try:
data = genData(header, row)
doc_ref.document(str(row[0])).set(data)
except:
print("Daily usage limit exceeded, rest of the data is cached and will be uploaded in the next cycle. (Server)")
Caching.saveCachingIndex(i)
exit()
i += 1
printProgressBar(i, l, prefix = 'Progress:', suffix = 'Complete ({}/{})'.format(i,l), length = 50)
print("Deleting unused fields")
for id in deletedFields:
log("deleting {} {}".format(entry_name, id))
doc_ref.document(id).delete()
def parseCached(path, count):
log("Parser - parse updated called")
db = firestore.client()
usage = LimitHandler.getCurrentUsage()
print("Current usage: {}".format(usage))
print("Daily writes left: {}".format(USAGE_LIMIT - usage))
file = open(path, encoding="utf8")
csvreader = csv.reader(file)
header = next(csvreader)
rows = []
c = 0
for row in csvreader:
rows.append(row)
log("Starting import to Firestore job...")
time.sleep(2)
l = len(rows)
doc_ref = db.collection("entries")
i = 0
for row in rows:
if i > count - 1:
usage += 1
if(usage > USAGE_LIMIT):
print("Daily usage limit exceeded, rest of the data is cached and will be uploaded in the next cycle.")
Caching.saveCachingIndex(path, i)
exit()
try:
data = genData(header, row)
doc_ref.document(str(row[0])).set(data)
except:
print("Daily usage limit exceeded, rest of the data is cached and will be uploaded in the next cycle. (Server)")
Caching.saveCachingIndex(path, i)
exit()
LimitHandler.incrementUsage()
printProgressBar(i, l, prefix = 'Progress:', suffix = 'Complete ({}/{})'.format(i,l), length = 50)
i += 1
Caching.saveCachingIndex(path, -1)