comparison frontends/src/jp/cmd_file.py @ 1643:17f9b911899a

jp (file): new file/upload command
author Goffi <goffi@goffi.org>
date Sun, 22 Nov 2015 17:37:47 +0100
parents 5a641e7b858a
children d17772b0fe22
comparison
equal deleted inserted replaced
1642:7ec7ce9cdc4c 1643:17f9b911899a
36 class Send(base.CommandBase): 36 class Send(base.CommandBase):
37 def __init__(self, host): 37 def __init__(self, host):
38 super(Send, self).__init__(host, 'send', use_progress=True, use_verbose=True, help=_('Send a file to a contact')) 38 super(Send, self).__init__(host, 'send', use_progress=True, use_verbose=True, help=_('Send a file to a contact'))
39 39
40 def add_parser_options(self): 40 def add_parser_options(self):
41 self.parser.add_argument("files", type=str, nargs = '+', help=_("A list of file")) 41 self.parser.add_argument("files", type=str, nargs='+', metavar='file', help=_("a list of file"))
42 self.parser.add_argument("jid", type=base.unicode_decoder, help=_("The destination jid")) 42 self.parser.add_argument("jid", type=base.unicode_decoder, help=_("the destination jid"))
43 self.parser.add_argument("-b", "--bz2", action="store_true", help=_("Make a bzip2 tarball")) 43 self.parser.add_argument("-b", "--bz2", action="store_true", help=_("make a bzip2 tarball"))
44 44
45 def connected(self): 45 def connected(self):
46 """Send files to jabber contact""" 46 """Send files to jabber contact"""
47 self.need_loop=True 47 self.need_loop=True
48 super(Send, self).connected() 48 super(Send, self).connected()
213 if self.args.multiple: 213 if self.args.multiple:
214 self.host.quit_on_progress_end = False 214 self.host.quit_on_progress_end = False
215 self.disp(_(u"waiting for incoming file request"),2) 215 self.disp(_(u"waiting for incoming file request"),2)
216 216
217 217
218 class Upload(base.CommandBase):
219 def __init__(self, host):
220 super(Upload, self).__init__(host, 'upload', use_progress=True, use_verbose=True, help=_('Upload a file'))
221
222 def add_parser_options(self):
223 self.parser.add_argument("file", type=str, help=_("file to upload"))
224 self.parser.add_argument("jid", type=base.unicode_decoder, nargs='?', help=_("jid of upload component (nothing to autodetect)"))
225 self.parser.add_argument("--ignore-tls-errors", action="store_true", help=_("ignore invalide TLS certificate"))
226
227 def connected(self):
228 """Send files to jabber contact"""
229 self.need_loop=True
230 super(Upload, self).connected()
231 self.uploadFile()
232
233 def onProgressStarted(self, metadata):
234 self.disp(_(u'File upload started'),2)
235
236 def onProgressFinished(self, metadata):
237 self.disp(_(u'File uploaded successfully'),2)
238 try:
239 url = metadata['url']
240 except KeyError:
241 self.disp(u'download URL not found in metadata')
242 else:
243 self.disp(_(u'URL to retrieve the file:'),1)
244 # XXX: url is display alone on a line to make parsing easier
245 self.disp(url)
246
247 def onProgressError(self, error_msg):
248 self.disp(_(u'Error while uploading file: {}').format(error_msg),error=True)
249
250 def gotId(self, data, file_):
251 """Called when a progress id has been received
252
253 @param pid(unicode): progress id
254 @param file_(str): file path
255 """
256 try:
257 self.progress_id = data['progress']
258 except KeyError:
259 # TODO: if 'xmlui' key is present, manage xmlui message display
260 self.disp(_(u"Can't upload file"), error=True)
261 self.host.quit(2)
262
263 def error(self, failure):
264 self.disp(_("Error while trying to upload a file: {reason}").format(reason=failure), error=True)
265 self.host.quit(1)
266
267 def uploadFile(self):
268 file_ = self.args.file
269 if not os.path.exists(file_):
270 self.disp(_(u"file [{}] doesn't exist !").format(file_), error=True)
271 self.host.quit(1)
272 if os.path.isdir(file_):
273 self.disp(_(u"[{}] is a dir! Can't upload a dir").format(file_))
274 self.host.quit(1)
275
276 self.full_dest_jid = self.host.get_full_jid(self.args.jid) if self.args.jid is not None else ''
277 options = {}
278 if self.args.ignore_tls_errors:
279 options['ignore-tls-errors'] = C.BOOL_TRUE
280
281 path = os.path.abspath(file_)
282 self.host.bridge.fileUpload(path, '', self.full_dest_jid, options, self.profile, callback=lambda pid, file_=file_: self.gotId(pid, file_), errback=self.error)
283
284
218 class File(base.CommandBase): 285 class File(base.CommandBase):
219 subcommands = (Send, Receive) 286 subcommands = (Send, Receive, Upload)
220 287
221 def __init__(self, host): 288 def __init__(self, host):
222 super(File, self).__init__(host, 'file', use_profile=False, help=_('File sending/receiving')) 289 super(File, self).__init__(host, 'file', use_profile=False, help=_('File sending/receiving'))