Advertisement
AceScottie

company editor

Jan 18th, 2019
434
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.16 KB | None | 0 0
  1. #import MySQLdb will add this later
  2. import pysftp, os
  3. from Tkinter import *
  4.  
  5. ##FIXES--------------------------------------------------------
  6. if os.name == 'nt': #windows fix
  7.     import win32api, win32con
  8. if getattr(sys, 'frozen', False): #windows path fix
  9.     os.chdir(sys._MEIPASS)
  10.     exe_path = os.path.dirname(sys.executable)
  11. elif __file__:
  12.     exe_path = os.path.dirname(__file__)
  13. def retag(tag, *args): #for bind_class functions for multiple objects bound to same class
  14.     for widget in args:
  15.         widget.bindtags((tag,) + widget.bindtags())
  16. def no(): #fix for cButton function, used as a default command
  17.     pass
  18. #--------------------------------------------------------------
  19.  
  20.  
  21. class GUI:
  22.     def __init__(self):
  23.         #self.sql = SQL("address", "user", "password", "database")
  24.         #self.sftp = SFTP("address", "user", "password")
  25.         self.root = Tk() ##initilise the Tkinter GUI framework
  26.         self.quit = False
  27.    
  28.     def run(self): ##this is where the GUI is built.
  29.         self.root.protocol("WM_DELETE_WINDOW", lambda e=Event() : self.quitting(e))
  30.         self.root.title("Clients")
  31.         self.root.bind("<Destroy>", lambda e=Event() :self.quitting(e))
  32.         window = self.cFrame(self.root, expand=1, fill=BOTH, bg="white")
  33.        
  34.         defalt_option = StringVar(window)
  35.         defalt_option.set("Barclays") # initial value
  36.         option = OptionMenu(window, defalt_option, "Barclays", "BNY", "CompanyX", "CompanyY")
  37.         option.pack()
  38.        
  39.         Read = self.cButton(window, text="Read", side=LEFT, command=lambda e=Event(), name=defalt_option: self.get_client_data(e, name))
  40.         Delete = self.cButton(window, text="Delete", side=LEFT, command=lambda e=Event(), name=defalt_option: self.delete_client_data(e, name))
  41.         padder = self.cFrame(window, side=TOP, fill=Y)
  42.         self.result = Label(window, text="test", bg="#000000", fg="#ffffff")
  43.         self.result.pack(side=BOTTOM, fill=X, expand=1)
  44.         bottom = self.cFrame(self.root, expand=1, fill=BOTH, bg="white")
  45.         name_L = Label(bottom, text="Name:")
  46.         name_L.pack(side=LEFT)
  47.         name = Entry(bottom)
  48.         name.pack(side=LEFT)
  49.         path_L = Label(bottom, text="Path:")
  50.         path_L.pack(side=LEFT)
  51.         path = Entry(bottom)
  52.         path.pack(side=LEFT)
  53.         Add = self.cButton(bottom, text="Add", side=LEFT, command=lambda e=Event(), n=name, p=path: self.add_client_data(e, n, p))
  54.         while not self.quit:
  55.                     self.root.update_idletasks()
  56.                     self.root.update()
  57.        
  58.     def get_client_data(self, event, name):
  59.         print("getting client data for %s" %name.get())
  60.    
  61.     def delete_client_data(self, event, name):
  62.         print("removing client data for %s" %name.get())
  63.        
  64.     def add_client_data(self, event, name, path):
  65.         print("adding client %s with path %s" %(name.get(), path.get()))
  66.        
  67.     def quitting(self, event): ##override for the quitting options to also close all threads
  68.         try:
  69.             print("application quitting")
  70.             self.quit = True
  71.             self.root.destroy()
  72.             self.root.quit()
  73.         except Exception as err:
  74.             print(err)
  75.     def set_quit(self): #for keyboard interrupt to quit all running threads
  76.         self.quit=True
  77.         self.quitting(Event())
  78.        
  79.     def cButton(self, element, borderwidth=2, padx=0, pady=0, image=None, fg="black", bg="#eeeeee", text="", relief="groove", command=no(), side=TOP, expand=0, fill=None): #basic button
  80.         try:
  81.             b = Button(element, borderwidth=borderwidth, fg=fg, image=image, bg=bg, text=text, relief=relief, command=command, padx=padx, pady=pady)
  82.             b.pack(side=side, expand=expand, fill=fill)
  83.             return b
  84.         except Exception as err:
  85.             exc_type, exc_obj, exc_tb = sys.exc_info()
  86.             self.log.error("cButton failed\n%s, %s, %s, %s" %(err, exc_type, exc_obj, traceback.print_tb(exc_tb)))     
  87.     def cFrame(self, element, bg="white", borderwidth=0, relief="groove", side=TOP, padx=0, pady=0, height=0, width=0, expand=0, fill=None, image=None):#basic frame
  88.         try:
  89.             f = Frame(element, bg=bg, borderwidth=borderwidth, relief=relief, height=height, width=width, image=image)
  90.             f.pack(side=side, padx=padx, pady=pady, expand=expand, fill=fill)
  91.             return f
  92.         except Exception as err:
  93.             exc_type, exc_obj, exc_tb = sys.exc_info()
  94.             self.log.error("cFrame failed\n%s, %s, %s, %s" %(err, exc_type, exc_obj, traceback.print_tb(exc_tb)))
  95.        
  96. class SFTP: #sftp controls
  97.     def __init__(self, sftp_address, sftp_user, sftp_pass):
  98.         self.quit = False
  99.         try:
  100.             self.log.log("looking for known hosts at: "+exe_path+'/__known_hosts__')
  101.             cnopts = pysftp.CnOpts(exe_path+'/__known_hosts__')
  102.             self.sftp =  pysftp.Connection(sftp_address, username=sftp_user, password=sftp_pass, cnopts=cnopts)
  103.             self.sftp_busy = False
  104.             t=threading.Thread(target=self.keep_alive)
  105.             t.start()
  106.         except Exception as err:
  107.             print(err)
  108.            
  109.     def keep_alive(self): #keeps sftp alive and connected when idle
  110.         try:
  111.             while True:
  112.                 if self.quit:
  113.                     break
  114.                 while self.sftp_busy:
  115.                     time.sleep(0.1)
  116.                 self.sftp_busy = True
  117.                 self.sftp.listdir("/DataVolume/shares/Public/encrypt/main/")
  118.                 self.sftp_busy = False
  119.                 i = 0
  120.                 while i <= 120:
  121.                     if self.quit:
  122.                         break
  123.                     time.sleep(1)
  124.                     i+=1
  125.         except Exception as err:
  126.             self.sftp_busy = False
  127.             print(err)
  128.             self.keep_alive()
  129.     def upload(self, file, location): #downloads using sftp
  130.         try:
  131.             self.log.log("downloading %s to %s" %(file, path))
  132.             while self.sftp_busy:
  133.                 time.sleep(0.1)
  134.             self.sftp_busy = True
  135.             self.sftp.put(file, location)
  136.             self.sftp_busy = False
  137.         except Exception as err:
  138.             self.sftp_busy = False
  139.             print(err)
  140.     def download(self, file, location): #downloads using sftp
  141.         try:
  142.             self.log.log("downloading %s to %s" %(file, path))
  143.             while self.sftp_busy:
  144.                 time.sleep(0.1)
  145.             self.sftp_busy = True
  146.             self.sftp.get(file, location)
  147.             self.sftp_busy = False
  148.         except Exception as err:
  149.             self.sftp_busy = False
  150.             print(err)
  151.            
  152. class SQL: #sql based functions (ignore this for now)
  153.     def __init__(self, db_address, db_user, db_pass, db_database):
  154.         try:
  155.             self.db_address = db_address
  156.             self.db_user = db_user
  157.             self.db_pass = db_pass
  158.             self.db_database = db_database
  159.             self.DB = self.create_connection()
  160.             self.DB.autocommit(True)
  161.             self.sql_busy = False
  162.         except Exception as err:
  163.             print(err)
  164.     def create_connection(self): #simple connection handler
  165.         return MySQLdb.connect(host=self.db_address, user=self.db_user, passwd=self.db_pass, db=self.db_database)
  166.     def add_client(self, name, path):
  167.         while self.sql_busy:
  168.             time.sleep(0.25)
  169.         self.sql_busy = True
  170.         cur = self.DB.cursor()
  171.         cur.execute("INSERT INTO clients (client, path) VALUES ('%s', '%s');" %(name, path))
  172.         self.DB.commit()
  173.         self.sql_busy = False
  174.     def read_client(self, name):
  175.         while self.sql_busy:
  176.             time.sleep(0.25)
  177.         self.sql_busy = True
  178.         cur = self.DB.cursor()
  179.         cur.execute("SELECT * FROM clients where client = '%s' LIMIT 1;" %name)
  180.         data = cur.fetchall()
  181.         self.sql_busy = False
  182.         data = data[0]
  183.         return data
  184.     def delete_client(self, name):
  185.         while self.sql_busy:
  186.             time.sleep(0.25)
  187.         self.sql_busy = True
  188.         cur = self.DB.cursor()
  189.         cur.execute("DELETE FROM clients where client = '%s' LIMIT 1;" %name)
  190.         self.DB.commit()
  191.         self.sql_busy = False
  192.        
  193.        
  194. if __name__ == "__main__":
  195.     app = GUI()
  196.     try:
  197.         app.run()
  198.     except Exception as err:
  199.         print(err)
  200.         app.set_quit()
  201.     except KeyboardInterrupt:
  202.         app.set_quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement