Source code for core.paths

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Path management."""

import os

from . import log

__paths = {}


def _identify_folder_name(base, name):
    """
    Allows folder names to be lowercase on case-sensitive systems.
    Returns "base/name" where name is lowercase if the lower case version
    exists and the standard case version does not.

    Args:
        base: the path containing the desired folder.
        name: the standard case name of the desired folder.
    """
    normal = os.path.join(base, name)
    lower = os.path.join(base, name.lower())
    if os.path.isdir(lower) and not os.path.isdir(normal):
        return lower
    return normal


[docs]def register(name, *path_elms, **kwargs): """Registers a path constructed by <path_elms> under <name>. If multiple path elements are given, the last element will undergo case correction (see _identify_folder_name). Args: name: a registered name corresponding to some path segment path_elems: path elements, which will be joined to the root path allow_create: If True, the registered path will be created if it does not already exist. Defaults to True. """ if len(path_elms) > 1: __paths[name] = _identify_folder_name(os.path.join( *path_elms[:-1]), path_elms[-1]) else: __paths[name] = path_elms[0] log.i('Registering path %s as %s', name, __paths[name]) if kwargs.get('allow_create', True) and not os.path.exists(__paths[name]): os.makedirs(__paths[name])
[docs]def get(name, *paths): """Returns the path registered under <name>, or an empty string if <name> is not known.""" try: base = __paths[name] except KeyError: base = '' return os.path.join(base, *paths)
[docs]def clear(): """Clears the path cache.""" __paths.clear()