-
Notifications
You must be signed in to change notification settings - Fork 19
/
FTPGUI.py
176 lines (138 loc) · 5.32 KB
/
FTPGUI.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# -*- coding: utf-8 -*-
## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
# FTPGUI.py ---
# --------------------------------
# Copyright (c) 2020
# L. CAPOCCHI ([email protected])
# SPE Lab - SISU Group - University of Corsica
# --------------------------------
# Version 1.0 last modified: 20/15/20
## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
#
# GENERAL NOTES AND REMARKS:
#
## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
#
# GLOBAL VARIABLES AND FUNCTIONS
#
## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
from ftplib import FTP, all_errors
import wx
import os
class FTPStatusBar(wx.StatusBar):
def __init__(self, *args, **kw):
super(FTPStatusBar, self).__init__(*args, **kw)
self.SetFieldsCount(2)
self.SetStatusText('Welcome to DEVSimPy server', 0)
self.SetStatusWidths([-5, -2])
self.icon = wx.StaticBitmap(self, wx.NewIdRef(), wx.Bitmap(os.path.join(ICON_PATH_16_16, 'disconnect_network.png')))
self.Bind(wx.EVT_SIZE, self.OnSize)
self.PlaceIcon()
def PlaceIcon(self):
rect = self.GetFieldRect(1)
self.icon.SetPosition((rect.x+3, rect.y+3))
def OnSize(self, event):
self.PlaceIcon()
class FTPFrame(wx.Frame):
"""
"""
def __init__(self, *args, **kw):
super(FTPFrame, self).__init__(*args, **kw)
self.ftp = None
self.OnInit()
def OnInit(self):
panel = wx.Panel(self)
vbox = wx.BoxSizer(wx.VERTICAL)
hbox1 = wx.BoxSizer(wx.HORIZONTAL)
st1 = wx.StaticText(panel, label=_('Ftp site'))
hbox1.Add(st1, flag=wx.RIGHT, border=8)
self.ftpsite = wx.TextCtrl(panel, wx.NewIdRef(), 'http://lcapocchi.free.fr')
hbox1.Add(self.ftpsite, proportion=1)
vbox.Add(hbox1, flag=wx.EXPAND|wx.LEFT|wx.RIGHT|wx.TOP, border=10)
vbox.Add((-1, 10))
hbox2 = wx.BoxSizer(wx.HORIZONTAL)
st2 = wx.StaticText(panel, label=_('Login'))
hbox2.Add(st2, flag=wx.RIGHT, border=8)
self.login = wx.TextCtrl(panel, wx.NewIdRef(), 'lcapocchi')
hbox2.Add(self.login, proportion=1)
vbox.Add(hbox2, flag=wx.EXPAND|wx.LEFT|wx.RIGHT|wx.TOP, border=10)
vbox.Add((-1, 10))
hbox3 = wx.BoxSizer(wx.HORIZONTAL)
st3 = wx.StaticText(panel, label=_('Password'))
hbox3.Add(st3, flag=wx.RIGHT, border=8)
self.password = wx.TextCtrl(panel, wx.NewIdRef(), '', style=wx.TE_PASSWORD)
hbox3.Add(self.password, proportion=1)
vbox.Add(hbox3, flag=wx.EXPAND|wx.LEFT|wx.RIGHT|wx.TOP, border=10)
vbox.Add((-1, 10))
hbox4 = wx.BoxSizer(wx.HORIZONTAL)
con = wx.Button(panel, label=_('Connect'), size=(70, 30))
hbox4.Add(con)
discon = wx.Button(panel, label=_('DisConnect'), size=(100, 30))
hbox4.Add(discon, flag=wx.LEFT|wx.BOTTOM, border=5)
vbox.Add(hbox4, flag=wx.ALIGN_RIGHT|wx.RIGHT, border=10)
#wx.StaticText(self.panel, wx.NewIdRef(), _('Ftp site'), (20, 20))
#wx.StaticText(self.panel, wx.NewIdRef(), _('Login'), (20, 60))
#wx.StaticText(self.panel, wx.NewIdRef(), _('Password'), (20, 100))
#self.ftpsite = wx.TextCtrl(self.panel, wx.NewIdRef(), 'http://lcapocchi.free.fr', (110, 15), (120, -1))
#self.login = wx.TextCtrl(self.panel, wx.NewIdRef(), 'lcapocchi', (110, 55), (120, -1))
#self.password = wx.TextCtrl(self.panel, wx.NewIdRef(), '', (110, 95), (120, -1), style=wx.TE_PASSWORD)
#con = wx.Button(self.panel, wx.NewIdRef(), _('Connect'), (20, 160))
#discon = wx.Button(self.panel, wx.NewIdRef(), _('DisConnect'), (120, 160))
self.Bind(wx.EVT_BUTTON, self.OnConnect, id=con.GetId())
self.Bind(wx.EVT_BUTTON, self.OnDisConnect, id=discon.GetId())
self.statusbar = FTPStatusBar(self)
self.SetStatusBar(self.statusbar)
panel.SetSizer(vbox)
self.Centre()
def OnConnect(self, event):
"""
"""
if not self.ftp:
ftpsite = self.ftpsite.GetValue()
login = self.login.GetValue()
password = self.password.GetValue()
try:
self.ftp = FTP(ftpsite)
var = self.ftp.login(login, password)
self.statusbar.SetStatusText(_('User connected'))
self.statusbar.icon.SetBitmap(wx.Bitmap(os.path.join(ICON_PATH_16_16, 'connect_network.png')))
self.OnSend()
except AttributeError:
self.statusbar.SetForegroundColour(wx.RED)
self.statusbar.SetStatusText(_('Incorrect params'))
self.ftp = None
except all_errors as err:
self.statusbar.SetStatusText(str(err))
self.ftp = None
def OnSend(self, fn='out.kml'):
"""
"""
with open(fn, 'rb') as f:
sftp.storbinary('STOR %s'%"devsimpy/"+fn, f) # Send the file
def OnDisConnect(self, event):
"""
"""
if self.ftp:
self.ftp.quit()
self.ftp = None
self.statusbar.SetStatusText('User disconnected')
self.statusbar.icon.SetBitmap(wx.Bitmap(os.path.join(ICON_PATH_16_16, 'disconnect_network.png')))
### ------------------------------------------------------------
class TestApp(wx.App):
""" Testing application
"""
def OnInit(self):
import gettext
import builtins
builtins.__dict__['ICON_PATH']='icons'
builtins.__dict__['ICON_PATH_16_16']=os.path.join(ICON_PATH,'16x16')
builtins.__dict__['_'] = gettext.gettext
self.frame = FTPFrame(None, -1, 'Test')
self.frame.Show()
return True
def OnQuit(self, event):
self.Close()
if __name__ == '__main__':
app = TestApp(0)
app.MainLoop()