Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #import MySQLdb will add this later
- import pysftp, os
- from Tkinter import *
- ##FIXES--------------------------------------------------------
- if os.name == 'nt': #windows fix
- import win32api, win32con
- if getattr(sys, 'frozen', False): #windows path fix
- os.chdir(sys._MEIPASS)
- exe_path = os.path.dirname(sys.executable)
- elif __file__:
- exe_path = os.path.dirname(__file__)
- def retag(tag, *args): #for bind_class functions for multiple objects bound to same class
- for widget in args:
- widget.bindtags((tag,) + widget.bindtags())
- def no(): #fix for cButton function, used as a default command
- pass
- #--------------------------------------------------------------
- class GUI:
- def __init__(self):
- #self.sql = SQL("address", "user", "password", "database")
- #self.sftp = SFTP("address", "user", "password")
- self.root = Tk() ##initilise the Tkinter GUI framework
- self.quit = False
- def run(self): ##this is where the GUI is built.
- self.root.protocol("WM_DELETE_WINDOW", lambda e=Event() : self.quitting(e))
- self.root.title("Clients")
- self.root.bind("<Destroy>", lambda e=Event() :self.quitting(e))
- window = self.cFrame(self.root, expand=1, fill=BOTH, bg="white")
- defalt_option = StringVar(window)
- defalt_option.set("Barclays") # initial value
- option = OptionMenu(window, defalt_option, "Barclays", "BNY", "CompanyX", "CompanyY")
- option.pack()
- Read = self.cButton(window, text="Read", side=LEFT, command=lambda e=Event(), name=defalt_option: self.get_client_data(e, name))
- Delete = self.cButton(window, text="Delete", side=LEFT, command=lambda e=Event(), name=defalt_option: self.delete_client_data(e, name))
- padder = self.cFrame(window, side=TOP, fill=Y)
- self.result = Label(window, text="test", bg="#000000", fg="#ffffff")
- self.result.pack(side=BOTTOM, fill=X, expand=1)
- bottom = self.cFrame(self.root, expand=1, fill=BOTH, bg="white")
- name_L = Label(bottom, text="Name:")
- name_L.pack(side=LEFT)
- name = Entry(bottom)
- name.pack(side=LEFT)
- path_L = Label(bottom, text="Path:")
- path_L.pack(side=LEFT)
- path = Entry(bottom)
- path.pack(side=LEFT)
- Add = self.cButton(bottom, text="Add", side=LEFT, command=lambda e=Event(), n=name, p=path: self.add_client_data(e, n, p))
- while not self.quit:
- self.root.update_idletasks()
- self.root.update()
- def get_client_data(self, event, name):
- print("getting client data for %s" %name.get())
- def delete_client_data(self, event, name):
- print("removing client data for %s" %name.get())
- def add_client_data(self, event, name, path):
- print("adding client %s with path %s" %(name.get(), path.get()))
- def quitting(self, event): ##override for the quitting options to also close all threads
- try:
- print("application quitting")
- self.quit = True
- self.root.destroy()
- self.root.quit()
- except Exception as err:
- print(err)
- def set_quit(self): #for keyboard interrupt to quit all running threads
- self.quit=True
- self.quitting(Event())
- 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
- try:
- b = Button(element, borderwidth=borderwidth, fg=fg, image=image, bg=bg, text=text, relief=relief, command=command, padx=padx, pady=pady)
- b.pack(side=side, expand=expand, fill=fill)
- return b
- except Exception as err:
- exc_type, exc_obj, exc_tb = sys.exc_info()
- self.log.error("cButton failed\n%s, %s, %s, %s" %(err, exc_type, exc_obj, traceback.print_tb(exc_tb)))
- 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
- try:
- f = Frame(element, bg=bg, borderwidth=borderwidth, relief=relief, height=height, width=width, image=image)
- f.pack(side=side, padx=padx, pady=pady, expand=expand, fill=fill)
- return f
- except Exception as err:
- exc_type, exc_obj, exc_tb = sys.exc_info()
- self.log.error("cFrame failed\n%s, %s, %s, %s" %(err, exc_type, exc_obj, traceback.print_tb(exc_tb)))
- class SFTP: #sftp controls
- def __init__(self, sftp_address, sftp_user, sftp_pass):
- self.quit = False
- try:
- self.log.log("looking for known hosts at: "+exe_path+'/__known_hosts__')
- cnopts = pysftp.CnOpts(exe_path+'/__known_hosts__')
- self.sftp = pysftp.Connection(sftp_address, username=sftp_user, password=sftp_pass, cnopts=cnopts)
- self.sftp_busy = False
- t=threading.Thread(target=self.keep_alive)
- t.start()
- except Exception as err:
- print(err)
- def keep_alive(self): #keeps sftp alive and connected when idle
- try:
- while True:
- if self.quit:
- break
- while self.sftp_busy:
- time.sleep(0.1)
- self.sftp_busy = True
- self.sftp.listdir("/DataVolume/shares/Public/encrypt/main/")
- self.sftp_busy = False
- i = 0
- while i <= 120:
- if self.quit:
- break
- time.sleep(1)
- i+=1
- except Exception as err:
- self.sftp_busy = False
- print(err)
- self.keep_alive()
- def upload(self, file, location): #downloads using sftp
- try:
- self.log.log("downloading %s to %s" %(file, path))
- while self.sftp_busy:
- time.sleep(0.1)
- self.sftp_busy = True
- self.sftp.put(file, location)
- self.sftp_busy = False
- except Exception as err:
- self.sftp_busy = False
- print(err)
- def download(self, file, location): #downloads using sftp
- try:
- self.log.log("downloading %s to %s" %(file, path))
- while self.sftp_busy:
- time.sleep(0.1)
- self.sftp_busy = True
- self.sftp.get(file, location)
- self.sftp_busy = False
- except Exception as err:
- self.sftp_busy = False
- print(err)
- class SQL: #sql based functions (ignore this for now)
- def __init__(self, db_address, db_user, db_pass, db_database):
- try:
- self.db_address = db_address
- self.db_user = db_user
- self.db_pass = db_pass
- self.db_database = db_database
- self.DB = self.create_connection()
- self.DB.autocommit(True)
- self.sql_busy = False
- except Exception as err:
- print(err)
- def create_connection(self): #simple connection handler
- return MySQLdb.connect(host=self.db_address, user=self.db_user, passwd=self.db_pass, db=self.db_database)
- def add_client(self, name, path):
- while self.sql_busy:
- time.sleep(0.25)
- self.sql_busy = True
- cur = self.DB.cursor()
- cur.execute("INSERT INTO clients (client, path) VALUES ('%s', '%s');" %(name, path))
- self.DB.commit()
- self.sql_busy = False
- def read_client(self, name):
- while self.sql_busy:
- time.sleep(0.25)
- self.sql_busy = True
- cur = self.DB.cursor()
- cur.execute("SELECT * FROM clients where client = '%s' LIMIT 1;" %name)
- data = cur.fetchall()
- self.sql_busy = False
- data = data[0]
- return data
- def delete_client(self, name):
- while self.sql_busy:
- time.sleep(0.25)
- self.sql_busy = True
- cur = self.DB.cursor()
- cur.execute("DELETE FROM clients where client = '%s' LIMIT 1;" %name)
- self.DB.commit()
- self.sql_busy = False
- if __name__ == "__main__":
- app = GUI()
- try:
- app.run()
- except Exception as err:
- print(err)
- app.set_quit()
- except KeyboardInterrupt:
- app.set_quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement