Exemplo n.º 1
0
def add_hook(on_prevent_change=None, verbose_finder=False):
    """Creates and adds the import hook in sys.meta_path

    When an attempt is made to change the value of a constant, ``on_prevent_change``
    is called. By default, this function just prints a warning about what
    was done. This could be replaced by a function that logs the results or
    one that raises an exception, etc.
    """
    if on_prevent_change is None:
        on_prevent_change = on_change_print
    callback_params = {"on_prevent_change": on_prevent_change}

    module_class = make_class(**callback_params)
    console_dict = FinalDict(CONSOLE_NAME, on_prevent_change=on_prevent_change)
    CONSTANTS[CONSOLE_NAME] = {}

    hook = import_hook.create_hook(
        module_class=module_class,
        transform_source=transform_source,
        exec_=exec_,
        callback_params=callback_params,
        console_dict=console_dict,
        hook_name=__name__,
        verbose_finder=verbose_finder,
    )
    return hook
Exemplo n.º 2
0
def add_hook(verbose_finder=False):
    """Creates and automatically adds the import hook in sys.meta_path"""
    hook = import_hook.create_hook(
        hook_name=__name__,
        transform_bytecode=transform_bytecode,
    )
    return hook
Exemplo n.º 3
0
def add_hook(verbose_finder=False):
    """Creates and automatically adds the import hook in sys.meta_path"""
    hook = import_hook.create_hook(
        hook_name=__name__,
        source_init=source_init,
        transform_source=transform_source,
        verbose_finder=verbose_finder,
    )
    return hook
Exemplo n.º 4
0
def add_hook(show_original=False,
             show_transformed=False,
             verbose_finder=False):
    """Creates and automatically adds the import hook in sys.meta_path"""
    callback_params = {
        "show_original": show_original,
        "show_transformed": show_transformed,
    }
    hook = import_hook.create_hook(
        transform_source=transform_source,
        callback_params=callback_params,
        hook_name=__name__,
        verbose_finder=verbose_finder,
    )
    return hook
Exemplo n.º 5
0
def add_hook():
    """Creates and automatically adds the import hook in sys.meta_path"""
    hook = import_hook.create_hook(transform_source=transform_source)
    return hook
Exemplo n.º 6
0
    """Similar to Python's dir, but shows the original name
    entered, not the transformed one. It also filters out any variable
    whose names starts with a double underscore.

    Note: the real Python dir() should be available as true_dir().
    """
    import inspect
    if obj is not None:
        names = dir(obj)
    else:
        names = list(inspect.currentframe().f_back.f_locals)
    for k, v in __NAMES_MAP.items():
        names = [_.replace(v, k) for _ in names]
    names = [name for name in names if not names.startswith("__")]
    if obj is None:
        # Purposely hide some names :-)
        for name in ['dir', 'true_dir']:
            if name in names:
                names.remove(name)
    return sorted(names)


import_hook.create_hook(
    transform_source=transform_names,
    console_dict={
        "__NAMES_MAP": __NAMES_MAP,
        "dir": new_dir,
        "true_dir": dir
    },
)