comparison cagou/core/cagou_main.py @ 420:83d184393fe1

core: downloadURL helper method
author Goffi <goffi@goffi.org>
date Wed, 26 Feb 2020 16:38:17 +0100
parents b047d14e4be5
children f8ba934ea462
comparison
equal deleted inserted replaced
419:b047d14e4be5 420:83d184393fe1
19 19
20 import os.path 20 import os.path
21 import glob 21 import glob
22 import sys 22 import sys
23 from pathlib import Path 23 from pathlib import Path
24 from urllib import parse as urlparse
24 from functools import partial 25 from functools import partial
25 from sat.core.i18n import _ 26 from sat.core.i18n import _
26 from . import kivy_hack 27 from . import kivy_hack
27 kivy_hack.do_hack() 28 kivy_hack.do_hack()
28 from cagou.backport import do_backport 29 from cagou.backport import do_backport
35 from sat_frontends.quick_frontend import quick_chat 36 from sat_frontends.quick_frontend import quick_chat
36 from sat_frontends.quick_frontend import quick_utils 37 from sat_frontends.quick_frontend import quick_utils
37 from sat_frontends.tools import jid 38 from sat_frontends.tools import jid
38 from sat.tools import utils as sat_utils 39 from sat.tools import utils as sat_utils
39 from sat.tools import config 40 from sat.tools import config
41 from sat.tools.common import data_format
40 from sat.tools.common import dynamic_import 42 from sat.tools.common import dynamic_import
43 from sat.tools.common import files_utils
41 import kivy 44 import kivy
42 kivy.require('1.11.0') 45 kivy.require('1.11.0')
43 import kivy.support 46 import kivy.support
44 main_config = config.parseMainConf(log_filenames=True) 47 main_config = config.parseMainConf(log_filenames=True)
45 bridge_name = config.getConfig(main_config, '', 'bridge', 'dbus') 48 bridge_name = config.getConfig(main_config, '', 'bridge', 'dbus')
1047 self.showExtraUI(share_wid) 1050 self.showExtraUI(share_wid)
1048 except Exception as e: 1051 except Exception as e:
1049 log.error(e) 1052 log.error(e)
1050 self.closeUI() 1053 self.closeUI()
1051 1054
1055 def downloadURL(
1056 self, url, callback, errback=None, options=None, dest=C.FILE_DEST_DOWNLOAD,
1057 profile=C.PROF_KEY_NONE):
1058 """Download an URL (decrypt it if necessary)
1059
1060 @param url(str, parse.SplitResult): url to download
1061 @param callback(callable): method to call when download is complete
1062 @param errback(callable, None): method to call in case of error
1063 if None, default errback will be called
1064 @param dest(str): where the file should be downloaded:
1065 - C.FILE_DEST_DOWNLOAD: in platform download directory
1066 - C.FILE_DEST_CACHE: in SàT cache
1067 @param options(dict, None): options to pass to bridge.fileDownloadComplete
1068 """
1069 if not isinstance(url, urlparse.ParseResult):
1070 url = urlparse.urlparse(url)
1071 if errback is None:
1072 errback = partial(
1073 self.errback,
1074 title=_("Download error"),
1075 message=_("Error while downloading {url}: {{msg}}").format(url=url.geturl()))
1076 name = Path(url.path).name.strip() or C.FILE_DEFAULT_NAME
1077 log.info(f"downloading/decrypting file {name!r}")
1078 if dest == C.FILE_DEST_DOWNLOAD:
1079 dest_path = files_utils.get_unique_name(Path(self.downloads_dir)/name)
1080 elif dest == C.FILE_DEST_CACHE:
1081 dest_path = ''
1082 else:
1083 raise exceptions.InternalError(f"Invalid dest_path: {dest_path!r}")
1084 self.bridge.fileDownloadComplete(
1085 url.geturl(),
1086 str(dest_path),
1087 '' if not options else data_format.serialise(options),
1088 profile,
1089 callback=callback,
1090 errback=errback
1091 )
1092
1052 def notify(self, type_, entity=None, message=None, subject=None, callback=None, 1093 def notify(self, type_, entity=None, message=None, subject=None, callback=None,
1053 cb_args=None, widget=None, profile=C.PROF_KEY_NONE): 1094 cb_args=None, widget=None, profile=C.PROF_KEY_NONE):
1054 super().notify( 1095 super().notify(
1055 type_=type_, entity=entity, message=message, subject=subject, 1096 type_=type_, entity=entity, message=message, subject=subject,
1056 callback=callback, cb_args=cb_args, widget=widget, profile=profile) 1097 callback=callback, cb_args=cb_args, widget=widget, profile=profile)