当前位置: 首页>>代码示例>>Python>>正文


Python Mega.get_files_in_node方法代码示例

本文整理汇总了Python中mega.Mega.get_files_in_node方法的典型用法代码示例。如果您正苦于以下问题:Python Mega.get_files_in_node方法的具体用法?Python Mega.get_files_in_node怎么用?Python Mega.get_files_in_node使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在mega.Mega的用法示例。


在下文中一共展示了Mega.get_files_in_node方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: MegaBackend

# 需要导入模块: from mega import Mega [as 别名]
# 或者: from mega.Mega import get_files_in_node [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

示例2: DriverMega

# 需要导入模块: from mega import Mega [as 别名]
# 或者: from mega.Mega import get_files_in_node [as 别名]
class DriverMega(Service):

    ERROR_TRY_AGAIN = -3
    NODE_FILE       = 0
    NODE_ROOT       = 2


    def __init__(self, params):
        '''
        Connects to Mega with the provided credentials

        Arguments
        ---------
        params: A dict with the params to be passed to the initialised object
        '''

        self.logger = logging.getLogger('clouddump')
        self.logger.info("Connecting to Mega...")
        self.params = params
        self.mega = Mega()
        try:
            self.mega.login(params['user_name'], params['password'])
            self.logger.info("Sucessfully logged in to Mega.")
        except Exception:
            self.logger.error(
                "Authentication to Mega failed. Please check user / password.")
            sys.exit(errno.EPERM)


    def upload(self, file_name):
        '''
        Uploads the file to Mega, retrying if it couldn't be achieved

        Arguments
        ---------
        file_name: A string with the name of the file to be uploaded
        '''

        try:
            link = self._upload_try(file_name)
        except Exception as e:
            if e == DriverMega.ERROR_TRY_AGAIN:
                count = 0
                self.logger.warn(
                    "Failed to upload %s to Mega. Trying again..." %
                    (file_name))
                while link == None and count < self.params['retries']:
                    link = self._upload_try(file_name)
                    count += 1
                if link == None:
                    self.logger.error("Upload failed after all retries.")


    def delete_old_files(self):
        '''Delete files from the specified folder older than x days'''

        files = self._get_folder_files()
        if len(files) > 0:
            past = datetime.datetime.now() - datetime.timedelta(
                days = self.params["delete_files_older_than"])
            past_timestamp = calendar.timegm(past.utctimetuple())
            for file_id, file_metadata in files.iteritems():
                if file_metadata['ts'] < past_timestamp:
                    try:
                        self.mega.delete(file_id)
                        self.logger.info(
                            "%s deleted" % (file_metadata['a']['n']))
                    except Exception as e:
                        if e == DriverMega.ERROR_TRY_AGAIN:
                            self.logger.warn(
                                "Failed to delete %s from Mega." %
                                (file_metadata['a']['n']))

        else:
            self.logger.info("No files to delete")


    def _get_folder_files(self):
        '''
        Return the files stored in the specified folder, or from the root one
        if no folder was passed
        '''

        if self.params['folder_name'] == '':
            only_files = {}
            files = self.mega.get_files_in_node(DriverMega.NODE_ROOT)
            for key, value in files.iteritems():
                if value['t'] == DriverMega.NODE_FILE:
                    only_files[key] = value
            return only_files

        folder = self.mega.find(self.params['folder_name'])
        return self.mega.get_files_in_node(folder[0])


    def _upload_try(self, file_name):
        '''
        Uploads the file to the selected folder in Mega, or root if no
        folder was chosen

#.........这里部分代码省略.........
开发者ID:svera,项目名称:clouddump,代码行数:103,代码来源:driver_mega.py


注:本文中的mega.Mega.get_files_in_node方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。