-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_main.py
314 lines (301 loc) · 10.5 KB
/
run_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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import tkinter as tk
import tkinter.font as tkFont
import tkinter.ttk as ttk
from tkinter import *
from tkinter.filedialog import askopenfilename
from tkinter.filedialog import asksaveasfile
from ItemData import ItemData
from GUI1 import *
from Data_Analysis import *
from datetime import date
#Future Possibilities
#import hyperlink
#https://hyperlink.readthedocs.io/en/latest/
#Variables-----------------
item_header = [' Name ', 'Lowest Price ($)', 'Current Price ($)', ' Link ']
item_list = []
#Is an older data opened:
old = False
#Is a new data loaded:
op = False
fileUsing = ""
cvTitle = ""
#GUI Frame for GUI2 and GUI3-----------------
class MultiColumnListbox(object):
def __init__(self):
self.tree = None
self.setup()
self._build_tree()
def setup(self):
cvTitle = get_cvTitle()
s = """Currently Viewing: """ + cvTitle + """ || Note: click on header to sort by column
If you would like to merge past list with new link entered, please do not "save" until you have opened past document"""
msg = ttk.Label(wraplength="4i", justify="left", anchor="n",
padding=(10, 2, 10, 6), text=s)
msg.pack(fill='x')
container = ttk.Frame()
container.pack(fill='both', expand=True)
# create a treeview with dual scrollbars
self.tree = ttk.Treeview(columns=item_header, show="headings")
vsb = ttk.Scrollbar(orient="vertical",
command=self.tree.yview)
hsb = ttk.Scrollbar(orient="horizontal",
command=self.tree.xview)
self.tree.configure(yscrollcommand=vsb.set,
xscrollcommand=hsb.set)
self.tree.grid(column=0, row=0, sticky='nsew', in_=container)
vsb.grid(column=1, row=0, sticky='ns', in_=container)
hsb.grid(column=0, row=1, sticky='ew', in_=container)
container.grid_columnconfigure(0, weight=1)
container.grid_rowconfigure(0, weight=1)
def _build_tree(self):
for col in item_header:
self.tree.heading(col, text=col.title(),
command=lambda c=col: sortby(self.tree, c, 0))
# adjust the column's width to the header string
self.tree.column(col,
width=tkFont.Font().measure(col.title()))
for item in item_list:
self.tree.insert('', 'end', values=item)
# adjust column's width if necessary to fit each value
for ix, val in enumerate(item):
col_w = int(tkFont.Font().measure(val)*2/3)
if self.tree.column(item_header[ix],width=None)<col_w:
self.tree.column(item_header[ix], width=col_w)
#Sorting data in GUI-----------------
def sortby(tree, col, descending):
"""sort tree contents when a column header is clicked on"""
# grab values to sort
data = [(tree.set(child, col), child) \
for child in tree.get_children('')]
# if the data to be sorted is numeric change to float
#data = change_numeric(data)
# now sort the data in place
data.sort(reverse=descending)
for ix, item in enumerate(data):
tree.move(item[1], '', ix)
# switch the heading so it will sort in the opposite direction
tree.heading(col, command=lambda col=col: sortby(tree, col, \
int(not descending)))
#Comparing old data with new data when opening old file with a new data opened-----------------
def compare(old, new):
#Things to improve: if code is handling an costco catagory more than 1 page, "compare" should recognize
# that it is not compareing old with new but adding new with new items, this could be achieved by adding
# variable that confirms there are nothing duplicated and the date of the recording for two lists equals
dataC = []
#day = date.today.shiftime("%m/%d/%y")
for o in old:
check1 = False
for n in new:
if o[0].strip() == n[0].strip():
check1 = True
lowest_price = ""
if o[2] == "Unavailable":
lowest_price = n[2]
elif float(o[2].replace(',','').replace('$','')) > float(n[2].replace(',','').replace('$','')):
lowest_price = n[2]
else:
lowest_price = o[2]
if o[1].strip() != "N/A":
if float(o[1].replace(',','').replace('$','')) < float(lowest_price.replace(',','').replace('$','')):
lowest_price = o[1]
dataC.append((n[0], lowest_price, n[2], n[3]))
if check1 == False:
dataC.append((o[0],o[2],"Unavailable","N/A"))
for nn in new:
check2 = False
for dd in dataC:
if nn[0].strip() == dd[0].strip():
check2 = True
if check2 == False:
dataC.append((nn[0],nn[1],nn[2],nn[3]))
return dataC
#Menu Functions-----------------
def writeFile(arr, FN, t):
file = open(FN,"w+")
file.write(t+ " -----LAST: " + date.today().strftime("%m/%d/%y") +"\n")
for x in arr:
a = 0
while a<4:
file.write("{" + str(a) + "}" + x[a])
file.write("\n")
a+=1
file.write("=====\n")
file.close
def loadFile(FN):
arr2 = []
count = 0
L1 = ""
L2 = ""
L3 = ""
L4 = ""
with open(FN, "rt") as reading:
for line in reading:
if line.find(" -----LAST:") != -1:
if count ==0:
temp_title = line[:line.find(" -----LAST:")]
if temp_title != cvTitle:
set_cvTitle(get_cvTitle() + temp_title)
else:
return "BAD FILE"
elif line.find("=====") != -1:
arr2.append((L1, L2, L3, L4))
L1 = ""
L2 = ""
L3 = ""
L4 = ""
elif line.strip() == "":
pass
elif line.find("{0}") != -1:
L1 = line[3:].strip("\n")
elif line.find("{1}") != -1:
L2 = line[3:].strip("\n")
elif line.find("{2}") != -1:
L3 = line[3:].strip("\n")
elif line.find("{3}") != -1:
L4 = line[3:].strip("\n")
else:
pass
count += 1
return arr2
def SaveFile():
fileUsing = get_fileUsing()
op = get_op()
cvTitle = get_cvTitle()
item_list = get_item_list()
if op != True:
print("No changes made that needs to be saved")
elif oldFileOpened("C"):
writeFile(item_list, fileUsing, cvTitle)
else:
set_fileUsing(asksaveasfile())
if get_fileUsing() == None:
return
naming = str(get_fileUsing())
naming = naming[naming.find("name='")+6:]
naming = naming[0:naming.find("' mode='w'")]
set_fileUsing(naming)
print(naming)
writeFile(item_list, get_fileUsing(), cvTitle)
def OpenFile():
op = get_op()
item_list = get_item_list()
if oldFileOpened("C"):
print("Seems like a file is already opened")
else:
set_fileUsing(askopenfilename())
if get_fileUsing() == "":
return
print("Opening file from: " + get_fileUsing())
oldFileOpened("T")
arr2 = loadFile(get_fileUsing())
if op:
set_item_list(compare(arr2, item_list))
else:
set_item_list(arr2)
def About():
print("This is Costco Recorder made by Jason Hsu")
#GUI Command Editor-----------------
def combine_funcs(*funcs):
def combined_func(*args, **kwargs):
for f in funcs:
f(*args, **kwargs)
return combined_func
#Accessing GUI 2,3-----------------
def runtk2(opp):
root = tk.Tk()
root.title("Costco Recorder List")
if opp:
listbox = MultiColumnListbox()
else:
label = tk.Label(root, text="""
Go to top left menu's "File" and then "Open"
to open existing file""")
label.pack()
#---------------menu implementation-----------------
menu = Menu(root)
root.config(menu=menu)
filemenu = Menu(menu)
menu.add_cascade(label="File", menu=filemenu)
filemenu.add_command(label="Save", command=SaveFile)
filemenu.add_command(label="Open...", command= combine_funcs(OpenFile, root.destroy))
filemenu.add_separator()
filemenu.add_command(label="Exit", command=root.quit)
helpmenu = Menu(menu)
menu.add_cascade(label="Help", menu=helpmenu)
helpmenu.add_command(label="About...", command=About)
#----------------------------------------------------
root.geometry("1000x500")
root.mainloop()
def runtk3():
root2 = tk.Tk()
root2.title("Costco Recorder List With Old Recording")
listbox = MultiColumnListbox()
#---------------menu implementation-----------------
menu = Menu(root2)
root2.config(menu=menu)
filemenu = Menu(menu)
menu.add_cascade(label="File", menu=filemenu)
filemenu.add_command(label="Save", command=SaveFile)
filemenu.add_separator()
filemenu.add_command(label="Exit", command=root2.quit)
helpmenu = Menu(menu)
menu.add_cascade(label="Help", menu=helpmenu)
helpmenu.add_command(label="About...", command=About)
#----------------------------------------------------
root2.geometry("1000x500")
root2.mainloop()
#Accessing variables---------------
def get_item_list():
return item_list
def set_item_list(items):
global item_list
item_list = items
def get_old():
return old
def oldFileOpened(x):
global old
if x == "T":
old = True
return
elif x =="F":
old = False
return
elif x=="C":
return old
else:
return
def get_op():
return op
def set_op(boo):
global op
op = boo
def get_fileUsing():
return fileUsing
def set_fileUsing(text):
global fileUsing
fileUsing = text
def get_cvTitle():
return cvTitle
def set_cvTitle(title):
global cvTitle
cvTitle = title
#Main---------------
if __name__ == "__main__":
weblink = runtk1.callGUI()
if weblink == "readfile":
op = False
else:
op = True
loadAndAnalysis(weblink)
cvTitle = getTitle()
dataList = getData()
for x in dataList:
if cvTitle == x.getType:
price = x.getPrice.strip("\n")
if price.find(",")==-1:
price = price.replace("$","$,")
item_list.append((x.getTitle.strip("\n"), "N/A", price, x.getLink))
runtk2(op)
if oldFileOpened("C"):
runtk3()