Source

slipper / slipper / config.py

def repo_map_from_config(config):
    """Convert INI-style configuration to a useful repository map.

    `config` is a dictionary whose keys and values correspond to an
    INI file::

        >>> config = {
        ...     'slipper.DEFAULT.cache_dir': '/path/to/cache/dir',
        ...     'slipper.repo1.local_dir': '/path/to/repo1',
        ...     'slipper.repo1.hgweb_url': 'http://path.to/repo1',
        ...     'slipper.repo2.local_dir': '/path/to/repo2',
        ...     'slipper.repo2.hgweb_url': 'http://path.to/repo2',
        ...     'slipper.repo2.rst_extension': '.txt',
        ...     }

    Returns a dictionary whose keys are the names of each repository,
    and whose values are objects with the following string keys:

    - ``cache_dir``
    - ``rst_extension``
    - ``local_dir``
    - ``hgweb_url``

    ::

        >>> repo_map = repo_map_from_config(config)
        >>> sorted(repo_map.keys())
        ['repo1', 'repo2']
    """
    # Find default information.
    default = RepositoryInfo(
        cache_dir = config.pop('slipper.DEFAULT.cache_dir', None),
        rst_extension = config.pop('slipper.DEFAULT.rst_extension', '.rst'),
        stylesheet_path = config.pop('slipper.DEFAULT.stylesheet_path', None),
        local_dir = None,
        hgweb_url = None,
        )
    # Build the repository map.
    repo_map = {}
    for key, value in config.iteritems():
        keyparts = key.split('.')
        if len(keyparts) != 3 or keyparts[0] != 'slipper':
            # Not a configuration item we care about.
            continue
        _, repo_name, config_key = keyparts
        config = repo_map.setdefault(repo_name, default.copy())
        setattr(config, config_key, value)
    # Done.
    return repo_map


class RepositoryInfo(object):

    def __init__(self, **kw):
        self.__dict__.update(kw)

    def copy(self):
        return RepositoryInfo(**self.__dict__)