Exemplo n.º 1
0
def load_into_collections_from_zipfile(collections, zipfile):
    """
    Loads resources contained in the given ZIP archive for each of the
    given collection classes. 
    
    The ZIP file is expected to contain a list of file names obtained with
    the :func:`get_collection_filename` function, each pointing to a file
    of zipped collection resource data.
    
    :param collection_classes: sequence of collection resource classes
    :param str zipfile: ZIP file name
    """
    with ZipFile(zipfile) as zipf:
        names = zipf.namelist()
        name_map = dict([(os.path.splitext(name)[0], index)
                         for (index, name) in enumerate(names)])
        for coll in collections:
            coll_name = get_collection_name(coll)
            index = name_map.get(coll_name)
            if index is None:
                continue
            coll_fn = names[index]
            ext = os.path.splitext(coll_fn)[1]
            try:
                content_type = \
                    MimeTypeRegistry.get_type_for_extension(ext)
            except KeyError:
                raise ValueError('Could not infer MIME type for file '
                                 'extension "%s".' % ext)
            for mb in load_collection_from_stream(type(coll),
                                                  zipf.open(coll_fn, 'r'),
                                                  content_type):
                coll.add(mb)
    return collections
Exemplo n.º 2
0
def load_into_collections_from_zipfile(collections, zipfile):
    """
    Loads resources contained in the given ZIP archive into each of the
    given collections.

    The ZIP file is expected to contain a list of file names obtained with
    the :func:`get_collection_filename` function, each pointing to a file
    of zipped collection resource data.

    :param collections: sequence of collection resources
    :param str zipfile: ZIP file name
    """
    with ZipFile(zipfile) as zipf:
        names = zipf.namelist()
        name_map = dict([(os.path.splitext(name)[0], index)
                         for (index, name) in enumerate(names)])
        for coll in collections:
            coll_name = get_collection_name(coll)
            index = name_map.get(coll_name)
            if index is None:
                continue
            coll_fn = names[index]
            ext = os.path.splitext(coll_fn)[1]
            try:
                content_type = \
                    MimeTypeRegistry.get_type_for_extension(ext)
            except KeyError:
                raise ValueError('Could not infer MIME type for file '
                                 'extension "%s".' % ext)
            # Strings are always written as UTF-8 encoded byte strings when
            # the zip file is created, so we have to wrap the iterator into
            # a decoding step.
            coll_data = DecodingStream(zipf.open(coll_fn, 'r'))
            load_into_collection_from_stream(coll, coll_data, content_type)
Exemplo n.º 3
0
def load_into_collections_from_zipfile(collections, zipfile):
    """
    Loads resources contained in the given ZIP archive for each of the
    given collection classes. 
    
    The ZIP file is expected to contain a list of file names obtained with
    the :func:`get_collection_filename` function, each pointing to a file
    of zipped collection resource data.
    
    :param collection_classes: sequence of collection resource classes
    :param str zipfile: ZIP file name
    """
    with ZipFile(zipfile) as zipf:
        names = zipf.namelist()
        name_map = dict([(os.path.splitext(name)[0], index)
                         for (index, name) in enumerate(names)])
        for coll in collections:
            coll_name = get_collection_name(coll)
            index = name_map.get(coll_name)
            if index is None:
                continue
            coll_fn = names[index]
            ext = os.path.splitext(coll_fn)[1]
            try:
                content_type = \
                    MimeTypeRegistry.get_type_for_extension(ext)
            except KeyError:
                raise ValueError('Could not infer MIME type for file '
                                 'extension "%s".' % ext)
            for mb in load_collection_from_stream(type(coll),
                                                  zipf.open(coll_fn, 'r'),
                                                  content_type):
                coll.add(mb)
    return collections
Exemplo n.º 4
0
def load_collection_from_file(collection_class, filename, content_type=None):
    """
    Loads resources from the specified file into the given collection
    resource.
    
    If no content type is provided, an attempt is made to look up the 
    extension of the given filename in the MIME content type registry.
    """
    if content_type is None:
        ext = os.path.splitext(filename)[1]
        try:
            content_type = MimeTypeRegistry.get_type_for_extension(ext)
        except KeyError:
            raise ValueError('Could not infer MIME type for file extension '
                             '"%s".' % ext)
    return load_collection_from_stream(collection_class, open(filename, 'rU'),
                                       content_type)
Exemplo n.º 5
0
def load_into_collection_from_file(collection, filename, content_type=None):
    """
    Loads resources from the specified file into the given collection
    resource.

    If no content type is provided, an attempt is made to look up the
    extension of the given filename in the MIME content type registry.
    """
    if content_type is None:
        ext = os.path.splitext(filename)[1]
        try:
            content_type = MimeTypeRegistry.get_type_for_extension(ext)
        except KeyError:
            raise ValueError('Could not infer MIME type for file extension '
                             '"%s".' % ext)
    load_into_collection_from_stream(collection, open(filename, 'rU'),
                                     content_type)
Exemplo n.º 6
0
def load_into_collections_from_zipfile(collections, zipfile):
    """
    Loads resources contained in the given ZIP archive into each of the
    given collections.

    The ZIP file is expected to contain a list of file names obtained with
    the :func:`get_collection_filename` function, each pointing to a file
    of zipped collection resource data.

    :param collections: sequence of collection resources
    :param str zipfile: ZIP file name
    """
    with ZipFile(zipfile) as zipf:
        names = zipf.namelist()
        name_map = dict([(os.path.splitext(name)[0], index)
                         for (index, name) in enumerate(names)])
        for coll in collections:
            coll_name = get_collection_name(coll)
            index = name_map.get(coll_name)
            if index is None:
                continue
            coll_fn = names[index]
            ext = os.path.splitext(coll_fn)[1]
            try:
                content_type = \
                    MimeTypeRegistry.get_type_for_extension(ext)
            except KeyError:
                raise ValueError('Could not infer MIME type for file '
                                 'extension "%s".' % ext)
            # Strings are always written as UTF-8 encoded byte strings when
            # the zip file is created, so we have to wrap the iterator into
            # a decoding step.
            coll_data = DecodingStream(zipf.open(coll_fn, 'r'))
            load_into_collection_from_stream(coll,
                                             coll_data,
                                             content_type)