Did I find the right examples for you? yes no      Crawl my project      Python Jobs

All Samples(1)  |  Call(0)  |  Derive(0)  |  Import(1)
Decorator to support loading from compressed files for functions
whose first argument is the sourcePath

If the first argument ends with a recognized extension,
the decorator uncompresses sourcePath to a temporary folder
and runs the function on each resulting file until it succeeds.

Archive format is determined by file extension:
.zip .tar.gz .tar.bz2 .tar
(more...)

        def load(function=None, extensions=None, CustomException=IOError):
    """
    Decorator to support loading from compressed files for functions
    whose first argument is the sourcePath

    If the first argument ends with a recognized extension,
    the decorator uncompresses sourcePath to a temporary folder
    and runs the function on each resulting file until it succeeds.

    Archive format is determined by file extension:
    .zip .tar.gz .tar.bz2 .tar

    # Try load(sourcePath) on each file in the archive until one works
    @archiveIO.load
    def load(sourcePath):
        return open(sourcePath).read()
    # Try load(sourcePath) on TXT files and if all fail, try CSV files
    @archiveIO.load(extensions=['.txt', '.csv'])
    def load(sourcePath):
        return open(sourcePath).read()
    # Raise AppError instead of IOError
    @archiveIO.load(CustomException=AppError)
    def load(sourcePath):
        return open(sourcePath).read()
    """
    def load(function, *args, **kw):
        sourcePath = kw.get('sourcePath', args[0])
        try:
            archive = Archive(sourcePath)
        # If we did not recognize the extension, run function as usual
        except ArchiveError:
            return function(*args, **kw)
        # Make temporaryFolder
        with TemporaryFolder() as temporaryFolder:
            try:
                # Convert a disposable generator into a reusable list
                paths = list(archive.load(temporaryFolder))
                # For each path, run function and exit if successful
                for path in select_extensions(paths, extensions or []):
                    try:
                        return function(path, *args[1:], **kw)
                    except:
                        pass
            except:
                raise CustomException('Could not open archive')
            raise CustomException('Could not load archive')
    if function:
        return decorator(load, function)
    else:
        return decorator(load)
        


src/a/r/archiveIO-0.4.4/archiveIO/tests.py   archiveIO(Download)
from unittest import TestCase
 
from archiveIO import Archive, ArchiveError, save, load, select_extensions, walk_paths, EXTENSION_PACKS