當前位置: 首頁>>代碼示例>>Python>>正文


Python Mega.download方法代碼示例

本文整理匯總了Python中mega.Mega.download方法的典型用法代碼示例。如果您正苦於以下問題:Python Mega.download方法的具體用法?Python Mega.download怎麽用?Python Mega.download使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在mega.Mega的用法示例。


在下文中一共展示了Mega.download方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: MegaBackend

# 需要導入模塊: from mega import Mega [as 別名]
# 或者: from mega.Mega import download [as 別名]
class MegaBackend(duplicity.backend.Backend):
    """Connect to remote store using Mega.co.nz API"""

    def __init__(self, parsed_url):
        duplicity.backend.Backend.__init__(self, parsed_url)

        try:
            from mega import Mega
        except ImportError:
            raise BackendException('Mega.co.nz backend requires Mega.co.nz APIs Python Module'
                                   '(see https://github.com/richardasaurus/mega.py).')

        # Setup client instance.
        self.client = Mega()
        self.client.domain = parsed_url.hostname
        self.__authorize(parsed_url.username, self.get_password())

        # Fetch destination folder entry (and crete hierarchy if required).
        folder_names = parsed_url.path[1:].split('/')
        files = self.client.get_files()

        parent_folder = self.client.root_id
        for folder_name in folder_names:
            entries = self.__filter_entries(files, parent_folder, folder_name, 'folder')
            if len(entries):
                # use first matching folder as new parent
                parent_folder = entries.keys()[0]
            else:
                # create subfolder if folder doesn't exist and use its handle as parent
                folder_node = self.client.create_folder(folder_name, parent_folder)
                parent_folder = self.client.get_id_from_obj(folder_node)
                # update filelist after creating new folder
                files = self.client.get_files()

        self.folder = parent_folder

    def _put(self, source_path, remote_filename):
        try:
            self._delete(remote_filename)
        except Exception:
            pass
        self.client.upload(source_path.get_canonical(), self.folder, dest_filename=remote_filename)

    def _get(self, remote_filename, local_path):
        files = self.client.get_files()
        entries = self.__filter_entries(files, self.folder, remote_filename, 'file')
        if len(entries):
            # get first matching remote file
            entry = entries.keys()[0]
            self.client.download((entry, entries[entry]), dest_filename=local_path.name)
        else:
            raise BackendException("Failed to find file '%s' in remote folder '%s'"
                                   % (remote_filename, self.__get_node_name(self.folder)),
                                   code=log.ErrorCode.backend_not_found)

    def _list(self):
        entries = self.client.get_files_in_node(self.folder)
        return [self.client.get_name_from_file({entry: entries[entry]}) for entry in entries]

    def _delete(self, filename):
        files = self.client.get_files()
        entries = self.__filter_entries(files, self.folder, filename, 'file')
        if len(entries):
            self.client.destroy(entries.keys()[0])
        else:
            raise BackendException("Failed to find file '%s' in remote folder '%s'"
                                   % (filename, self.__get_node_name(self.folder)),
                                   code=log.ErrorCode.backend_not_found)

    def __get_node_name(self, handle):
        """get node name from public handle"""
        files = self.client.get_files()
        return self.client.get_name_from_file({handle: files[handle]})

    def __authorize(self, email, password):
        self.client.login(email, password)

    def __filter_entries(self, entries, parent_id=None, title=None, type=None):
        result = {}
        type_map = {'folder': 1, 'file': 0}

        for k, v in entries.items():
            try:
                if parent_id is not None:
                    assert(v['p'] == parent_id)
                if title is not None:
                    assert(v['a']['n'] == title)
                if type is not None:
                    assert(v['t'] == type_map[type])
            except AssertionError:
                continue

            result.update({k: v})

        return result
開發者ID:cmjonze,項目名稱:duplicity,代碼行數:97,代碼來源:megabackend.py


注:本文中的mega.Mega.download方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。