Пример #1
0
def get_caller_module() -> Module:
    """Get reference to callers module."""
    name = get_caller_module_name(-2)
    if name:
        module = pkg.get_module(name)
        if isinstance(module, Module):
            return module
    raise ModuleNotFoundError("could not detect module of caller")
Пример #2
0
def get_lib() -> Module:
    """Get module for TTY I/O control.

    Depending on the plattform the module within the standard library, which is
    required for tty I/O control differs. The module :py:mod:`termios` provides
    an interface to the POSIX calls for tty I/O control. The module
    :py:mod:`msvcrt` provides access to some useful capabilities on Windows
    platforms.

    Returns:
        Reference to module for tty I/O control or None, if the module could
        not be determined.

    """
    for name in ['msvcrt', 'termios']:
        module = pkg.get_module(name, errors=False)
        if module:
            return module
    raise ImportError("no module for TTY I/O could be imported")
Пример #3
0
def update_vars(filepath: OptPathLike = None, pkgname: OptStr = None) -> None:
    """Update environment and application variables.

    Environment variables comprise static and runtime properties of the
    operating system like 'username' or 'hostname'. Application variables in
    turn, are intended to describe the application distribution by authorship
    information, bibliographic information, status, formal conditions and notes
    or warnings. For mor information see :PEP:`345`.

    Args:
        filepath: Valid filepath to python module, that contains the application
            variables as module attributes. By default the current top level
            module is used.

    """
    # Get package specific environment variables by parsing a given file for
    # module attributes. By default the file of the callers current top level
    # module is taken. If the name is not given, then use the name of the
    # current top level module.
    pkgname = pkgname or pkg.get_root_name(stack.get_caller_module_name(-2))
    filepath = filepath or pkg.get_module(pkgname).__file__
    text = pathlib.Path(filepath).read_text()
    pattern = r"^[ ]*__([^\d\W]\w*)__[ ]*=[ ]*['\"]([^'\"]*)['\"]"
    matches = re.finditer(pattern, text, re.M)
    info = {str(m.group(1)): str(m.group(2)) for m in matches}
    info['name'] = info.get('name', pkgname)

    # Get plattform specific environment variables
    info['encoding'] = get_encoding()
    info['hostname'] = get_hostname()
    info['osname'] = get_osname()
    info['username'] = get_username()

    # Update globals
    if not 'cache' in globals():
        globals()['cache'] = {}
    if not pkgname in globals()['cache']:
        globals()['cache'][pkgname] = {}
    globals()['cache'][pkgname]['vars'] = info
Пример #4
0
 def test_get_module(self) -> None:
     this = pkg.get_module()
     self.assertEqual(getattr(this, '__name__', None), __name__)
Пример #5
0
 def test_get_root_name(self) -> None:
     name = pkg.get_root_name(pkg.__name__)
     self.assertTrue(bool(pkg.get_module(name).__file__))