#!/usr/bin/python # Get the GUI stuff import wx,time # We're going to be handling files and directories from ftplib import FTP from sys import platform as _platform import os import ftplib #from commonDataStructure import FTPUploadGUIData filePath = "" fileName = "" fileSize = 0 so_far = 0 callback = 0 class MyDialognFTPupload(wx.Dialog): def __init__(self, parent, id, logging,path, title,pos , size): wx.Dialog.__init__(self,parent,wx.ID_ANY,title=title, pos=pos ,size=size) self.log = logging self.log.debug("FTP Upload Dialog get Initialize:") ico = wx.Icon(path + os.sep + 'icon-netapp.ico', wx.BITMAP_TYPE_ICO) self.SetIcon(ico) self.InitUIFTP() self.Centre() def InitUIFTP(self): from wx.lib.scrolledpanel import ScrolledPanel panel = ScrolledPanel(self) panel.SetupScrolling() #panel = wx.Panel(self) self.SetBackgroundColour("LIGHT GREY")#("sky blue") font = wx.SystemSettings_GetFont(wx.SYS_SYSTEM_FONT) font.SetPointSize(9) vbox = wx.BoxSizer(wx.VERTICAL) if _platform == "linux" or _platform == "linux2": emptyLabel = wx.StaticText(panel,label='', size=(9,-1)) hbox = wx.BoxSizer(wx.HORIZONTAL) st1 = wx.StaticText(panel, label='Case Number:', style=wx.ALIGN_RIGHT) #st1.SetFont(font) self.editCaseNo = wx.TextCtrl(panel, id=10, value="", size=(150,-1),style=wx.TE_PROCESS_ENTER) hbox.Add(emptyLabel, flag=wx.RIGHT, border=8) hbox.Add(st1, flag=wx.RIGHT, border=8) hbox.Add(self.editCaseNo, flag=wx.RIGHT, border=8) hbox1 = wx.BoxSizer(wx.HORIZONTAL) st2 = wx.StaticText(panel, label='FTP Server:',style=wx.ALIGN_RIGHT) #st2.SetFont(font) httpList = ['ftp-europe.netapp.com','ftp.netapp.com'] self.cb = wx.ComboBox(panel, id=wx.NewId(),value=httpList[0], size=(200, -1), choices=httpList, style=wx.CB_READONLY) hbox1.Add(emptyLabel, flag=wx.RIGHT, border=25) hbox1.Add(st2, flag=wx.RIGHT, border=8) hbox1.Add(self.cb, flag=wx.RIGHT, border=8) hbox2 = wx.BoxSizer(wx.HORIZONTAL) st1 = wx.StaticText(panel, label='File:' ,style=wx.ALIGN_RIGHT) #st1.SetFont(font) self.editFile = wx.TextCtrl(panel, id=20,value="", size=(270,-1)) btn = wx.Button(panel,1,label='Browse', size=(100, 22)) hbox2.Add(emptyLabel, flag=wx.RIGHT, border=8) hbox2.Add(st1, flag=wx.RIGHT, border=2) hbox2.Add(self.editFile, flag=wx.RIGHT, border=05) hbox2.Add(btn, flag=wx.RIGHT, border=10) hbox3 = wx.BoxSizer(wx.HORIZONTAL) self.count = 0 self.progress = wx.Gauge(panel, 05, size=(430, 20)) self.progress.SetBezelFace(3) self.progress.SetShadowWidth(3) hbox3.Add(emptyLabel, flag=wx.RIGHT, border=8) hbox3.Add(self.progress, 0, wx.EXPAND) else: hbox = wx.BoxSizer(wx.HORIZONTAL) st1 = wx.StaticText(panel, label='Case Number:',size=(150, -1), style=wx.ALIGN_RIGHT) #st1.SetFont(font) self.editCaseNo =wx.TextCtrl(panel, id=10, value="", size=(150, -1), style=wx.TE_PROCESS_ENTER) hbox.Add(st1, flag=wx.RIGHT, border=8) hbox.Add(self.editCaseNo, flag=wx.RIGHT, border=8) hbox1 = wx.BoxSizer(wx.HORIZONTAL) st2 = wx.StaticText(panel, label='FTP Server:',size=(150,-1) ,style=wx.ALIGN_RIGHT) #st2.SetFont(font) httpList = ['ftp-europe.netapp.com','ftp.netapp.com'] self.cb=wx.ComboBox(panel, id=wx.NewId(),value=httpList[0], size=(160, -1), choices=httpList, style=wx.CB_READONLY) hbox1.Add(st2, flag=wx.RIGHT, border=8) hbox1.Add(self.cb, flag=wx.RIGHT, border=8) hbox2 = wx.BoxSizer(wx.HORIZONTAL) st1 = wx.StaticText(panel, label='File:' ,size=(50,-1) ,style=wx.ALIGN_RIGHT) #st1.SetFont(font) self.editFile = wx.TextCtrl(panel, id=20,value="", size=(270,-1)) btn = wx.Button(panel,1,label='Browse', size=(100, 22)) hbox2.Add(st1, flag=wx.RIGHT, border=2) hbox2.Add(self.editFile, flag=wx.RIGHT, border=05) hbox2.Add(btn, flag=wx.RIGHT, border=10) hbox3 = wx.BoxSizer(wx.HORIZONTAL) self.count = 0 self.progress = wx.Gauge(panel,05,size= (430,20)) self.progress.SetBezelFace(3) self.progress.SetShadowWidth(3) hbox3.Add(self.progress, 0, wx.EXPAND) hbox4 = wx.BoxSizer(wx.HORIZONTAL) self.st11 = wx.StaticText(panel, label='',size=(360,-1) ,style=wx.ALIGN_RIGHT) hbox4.Add(self.st11, flag=wx.LEFT|wx.RIGHT, border=10) self.editCaseNo.Bind(wx.EVT_ENTER_WINDOW, self.onHover) self.editCaseNo.Bind(wx.EVT_LEAVE_WINDOW, self.onLeaveHover) self.editCaseNo.Bind(wx.EVT_KILL_FOCUS, self.onFocusOut) self.editFile.Bind(wx.EVT_ENTER_WINDOW, self.onHover) self.editFile.Bind(wx.EVT_LEAVE_WINDOW, self.onLeaveHover) hboxBtns = wx.BoxSizer(wx.HORIZONTAL) btn1 = wx.Button(panel, 2,label='Start', size=(70, 30)) hboxBtns.Add(btn1, flag=wx.LEFT|wx.RIGHT, border=95) btn2 = wx.Button(panel,3, label='Back', size=(70, 30)) hboxBtns.Add(btn2, flag=wx.LEFT|wx.RIGHT, border=50) vbox.Add(hbox, flag=wx.EXPAND|wx.LEFT|wx.RIGHT|wx.TOP, border=5) vbox.Add(hbox1, flag=wx.EXPAND|wx.LEFT|wx.RIGHT|wx.TOP, border=5) vbox.Add(hbox2, flag=wx.EXPAND|wx.LEFT|wx.RIGHT|wx.TOP |wx.BOTTOM,border=15) vbox.Add(hbox3, flag=wx.EXPAND|wx.LEFT|wx.RIGHT|wx.TOP |wx.BOTTOM, border=8) vbox.Add(hbox4, flag=wx.EXPAND|wx.LEFT|wx.RIGHT|wx.TOP, border=5) vbox.Add(hboxBtns, flag=wx.ALIGN_LEFT|wx.RIGHT |wx.TOP |wx.BOTTOM,border=20) self.Bind(wx.EVT_BUTTON, self.OnSearch, id=1) self.Bind(wx.EVT_BUTTON, self.OnStart, id=2) self.Bind(wx.EVT_BUTTON, self.OnBack, id=3) self.Bind(wx.EVT_COMBOBOX, self.EvtOnFTPServerSelect, id=self.cb.GetId()) self.ignoreEvtText = False panel.SetSizer(vbox) def onFocusOut(self,event): self.log.info("Validated the case Number:") size = len(self.editCaseNo.GetValue()) if size is not 10 and size is not 0: self.dia = wx.MessageDialog(self, "Enter 10 digits\n", "Warning ...", wx.OK) if self.dia.ShowModal() ==wx.ID_OK: self.editCaseNo.SetValue(self.editCaseNo.GetValue()) self.editCaseNo.Enable(True) self.editCaseNo.SetFocus() self.editCaseNo.SetInsertionPointEnd() #self.dia.Close() self.dia.Destroy() #return event.Skip() return def OnSearch(self, event): global fileName global filePath self.openFileDialog = wx.FileDialog(self ,"" ,"" ,".","All files (*.*)|*.*|files(*.py)|*.py|Exe(*.exe)|*.exe|Texts(*.txt)|*.txt", \ wx.FD_OPEN | wx.FD_FILE_MUST_EXIST) self.log.info(" Open File %s to upload:" %self.openFileDialog.GetFilename()) if self.openFileDialog.ShowModal() == wx.ID_OK: fileName = self.openFileDialog.GetFilename() filePath = self.openFileDialog.GetPath() self.editFile.SetValue(self.openFileDialog.GetPath()) self.openFileDialog.Destroy() else : self.log.warning(" Error in opening %s File :" %self.openFileDialog.GetFilename() ) #return event.Skip() return def OnStart(self, event): global fileName global filePath global fileSize global callback trans_file = open(filePath) #fileName statinfo = os.stat(filePath) #fileName fileSize = statinfo.st_size self.log.debug("opened File Size=%d:" % fileSize) try: ftp = FTP(self.cb.GetValue()) ftp.set_pasv(False) if not ftp.login("anonymous", "anonymous@netapp.com"): self.log.info("login to ftp") self.dlg = wx.MessageDialog(self, "FTP Login failed...\Try again!", "Notification", wx.YES_NO | wx.ICON_QUESTION) if self.dlg.ShowModal() == wx.ID_YES: self.log.info("FTP Login failed !..") if self.dlg.Destroy(): self.Close() else: self.log.info("successful login to FTP to Upload %s File" %fileName) tempPath = self.editCaseNo.GetValue() + os.sep #os.path.join(self.editCaseNo.GetValue(), fileName) ftp.cwd("to-ntap"); CRLF = ftplib.CRLF self.log.debug("CRLF =%s"%CRLF) while 1: buf = trans_file.readline() self.log.debug("trans_file.readline()=%s"%buf) if not buf: break if buf[-2:] != CRLF: if buf[-1] in CRLF: buf = buf[:-1] buf = buf + CRLF #conn.sendall(buf) if callback: self.wxcallback(buf) f=open(filePath, 'rb') ftp.storbinary("STOR " + tempPath ,f ,callback = self.wxcallback) ftp.quit() f.close() self.log.info("FTP Get Closed") font = wx.Font(14, wx.DECORATIVE, wx.ITALIC, wx.NORMAL) self.st11.SetFont(font) self.st11.SetForegroundColour('blue') self.st11.SetLabel("Data upload is successful...") self.log.debug("Data Upload is successful") time.sleep(3) self.Close() event.Skip() return except Exception, e: exceptionMessage=str(e) dlg = wx.MessageDialog( self, exceptionMessage, " Notification...", wx.OK) if dlg.ShowModal() == wx.ID_OK: self.EndModal(0) event.Skip() return def wxcallback(self, buffer): global so_far global fileSize so_far = so_far+len(buffer) pct = (so_far*100)/fileSize self.progress.SetValue(pct) return def OnBack(self,event): event.Skip() self.EndModal(0) return def onHover(self,event): obj_id=event.GetId() if obj_id == 10: msg = "Please enter the provided UAN number as 10 digit!" elif obj_id == 20: msg = "Please Enter the file name by Click on Browse Button!" else: msg = "" event.GetEventObject().SetToolTipString(msg) event.Skip() return def onLeaveHover(self, event): event.Skip() if event.GetId(): return def EvtOnFTPServerSelect(self, event): self.ignoreEvtText = True currentText = event.GetString() event.Skip() return