Skip to content

Commit 1337fb0

Browse files
committed
Fixed logging and improved error handling.
* Improved error handling A LOT. * Pluggy should no longer crash
1 parent 6b197f6 commit 1337fb0

File tree

1 file changed

+26
-23
lines changed

1 file changed

+26
-23
lines changed

pluggy.py

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from PIL import Image, ImageTk
1111
import re
1212
import csv
13-
import os
1413

1514
top = Tk()
1615
user_vars = {}
@@ -37,6 +36,10 @@ def update_vars(dat):
3736
available.set("Available fields:\n" + fields)
3837

3938

39+
def fix_file_ext(fname, ext):
40+
return fname if "\." in fname else fname + ext
41+
42+
4043
def all_indices(string, sub, offset=0):
4144
listindex = []
4245
i = string.find(sub, offset)
@@ -51,15 +54,14 @@ def load_csv():
5154
filetypes=(("CSV files", "*.csv"), ("All files", "*.*")))
5255
if fname == "":
5356
return
54-
loadstatus.set("Loading " + os.path.basename(fname))
57+
loadstatus.set("Loading CSV")
5558
global csvdat
5659
csvdat = []
5760
with open(fname, encoding="utf-8") as csvfile:
5861
reader = csv.DictReader(csvfile)
5962
try:
6063
rfieldnames = [asciify(fieldname).replace(" ", "_") for fieldname in reader.fieldnames]
6164
except UnicodeDecodeError as e:
62-
print(repr(e))
6365
messagebox.showerror("Parsing Error",
6466
"Unable to parse csv file due to unexpected unicode characters in column names. " +
6567
"Consider renaming your column names and trying agin. " +
@@ -71,15 +73,15 @@ def load_csv():
7173
for row in reader:
7274
row = {key.replace(" ", "_"): row[key] for key in row.keys()}
7375
csvdat.append(row)
74-
loadstatus.set(os.path.basename(fname) + " loaded.")
76+
loadstatus.set("CSV loaded.")
7577

7678

7779
def export_csv():
7880
fname = filedialog.asksaveasfilename(initialdir=".", title="Where should the file be saved?",
7981
filetypes=(("CSV files", "*.csv"), ("All files", "*.*")))
8082
if fname == "":
8183
return
82-
with open(fname, 'w', encoding="utf-8", newline='') as csvfile:
84+
with open(fix_file_ext(fname, ".csv"), 'w', encoding="utf-8", newline='') as csvfile:
8385
writer = csv.DictWriter(csvfile, fieldnames=csvdat[0].keys())
8486
writer.writeheader()
8587
writer.writerows(csvdat)
@@ -91,7 +93,7 @@ def query_selector(css_selector, wait_time, friendly=False):
9193
except exceptions.TimeoutException:
9294
if friendly:
9395
return None
94-
raise LookupError
96+
raise exceptions.InvalidSelectorException(css_selector)
9597

9698

9799
def pop_a_window():
@@ -157,14 +159,14 @@ def parse_command(s, rownum):
157159
user_vars[tokens[1]] = s[quotes[0] + 1:quotes[1]]
158160
return
159161
if tokens[0] == 'get':
160-
elem = query_selector(user_vars[tokens[2]], 1, friendly=True)
162+
elem = query_selector(user_vars[tokens[2]], 3, friendly=True)
161163
if elem is None:
162164
user_vars[tokens[1]] = ""
163165
return
164166
if elem.tag_name == 'input' or elem.tag_name == 'textarea':
165167
user_vars[tokens[1]] = elem.get_attribute("value")
166168
else:
167-
user_vars[tokens[1]] = elem.get_attribute("text")
169+
user_vars[tokens[1]] = elem.text
168170
return
169171
if tokens[0] == 'paste' and len(tokens) == 4:
170172
user_vars[tokens[1]] = user_vars[tokens[2]] + user_vars[tokens[3]]
@@ -181,16 +183,11 @@ def parse_command(s, rownum):
181183
if ok is None:
182184
raise TypeError("Abort mission")
183185
if not ok:
184-
raise LookupError
186+
raise LookupError()
185187
return
186-
except exceptions.InvalidSelectorException:
187-
print("Selector error")
188-
raise exceptions.InvalidSelectorException("hm")
189-
except (exceptions.NoSuchWindowException, exceptions.WebDriverException, AttributeError) as e:
188+
except (exceptions.NoSuchWindowException, AttributeError) as e:
190189
driver.quit()
191190
driver = None
192-
print(e)
193-
print("Value error" + repr(e))
194191
raise ValueError("Some webdriver related exception occurred.")
195192

196193

@@ -223,11 +220,13 @@ def run_program(tb, mainwindow):
223220
" Remember any spaces in variable names are replaced with underscores")
224221
return
225222
except IndexError:
226-
messagebox.showerror("Program Error", "Problem reading set command. Check your quotes.")
223+
messagebox.showerror("Program Error", "Syntax error. Make sure all commands have the right amount of args" +
224+
" and that you're only using single quotes.")
227225
break
228-
except exceptions.InvalidSelectorException:
229-
messagebox.showerror("Program Error", "Invalid css selector used. " +
230-
"Make sure you're using the right selectors and check your click commands.")
226+
except exceptions.InvalidSelectorException as ise:
227+
messagebox.showerror("Program Error", "Unable to find selector '" +
228+
str(ise)[str(ise).find(':') + 2:].strip() +
229+
"'. Make sure you're using the right selectors and check your click commands.")
231230
return
232231
except LookupError:
233232
continue
@@ -267,13 +266,17 @@ def test_program(tb, mainwindow):
267266
+ " not found. Check your spelling and capitalization. "
268267
+ "Remember any spaces in variable names are replaced with underscores")
269268
except IndexError:
270-
messagebox.showerror("Program Error", "Problem reading set command. Check your quotes.")
271-
except exceptions.InvalidSelectorException:
269+
messagebox.showerror("Program Error", "Syntax error. Make sure all commands have the right amount of args" +
270+
" and that you're only using single quotes.")
271+
return
272+
except exceptions.InvalidSelectorException as ise:
273+
messagebox.showerror("Program Error", "Unable to find selector '" +
274+
str(ise)[str(ise).find(':') + 2:].strip() +
275+
"'. Make sure you're using the right selectors and check your click commands.")
272276
return
273277
except LookupError:
274278
return
275279
except TypeError:
276-
print("YOU ENDED ME")
277280
return
278281

279282

@@ -293,7 +296,7 @@ def save_program(tb):
293296
filetypes=(("Pluggy program", "*.plgy"), ("All files", "*.*")))
294297
if fname == "":
295298
return
296-
with open(fname if ".plgy" in fname else fname + ".plgy", "w") as fopen:
299+
with open(fix_file_ext(fname, ".plgy"), "w") as fopen:
297300
text2save = str(tb.get("1.0", END))
298301
fopen.write(text2save)
299302

0 commit comments

Comments
 (0)