import_hook.py
This module contains the core functions required to create an import hook.
- class ideas.import_hook.IdeasLoader(filename, callback_params=None, create_module=None, exec_=None, module_class=None, source_init=None, transform_ast=None, transform_bytecode=None, transform_source=None)[source]
A custom loader which will transform the source prior to its execution
- class ideas.import_hook.IdeasMetaFinder(callback_params=None, create_module=None, excluded_paths=None, exec_=None, extensions=None, hook_name=None, module_class=None, source_init=None, transform_ast=None, transform_bytecode=None, transform_source=None)[source]
A custom finder to locate modules. The main reason for this code is to ensure that our custom loader, which does the code transformations, is used.
- ideas.import_hook.create_hook(callback_params=None, create_module=None, console_dict=None, exec_=None, extensions=None, first=True, hook_name=None, ipython_ast_node_transformer=None, module_class=None, source_init=None, transform_ast=None, transform_bytecode=None, transform_source=None)[source]
Function to facilitate the creation of an import hook.
Each of the following parameter is optional; most of these are never needed except in some unusual import hooks.
Usually, at least one of
transform_ast
,transform_bytecode
, andtransform_source
should be specified.callback_params
: a dict containing keyword parameters to be passed back to thetransform_source
function.create_module
: a custom function to create a module object instead of using Python’s default.console_dict
: a dict object used as ‘locals’ with the Ideas console, instead of its usual default.exec_
: a custom method used to execute the source code inside a module’s dict.extensions
: a list of file extensions, other than the usual .py, etc., used to identify modules containing source code.first
: ifTrue
, the custom hook will be used as the first location insys.meta_path
, to look for source files.hook_name
: used to give a more readablerepr
to the hook created.ipython_ast_node_transformer
: used to do AST transformations in an IPython/Jupyter environment. It should be a class derived fromast.NodeTransformer
and return anode
.module_class
: custom class to use for the module created instead of the default one assigned by Python.source_init
: custom code to be executed before any code from a user is executed. For example, if one creates an import hook that treats everyfloat
as aDecimal
object, this custom code could be:from decimal import Decimal
transform_ast
: used to do AST transformations in a Python environment (excluding IPython/Jupyter). It should be a class derived fromast.NodeTransformer
, eventually returning a tree object.transform_bytecode
: used to mutate a code object.transform_source
: used to transform some source code prior to execution.
- ideas.import_hook.make_ipython_ast_node_transformer(ipython_ast_node_transformer)[source]
Takes an AST transformer designed to work with IPython, and wraps it to add a warning in case the user would like to see how the code is actually transformed, since this is not possible when using IPython.