-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui_parent.py
310 lines (217 loc) · 11.5 KB
/
gui_parent.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
import os, sys, sqlite3
import time
import platform
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox
from article_parent import articleIDsClass
from log_parent import log_parametersClass
class guiClass:
def __init__(self, database, containerArticles, logParameters):
self.root=tk.Tk()
self.root.title('Welcome to ID_Extractor')
self.root.iconbitmap("logo.ico")
self.paddings = {'padx': 10, 'pady': 10}
style = ttk.Style(self.root)
style.configure("TLabel", font=('Helvetica', 12))
style.configure("TEntry", background = 'white')
style.configure("TButton", font=('Helvetica', 12))
style.configure("TCheckbutton", font=('Helvetica', 10))
#-- Parameters
self.message = tk.StringVar(self.root)
self.inputString = tk.StringVar(self.root)
self.path = ""
self.rowNumber = 0
welcomeMessage = tk.StringVar(self.root)
welcomeMessageString = "Welcome to ID_EXTRACTOR " + logParameters.ID_X_version + "\n"
welcomeMessage.set(welcomeMessageString)
ttk.Label(self.root, textvariable = welcomeMessage, style="TLabel").\
grid(column=0,row = self.rowNumber, sticky ='w', **self.paddings)
#-- First create the non-static head...
self.actualize_head(database)
#-- Now create the checkbuttons...
ttk.Label(self.root, text="Choose an export function:").\
grid(column=0, row = self.rowNumber, sticky ='w', **self.paddings)
selectedExports = []
val = []
i=0
for x, y in database.databases.items():
val.append(tk.StringVar(value=0)) #Checkbuttons forced to be off
check = ttk.Checkbutton(self.root, text = y['dbDescription'], variable = val[i])
check.grid(column=0,
row = self.rowNumber + 1, sticky ='w', **self.paddings)
selectedExports.append(check)
self.rowNumber = self.rowNumber + 1
i += 1
#-- Now the menue buttons...
ttk.Button(self.root, text = "Run application", style = "TButton",
command=lambda: self.start_process(selectedExports,
database, containerArticles, logParameters)).grid(column=0,
row = self.rowNumber + 1,
sticky ='w', **self.paddings)
ttk.Button(self.root, text = "Exit application", style = "TButton",
command=lambda: self.exit_application(database, containerArticles, logParameters)).grid(column=1,
row = self.rowNumber + 1,
sticky ='e', **self.paddings)
tk.mainloop()
def actualize_head(self, database):
self.rowNumber = 1
database.check_if_db_exists()
database.actualize_nr_of_entries()
infoMessage = "Found ID_Ex_database.db with " + str(database.dbSize) + " entries. Do you wish to run another extraction?"
message = "database.db has " + str(database.dbSize) + " entries. \n\n"
database.logBuffer.append(message)
if database.dbFromPreviousRunComplete == True:
ttk.Label(self.root, text=infoMessage, style="TLabel").\
grid(column=0,row = self.rowNumber, sticky ='w', **self.paddings)
self.chooseExtraction = tk.StringVar(self.root)
self.extractionSelected = ttk.Radiobutton(self.root, variable = self.chooseExtraction, text = "yes")
self.extractionSelected.grid(column=1, row = self.rowNumber, sticky ='w', **self.paddings)
self.rowNumber += 1
self.message.set("If yes, please enter the full path of the directory with .xml article files:")
else:
extractionSelected = False
self.rowNumber += 1
self.message.set("No ID_Ex_database.db found. Please enter the full path of the directory with .xml article files:")
ttk.Label(self.root, textvariable = self.message, style="TLabel").\
grid(column=0,row = self.rowNumber, sticky ='w', **self.paddings)
pathEntry = ttk.Entry(self.root, textvariable = self.inputString, style = "TEntry", width = "90").\
grid(column=1, row = self.rowNumber, sticky ='w', **self.paddings)
self.rowNumber += 1
def exit_application(self, database, containerArticles, logParameters):
res = messagebox.askokcancel(title="Exit application", message="Exit application?")
if res == True:
self.write_log(database, containerArticles, logParameters)
self.root.destroy()
def run_extraction(self, database, containerArticles, extraction, logParameters):
if extraction == True:
try:
files_in_folder = os.scandir(self.path)
except:
errMessage = "ERROR: Problem during opening directory " + self.path + ".\nCheck path and restart."
database.logBuffer.append(errMessage)
tk.messagebox.showwarning(title="ERROR", \
message=errMessage)
self.write_log(database, containerArticles, logParameters)
self.root.destroy()
sys.exit(0)
fileNames = []
fileNames.clear()
for fileName in files_in_folder:
if fileName.name.endswith('.xml'):
fileNames.append(fileName.name)
files_in_folder.close()
if len(fileNames) <= 0:
errMessage = "\nERROR: No .xml file found in the current directory. Check entry and restart."
database.logBuffer.append(errMessage)
tk.messagebox.showwarning(title="ERROR", message = errMessage)
self.write_log(database, containerArticles, logParameters)
self.root.destroy()
sys.exit(0)
#-- Now run soup and get IDs
containerArticles.clear()
for fileName in fileNames: #(DOIs are extracted during instantiation)
containerArticles.append(articleIDsClass(self.path, fileName, database, logParameters))
for articleID in containerArticles:
if not articleID.articleRecorded:
articleID.get_zenonIDs()
articleID.get_and_separate_others()
#-- Create db log and update databases
database.update(containerArticles)
def start_process(self, selectedExports, database, containerArticles, logParameters):
calledFunctions = []
calledFunction = ""
#-- Check if an extraction is necessary
if database.dbFromPreviousRunComplete == True:
test1 = list(self.extractionSelected.state())
if len(test1) == 0:
test1.append("Not selected")
if test1[0] != "Not selected":
extraction = True
else:
extraction = False
else:
extraction = True
message="Extraction selected: " + str(extraction) +"\n\n"
database.logBuffer.append(message)
#-- Get the export parameters
i=0
for x, y in database.databases.items():
test2 = list(selectedExports[i].state())
#There are two values: 'selected' and by default 'alternate' (i. e. the black square)
#In the following also the default setting (= the black square) is read as a "yes"
if len(test2) == 0:
test2.append("Not selected")
if test2[0] != "Not selected":
calledFunction = y['dbCall']
calledFunctions.append(calledFunction)
message="Following export function(s) selected: " + y['dbDescription'] +"\n"
database.logBuffer.append(message)
i += 1
self.path = self.inputString.get()
self.run_extraction(database, containerArticles, extraction, logParameters)
#--Write log and export files
for calledFunction in calledFunctions:
try:
database.show_and_export_records(calledFunction)
except:
errMessage = "ERROR: Cannot acces database."
tk.messagebox.showwarning(title="ERROR", message = errMessage)
database.logBuffer.append(errMessage)
self.write_log(database, containerArticles, logParameters)
self.root.destroy()
sys.exit(0)
self.write_log(database, containerArticles, logParameters)
#--Prepare for the next run
self.inputString.set(None) #Clear the input box for next run
self.actualize_head(database)
self.chooseExtraction.set("0") #Clear radiobutton for next run
self.message.set("Please enter the full path of the directory with .xml article files:")
def write_log(self, database, containerArticles, logParameters):
fileNameLog, fileNameExport = logParameters.actualize_logParameters()
operatingSystem = platform.system()
cwd = os.getcwd()
exists = os.path.exists("_ID_Ex_LOG")
if not exists:
os.makedirs("_ID_Ex_LOG")
if operatingSystem == "Windows":
logPath = cwd+"\\"+"_ID_Ex_LOG"+"\\"
if operatingSystem == "Linux":
logPath = cwd+'/'+"_ID_Ex_LOG"+'/'
#-- First the log
try:
log = open(logPath+fileNameLog, "w")
except:
print("ERROR: Could not write log. Press enter to quit")
input()
sys.exit(0)
log.write(f"ID_EXTRACTOR {logParameters.ID_X_version} LOG FILE\n\n")
logParameters.insert_log_timestamp(log)
for dbLog in database.logBuffer:
log.write(dbLog)
log.write("\n\n")
for containerArticle in containerArticles:
log.write(f"Path of extracted repository: {containerArticle.path}\n")
log.write(f"File name: {containerArticle.fileName} with {containerArticle.articleDOI}\n")
for entry in containerArticle.logBuffer:
log.write(f"{entry}\n")
log.write("\n")
log.close()
#-- Now the db export file
if len(database.queryResults) > 0:
try:
log = open(logPath+fileNameExport, "w")
except:
print("ERROR: Could not write export file. Press enter to quit")
input()
sys.exit(0)
log.write(f"ID_EXTRACTOR {logParameters.ID_X_version} EXPORT FILE\n\n")
logParameters.insert_log_timestamp(log)
for queryResult in database.queryResults:
log.write(queryResult)
log.close()
tk.messagebox.showinfo(title="Information", message="Log file written successfully.")
#-- Reset
actualTime = time.localtime()
database.reset_for_next_run()
containerArticles.clear()