1+ from csv import DictWriter , DictReader
2+ import re
3+ from tkinter import LEFT , N , S , E , W , END
4+ from tkinter import Tk , StringVar , Text , Button , Scrollbar , Frame , Label
15from tkinter import filedialog
2- from tkinter import *
3- from tkinter import messagebox
6+ from tkinter .messagebox import askyesno , askokcancel , askyesnocancel
7+ from tkinter .messagebox import showerror , showwarning
8+
9+ from PIL import Image , ImageTk
410from selenium import webdriver
5- from selenium .webdriver .common .keys import Keys
6- from selenium .common import exceptions
11+ from selenium .common .exceptions import InvalidSelectorException , TimeoutException , NoSuchWindowException
712from selenium .webdriver .common .by import By
13+ from selenium .webdriver .common .keys import Keys
14+ from selenium .webdriver .support import expected_conditions as ec
815from selenium .webdriver .support .ui import WebDriverWait
9- from selenium .webdriver .support import expected_conditions as EC
10- from PIL import Image , ImageTk
11- import re
12- import csv
1316
1417top = Tk ()
1518user_vars = {}
2427paused = False
2528cliks = 0
2629logging = False
30+ running = True
2731
2832
2933def asciify (s ):
@@ -37,7 +41,10 @@ def update_vars(dat):
3741
3842
3943def fix_file_ext (fname , ext ):
40- return fname if "." in fname else fname + ext
44+ foundext = re .search ("[^\/]+\.[^\/]*$" , fname )
45+ o = fname if foundext is not None else fname + ext
46+ print (fname , ":" , o )
47+ return o
4148
4249
4350def all_indices (string , sub , offset = 0 ):
@@ -58,14 +65,14 @@ def load_csv():
5865 global csvdat
5966 csvdat = []
6067 with open (fname , encoding = "utf-8" ) as csvfile :
61- reader = csv . DictReader (csvfile )
68+ reader = DictReader (csvfile )
6269 try :
6370 rfieldnames = [asciify (fieldname ).replace (" " , "_" ) for fieldname in reader .fieldnames ]
6471 except UnicodeDecodeError as e :
65- messagebox . showerror ("Parsing Error" ,
66- "Unable to parse csv file due to unexpected unicode characters in column names. " +
67- "Consider renaming your column names and trying agin. " +
68- "If the problem persists, file an issue on Github" )
72+ showerror ("Parsing Error" ,
73+ "Unable to parse csv file due to unexpected unicode characters in column names. " +
74+ "Consider renaming your column names and trying agin. " +
75+ "If the problem persists, file an issue on Github" )
6976 loadstatus .set ("Import a CSV" )
7077 available .set ("" )
7178 return
@@ -82,18 +89,18 @@ def export_csv():
8289 if fname == "" :
8390 return
8491 with open (fix_file_ext (fname , ".csv" ), 'w' , encoding = "utf-8" , newline = '' ) as csvfile :
85- writer = csv . DictWriter (csvfile , fieldnames = csvdat [0 ].keys ())
92+ writer = DictWriter (csvfile , fieldnames = csvdat [0 ].keys ())
8693 writer .writeheader ()
8794 writer .writerows (csvdat )
8895
8996
9097def query_selector (css_selector , wait_time , friendly = False ):
9198 try :
92- return WebDriverWait (driver , wait_time ).until (EC .presence_of_element_located ((By .CSS_SELECTOR , css_selector )))
93- except exceptions . TimeoutException :
99+ return WebDriverWait (driver , wait_time ).until (ec .presence_of_element_located ((By .CSS_SELECTOR , css_selector )))
100+ except TimeoutException :
94101 if friendly :
95102 return None
96- raise exceptions . InvalidSelectorException (css_selector )
103+ raise InvalidSelectorException (css_selector )
97104
98105
99106def pop_a_window ():
@@ -102,8 +109,8 @@ def pop_a_window():
102109 driver = webdriver .Chrome ()
103110 driver .get ("https://web.tabliss.io/" )
104111 else :
105- openanyway = messagebox . askokcancel ("Pluggy" ,
106- "Only one webdriver can be used at a time. Are you sure you want to open another one?" )
112+ openanyway = askokcancel ("Pluggy" ,
113+ "Only one webdriver can be used at a time. Are you sure you want to open another one?" )
107114 if openanyway :
108115 driver = webdriver .Chrome ()
109116 driver .get ("https://web.tabliss.io/" )
@@ -118,6 +125,11 @@ def expand_boi(s):
118125 return s
119126
120127
128+ def end_program ():
129+ global running
130+ running = False
131+
132+
121133def parse_command (s , rownum ):
122134 global driver
123135 tokens = s .split ()
@@ -179,100 +191,107 @@ def parse_command(s, rownum):
179191 user_vars [tokens [1 ]] = user_vars [tokens [2 ]][tokens [3 ]:tokens [4 ]]
180192 return
181193 if tokens [0 ] == 'confirm' :
182- ok = messagebox . askyesnocancel ("Confirmation" , "Does this look right?" )
194+ ok = askyesnocancel ("Confirmation" , "Does this look right?" )
183195 if ok is None :
184196 raise TypeError ("Abort mission" )
185197 if not ok :
186198 raise LookupError ()
187199 return
188- except (exceptions . NoSuchWindowException , AttributeError ) as e :
200+ except (NoSuchWindowException , AttributeError ):
189201 driver .quit ()
190202 driver = None
191203 raise ValueError ("Some webdriver related exception occurred." )
204+ except IndexError :
205+ raise IndexError ("Error in command: '" + s + "'. " )
192206
193207
194208def run_program (tb , mainwindow ):
195- global driver
209+ global running , logging
210+ running = True
196211 if driver is None :
197- messagebox . showerror ("No open browser" ,
198- "Make sure you have a browser open before you run the script." )
212+ showerror ("No open browser" ,
213+ "Make sure you have a browser open before you run the script." )
199214 return
200215 program = tb .get ("1.0" , 'end-1c' ).split ("\n " )
201216 global user_vars
202217 user_vars = {}
203218 if len (csvdat ) == 0 :
204- messagebox . showerror ("No CSV loaded" , "Be sure to load some data before you try to run your program." )
219+ showerror ("No CSV loaded" , "Be sure to load some data before you try to run your program." )
205220 return
206221 for rownum in range (len (csvdat )):
207222 row = csvdat [rownum ]
208223 try :
209224 user_vars = {k : row [k ] if k in row .keys () else user_vars [k ] for k in user_vars .keys () | row .keys ()}
210225 for line in program :
226+ if not running :
227+ return
211228 mainwindow .update ()
212229 parse_command (line .strip (), rownum )
213230 except ValueError :
214- messagebox . showwarning ("Browser Session Disconnected" , "Your browser session was disconnected" )
231+ showwarning ("Browser Session Disconnected" , "Your browser session was disconnected" )
215232 return
216233 except KeyError as e :
217- messagebox . showerror ("Program Error" , "Variable "
218- + str (e )
219- + " not found. Check your spelling and capitalization." +
220- " Remember any spaces in variable names are replaced with underscores" )
234+ showerror ("Program Error" , "Variable "
235+ + str (e )
236+ + " not found. Check your spelling and capitalization." +
237+ " Remember any spaces in variable names are replaced with underscores" )
221238 return
222239 except IndexError :
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." )
240+ showerror ("Program Error" , "Syntax error. Make sure all commands have the right amount of args" +
241+ " and that you're only using single quotes." )
225242 break
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." )
243+ except InvalidSelectorException as ise :
244+ showerror ("Program Error" , "Unable to find selector '" +
245+ str (ise )[str (ise ).find (':' ) + 2 :].strip () +
246+ "'. Make sure you're using the right selectors and check your click commands." )
230247 return
231248 except LookupError :
232249 continue
233250 except TypeError :
234251 return
235252 if logging :
236253 export_csv ()
254+ logging = False
237255
238256
239257def test_program (tb , mainwindow ):
240- global driver
241- global csvdat
242-
258+ global running
259+ running = True
243260 if driver is None :
244- messagebox . showerror ("No open browser" ,
245- "Make sure you have a browser open before you run the script." )
261+ showerror ("No open browser" ,
262+ "Make sure you have a browser open before you run the script." )
246263 return
247264 program = tb .get ("1.0" , 'end-1c' ).split ("\n " )
248265 global user_vars
249266 user_vars = {}
250267 if len (csvdat ) > 0 :
251268 row = csvdat [0 ]
252269 else :
253- messagebox . showerror ("No CSV loaded" , "Be sure to load some data before you try to run your program." )
270+ showerror ("No CSV loaded" , "Be sure to load some data before you try to run your program." )
254271 return
255272 try :
256273 user_vars = {k : row [k ] if k in row .keys () else user_vars [k ] for k in user_vars .keys () | row .keys ()}
257274 for line in program :
275+ if not running :
276+ return
258277 mainwindow .update ()
259278 parse_command (line .strip (), 0 )
260279 update_vars (user_vars .keys ())
261280 except ValueError :
262- messagebox . showwarning ("Browser Session Disconnected" , "Your browser session was disconnected" )
281+ showwarning ("Browser Session Disconnected" , "Your browser session was disconnected" )
263282 except KeyError as e :
264- messagebox . showerror ("Program Error" , "Variable "
265- + str (e )
266- + " not found. Check your spelling and capitalization. "
267- + "Remember any spaces in variable names are replaced with underscores" )
268- except IndexError :
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." )
283+ showerror ("Program Error" , "Variable "
284+ + str (e )
285+ + " not found. Check your spelling and capitalization. "
286+ + "Remember any spaces in variable names are replaced with underscores" )
287+ except IndexError as ie :
288+ showerror ("Program Error" , str ( ie ) + " Make sure all commands have the right amount of args" +
289+ " and that you're only using single quotes." )
271290 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." )
291+ except InvalidSelectorException as ise :
292+ showerror ("Program Error" , "Unable to find selector '" +
293+ str (ise )[str (ise ).find (':' ) + 2 :].strip () +
294+ "'. Make sure you're using the right selectors and check your click commands." )
276295 return
277296 except LookupError :
278297 return
@@ -301,25 +320,21 @@ def save_program(tb):
301320 fopen .write (text2save )
302321
303322
304- def end_program ():
305- raise TypeError ("problem" )
306-
307-
308323def trap (e ):
309324 global cliks
310325 cliks += 1
311326 if cliks > 1 :
312- v = messagebox . askyesno ("Proceed with caution" , "Are you the developer?" )
327+ v = askyesno ("Proceed with caution" , "Are you the developer?" )
313328 if v :
314329 for i in range (20 ):
315- messagebox . showerror ("Big mistake" , "You have activated my trap card" )
330+ showerror ("Big mistake" , "You have activated my trap card" )
316331 cliks = 0
317332
318333
334+ # Setting up Pluggy's GUI
319335top .title ("Pluggy" )
320336top .iconbitmap ("pluggy.ico" )
321337
322- # Code to add widgets will go here...
323338textarea = Text (top , height = 6 , width = 30 )
324339scrollbar = Scrollbar (top , command = textarea .yview )
325340scrollbar .place (in_ = textarea , relx = 1.0 , relheight = 1.0 , bordermode = "outside" )
0 commit comments