Package reflectometry :: Package reduction :: Module registry :: Class ExtensionRegistry

Class ExtensionRegistry

source code



Associate a file loader with an extension.

Note that there may be multiple loaders for the same extension.

Example
=======

registry = ExtensionRegistry()

# Add an association by setting an element
registry['.zip'] = unzip

# Multiple extensions for one loader
registry['.tgz'] = untar
registry['.tar.gz'] = untar

# Generic extensions to use after trying more specific extensions;
# these will be checked after the more specific extensions fail.
registry['.gz'] = gunzip

# Multiple loaders for one extension
registry['.cx'] = cx1
registry['.cx'] = cx2
registry['.cx'] = cx3

# Show registered extensions
print registry.extensions()

# Can also register a format name for explicit control from caller
registry['cx3'] = cx3
print registry.formats()

# Retrieve loaders for a file name
registry.lookup('hello.cx') -> [cx3,cx2,cx1]

# Run loader on a filename
registry.load('hello.cx') ->
    try:
        return cx3('hello.cx')
    except:
        try:
            return cx2('hello.cx')
        except:
            return cx1('hello.cx')

# Load in a specific format ignoring extension
registry.load('hello.cx',format='cx3') ->
    return cx3('hello.cx')

Instance Methods
 
__init__(self)
x.__init__(...) initializes x; see x.__class__.__doc__ for signature
source code
 
__setitem__(self, ext, loader) source code
 
__getitem__(self, ext) source code
 
__contains__(self, ext) source code
 
formats(self)
Return a sorted list of the registered formats.
source code
 
extensions(self)
Return a sorted list of registered extensions.
source code
 
lookup(self, path)
Return the loader associated with the file type of path.
source code
 
load(self, path, format=None)
Call the loader for the file type of path.
source code

Inherited from object: __delattr__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __str__

Properties

Inherited from object: __class__

Method Details

__init__(self)
(Constructor)

source code 
x.__init__(...) initializes x; see x.__class__.__doc__ for signature
Overrides: object.__init__
(inherited documentation)

lookup(self, path)

source code 

Return the loader associated with the file type of path.

Raises ValueError if file type is not known.

load(self, path, format=None)

source code 

Call the loader for the file type of path.

Raises ValueError if no loader is available. Raises KeyError if format is not available. May raise a loader-defined exception if loader fails.