示例#1
0
def work_in_zope(context, name, root_path):
    """
    Call this method from a Zope ExternalMethod. It assumes a local
    folder that has CIRCA-exported Zip files (using the "save" method,
    not "download"). Call it from the Web and pass in a "name" parameter
    which is the filename you want to import. Make sure you call the
    external method in the context of a folder where you want the files
    to be imported.

    Example external method code::
        from edw.circaimport import work_in_zope
        def do_import(self, REQUEST):
            name = REQUEST.get('name')
            return work_in_zope(self, name, '/path/to/downloads/folder')
    """
    from actors import ZopeActor
    assert '/' not in name and name != '..'
    zip_path = root_path + '/' + name
    zip_fs_file = open(zip_path, 'rb')
    report = StringIO()

    zf = ZipFile(zip_fs_file)
    index_file = StringIO(zf.read('index.txt'))
    actor = ZopeActor(context, report)
    def open_backup_file(name):
        return StringIO(zf.read(name))

    backupdata.walk_backup(index_file, open_backup_file, actor)

    actor.finished()

    zip_fs_file.close()
    return report.getvalue()
示例#2
0
def add_files_and_folders_from_circa_export(context, name, root_path):
    """
    Call this method from a Zope ExternalMethod. It assumes a local
    folder that has CIRCA-exported Zip files (using the "save" method,
    not "download"). Call it from the Web and pass in a "name" parameter
    which is the filename you want to import. Make sure you call the
    external method in the context of a folder where you want the files
    to be imported.

    Example external method code::
        from edw.circaimport import work_in_zope
        def do_import(self, REQUEST):
            name = REQUEST.get('name')
            return work_in_zope(self, name, '/path/to/downloads/folder')
    """
    logger.debug('start importing files and folders')

    from actors import ZopeActor
    assert '/' not in name and name != '..'
    zip_path = root_path + '/' + name
    zip_fs_file = open(zip_path, 'rb')

    zf = ZipFile(zip_fs_file)
    index_file = StringIO(zf.read('index.txt'))
    current_user = context.REQUEST.AUTHENTICATED_USER.getId()

    actor = ZopeActor(context, current_user)
    def _open_on_key_error(name):
        """
        Patches for when the names are wrong (usually unicode encoding problems)
        """
        chain = name.split('/')
        parent_path = '/'.join(chain[:-1])
        matching_path = parent_path + '/' + chain[-1][0]
        matches = [fname for fname in zf.namelist()
                if fname.startswith(matching_path)]
        assert len(matches) == 1
        return StringIO(zf.read(matches[0]))

    def open_backup_file(name):
        try:
            return StringIO(zf.read(name))
        except KeyError:
            return _open_on_key_error(name)

    def get_date(name):
        info = zf.getinfo(name)
        return datetime.date(*info.date_time[0:3])

    backupdata.walk_backup(index_file, open_backup_file, get_date, actor)

    actor.finished()

    zip_fs_file.close()

    logger.debug('done importing files and folders')