##// END OF EJS Templates
Envio de graficos por FTP:...
Daniel Valdez -
r283:7162b4ee74da
parent child
Show More
@@ -0,0 +1,293
1 import os.path
2 import ftplib
3
4
5 class Ftp():
6 """
7 Ftp is a public class used to define custom File Transfer Protocol from "ftplib" python module
8
9 Non-standard Python modules used: None
10
11 Written by "Daniel Suarez":mailto:daniel.suarez@jro.igp.gob.pe Oct. 26, 2010
12 """
13
14 def __init__(self,host = None, username=None, passw=None, remotefolder=None):
15 """
16 This method is used to setting parameters for FTP and establishing connection to remote host
17
18 Inputs:
19 host - remote host IP Address
20
21 username - remote host Username
22
23 passw - remote host Passw
24
25 remotefolder - remote host current working directory
26
27 Return: void
28
29 Affects:
30 self.status - in Error Case or Connection Failed this parameter is set to 1 else 0
31
32 self.folderList - sub-folder list of remote folder
33
34 self.fileList - file list of remote folder
35
36
37 """
38
39 if ((host == None) and (username==None) and (passw==None) and (remotefolder==None)):
40 host, username, passw, remotefolder = self.parmsByDefault()
41
42 self.host = host
43 self.username = username
44 self.passw = passw
45 self.remotefolder = remotefolder
46 self.file = None
47 self.ftp = None
48 self.status = 0
49
50 try:
51 self.ftp = ftplib.FTP(self.host)
52 self.ftp.login(self.username,self.passw)
53 self.ftp.cwd(self.remotefolder)
54 # print 'Connect to FTP Server: Successfully'
55
56 except ftplib.all_errors:
57 print 'Error FTP Service'
58 self.status = 1
59 return
60
61
62
63 self.dirList = []
64
65 try:
66 self.dirList = self.ftp.nlst()
67
68 except ftplib.error_perm, resp:
69 if str(resp) == "550 No files found":
70 print "no files in this directory"
71 self.status = 1
72 return
73
74 except ftplib.all_errors:
75 print 'Error Displaying Dir-Files'
76 self.status = 1
77 return
78
79 self.fileList = []
80 self.folderList = []
81 #only for test
82 for f in self.dirList:
83 name, ext = os.path.splitext(f)
84 if ext != '':
85 self.fileList.append(f)
86 # print 'filename: %s - size: %d'%(f,self.ftp.size(f))
87
88 def parmsByDefault(self):
89 host = 'jro.igp.gob.pe'
90 username = 'operaciones'
91 passw = 'mst98vhf'
92 remotefolder = '/users/database/on-line/'
93
94 return host, username, passw, remotefolder
95
96
97 def mkd(self,dirname):
98 """
99 mkd is used to make directory in remote host
100
101 Input:
102 dirname - directory name
103
104 Return:
105 1 in error case else 0
106 """
107 try:
108 self.ftp.mkd(dirname)
109 except:
110 print 'Error creating remote folder:%s'%dirname
111 return 1
112
113 return 0
114
115
116 def delete(self,filename):
117 """
118 delete is used to delete file in current working directory of remote host
119
120 Input:
121 filename - filename to delete in remote folder
122
123 Return:
124 1 in error case else 0
125 """
126
127 try:
128 self.ftp.delete(filename)
129 except:
130 print 'Error deleting remote file:%s'%filename
131 return 1
132
133 return 0
134
135 def download(self,filename,localfolder):
136 """
137 download is used to downloading file from remote folder into local folder
138
139 Inputs:
140 filename - filename to donwload
141
142 localfolder - directory local to store filename
143
144 Returns:
145 self.status - 1 in error case else 0
146 """
147
148 self.status = 0
149
150
151 if not(filename in self.fileList):
152 print 'filename:%s not exists'%filename
153 self.status = 1
154 return self.status
155
156 newfilename = os.path.join(localfolder,filename)
157
158 self.file = open(newfilename, 'wb')
159
160 try:
161 print 'Download: ' + filename
162 self.ftp.retrbinary('RETR ' + filename, self.__handleDownload)
163 print 'Download Complete'
164 except ftplib.all_errors:
165 print 'Error Downloading ' + filename
166 self.status = 1
167 return self.status
168
169 self.file.close()
170
171 return self.status
172
173
174 def __handleDownload(self,block):
175 """
176 __handleDownload is used to handle writing file
177 """
178 self.file.write(block)
179
180
181 def upload(self,filename,remotefolder=None):
182 """
183 upload is used to uploading local file to remote directory
184
185 Inputs:
186 filename - full path name of local file to store in remote directory
187
188 remotefolder - remote directory
189
190 Returns:
191 self.status - 1 in error case else 0
192 """
193
194 if remotefolder == None:
195 remotefolder = self.remotefolder
196
197 self.status = 0
198
199 try:
200 self.ftp.cwd(remotefolder)
201
202 self.file = open(filename, 'rb')
203
204 (head, tail) = os.path.split(filename)
205
206 command = "STOR " + tail
207
208 print 'Uploading: ' + tail
209 self.ftp.storbinary(command, self.file)
210 print 'Upload Completed'
211
212 except ftplib.all_errors:
213 print 'Error Uploading ' + tail
214 self.status = 1
215 return self.status
216
217 self.file.close()
218
219 #back to initial directory in __init__()
220 self.ftp.cwd(self.remotefolder)
221
222 return self.status
223
224
225 def dir(self,remotefolder):
226 """
227 dir is used to change working directory of remote host and get folder and file list
228
229 Input:
230 remotefolder - current working directory
231
232 Affects:
233 self.fileList - file list of working directory
234
235 Return:
236 infoList - list with filenames and size of file in bytes
237
238 self.folderList - folder list
239 """
240
241 self.remotefolder = remotefolder
242 print 'Change to ' + self.remotefolder
243 try:
244 self.ftp.cwd(remotefolder)
245 except ftplib.all_errors:
246 print 'Error Change to ' + self.remotefolder
247 infoList = None
248 self.folderList = None
249 return infoList,self.folderList
250
251 self.dirList = []
252
253 try:
254 self.dirList = self.ftp.nlst()
255
256 except ftplib.error_perm, resp:
257 if str(resp) == "550 No files found":
258 print "no files in this directory"
259 infoList = None
260 self.folderList = None
261 return infoList,self.folderList
262 except ftplib.all_errors:
263 print 'Error Displaying Dir-Files'
264 infoList = None
265 self.folderList = None
266 return infoList,self.folderList
267
268 infoList = []
269 self.fileList = []
270 self.folderList = []
271 for f in self.dirList:
272 name,ext = os.path.splitext(f)
273 if ext != '':
274 self.fileList.append(f)
275 value = (f,self.ftp.size(f))
276 infoList.append(value)
277
278 if ext == '':
279 self.folderList.append(f)
280
281 return infoList,self.folderList
282
283
284 def close(self):
285 """
286 close is used to close and end FTP connection
287
288 Inputs: None
289
290 Return: void
291
292 """
293 self.ftp.close() No newline at end of file
@@ -2,7 +2,7 import os
2 import numpy
2 import numpy
3 import time, datetime
3 import time, datetime
4 import mpldriver
4 import mpldriver
5
5 from customftp import *
6
6
7 class Figure:
7 class Figure:
8
8
@@ -117,6 +117,7 class Figure:
117 self.heightscreen)
117 self.heightscreen)
118
118
119 self.axesObjList = []
119 self.axesObjList = []
120
120
121
121 def setDriver(self, driver=mpldriver):
122 def setDriver(self, driver=mpldriver):
122
123
@@ -160,6 +161,11 class Figure:
160
161
161 self.__driver.saveFigure(self.fig, filename, *args)
162 self.__driver.saveFigure(self.fig, filename, *args)
162
163
164 def sendByFTP(self, figfilename):
165 ftpObj = Ftp()
166 ftpObj.upload(figfilename)
167 ftpObj.close()
168
163 def draw(self):
169 def draw(self):
164
170
165 self.__driver.draw(self.fig)
171 self.__driver.draw(self.fig)
@@ -1,5 +1,5
1 import numpy
1 import numpy
2 import time, datetime
2 import time, datetime, os
3 from graphics.figure import *
3 from graphics.figure import *
4
4
5 class CrossSpectraPlot(Figure):
5 class CrossSpectraPlot(Figure):
@@ -194,6 +194,7 class RTIPlot(Figure):
194 self.HEIGHT = 150
194 self.HEIGHT = 150
195 self.WIDTHPROF = 120
195 self.WIDTHPROF = 120
196 self.HEIGHTPROF = 0
196 self.HEIGHTPROF = 0
197 self.counterftp = 0
197
198
198 def getSubplots(self):
199 def getSubplots(self):
199
200
@@ -238,7 +239,7 class RTIPlot(Figure):
238 def run(self, dataOut, idfigure, wintitle="", channelList=None, showprofile='True',
239 def run(self, dataOut, idfigure, wintitle="", channelList=None, showprofile='True',
239 xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None,
240 xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None,
240 timerange=None,
241 timerange=None,
241 save=False, figpath='./', figfile=None):
242 save=False, figpath='./', figfile=None, ftp=False, ftpratio=1):
242
243
243 """
244 """
244
245
@@ -333,6 +334,12 class RTIPlot(Figure):
333
334
334 self.saveFigure(figpath, figfile)
335 self.saveFigure(figpath, figfile)
335
336
337 self.counterftp += 1
338 if (ftp and (self.counterftp==ftpratio)):
339 figfilename = os.path.join(figpath,figfile)
340 self.sendByFTP(figfilename)
341 self.counterftp = 0
342
336 if x[1] + (x[1]-x[0]) >= self.axesList[0].xmax:
343 if x[1] + (x[1]-x[0]) >= self.axesList[0].xmax:
337 self.__isConfig = False
344 self.__isConfig = False
338
345
@@ -709,6 +716,7 class CoherenceMap(Figure):
709 self.HEIGHT = 150
716 self.HEIGHT = 150
710 self.WIDTHPROF = 120
717 self.WIDTHPROF = 120
711 self.HEIGHTPROF = 0
718 self.HEIGHTPROF = 0
719 self.counterftp = 0
712
720
713 def getSubplots(self):
721 def getSubplots(self):
714 ncol = 1
722 ncol = 1
@@ -745,7 +753,7 class CoherenceMap(Figure):
745 def run(self, dataOut, idfigure, wintitle="", pairsList=None, showprofile='True',
753 def run(self, dataOut, idfigure, wintitle="", pairsList=None, showprofile='True',
746 xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None,
754 xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None,
747 timerange=None,
755 timerange=None,
748 save=False, figpath='./', figfile=None,
756 save=False, figpath='./', figfile=None, ftp=False, ftpratio=1,
749 coherence_cmap='jet', phase_cmap='RdBu_r'):
757 coherence_cmap='jet', phase_cmap='RdBu_r'):
750
758
751 if pairsList == None:
759 if pairsList == None:
@@ -853,6 +861,12 class CoherenceMap(Figure):
853
861
854 self.saveFigure(figpath, figfile)
862 self.saveFigure(figpath, figfile)
855
863
864 self.counterftp += 1
865 if (ftp and (self.counterftp==ftpratio)):
866 figfilename = os.path.join(figpath,figfile)
867 self.sendByFTP(figfilename)
868 self.counterftp = 0
869
856 if x[1] + (x[1]-x[0]) >= self.axesList[0].xmax:
870 if x[1] + (x[1]-x[0]) >= self.axesList[0].xmax:
857 self.__isConfig = False
871 self.__isConfig = False
858
872
General Comments 0
You need to be logged in to leave comments. Login now