Package autodesk_toxik :: Package utils :: Module interp
[frames] | no frames]

Module interp

The interp module contains utility functions related to the Python interpreter itself: starting a script in an existing interpreter, loading a specific file as a module, frame hack utilities, that sort of things.

Functions
 
runScript(scriptPath, args=[])
Runs the given python file scriptPath as a script (not as a module) providing it the given command-line arguments args.
module
loadModule(filePath, compile=True, reload=False)
Loads a specific Python file as a module.
dict
getModuleTypesByExtension()
Gets the Python file extensions recognized by Composite indexed by module types.
dict
getExtensionsByModuleType()
Gets the Python module types recognized by Composite indexed by file extensions.
list
getSourceFileExtensions()
Gets the extensions of a Python source file.
list
getCompiledFileExtensions()
Gets the extensions of a Python compiled file (optimized included).
imp.PY_SOURCE, imp.PY_COMPILED, imp.C_EXTENSION or None
getModuleTypeFromExtension(ext)
Gets the module type associated with the given file extension or None if the extension is not recognized on this platform.
str
getCompiledFilePath(filePath)
Gets the path of the compiled Python file generated by the interpreter when the specified Python file is imported.
str
getOptimizedFilePath(filePath)
Gets the path of the compiled optimized Python file generated by the interpreter when the specified Python file is imported in optimize mode.
str
getModuleNameFromPath(filePath)
Gets a unique module name from a file path which is composed of the file basename suffixed by an hex representation of the normalized absolute path of the file.
str
getValidVariableName(s)
Converts an arbitrary string to a valid Python variable name.
str
getObjectName(o)
Returns: The name of the given object (__name__ attribute).
str
getObjectModuleName(o)
Returns: The name of the module in which the object was defined (__module__ attribute).
str
getModulePath(moduleName)
Returns: The path of the file from which the module with the given name was loaded.
Variables
  moduleTypesByExtension = {}
  extensionsByModuleType = {}
Function Details

runScript(scriptPath, args=[])

 

Runs the given python file scriptPath as a script (not as a module) providing it the given command-line arguments args. The script is ran within the current python interpreter and has access to the symbols in the global namepace (see globals() builtin function), so it has access to modules that are already loaded.

Parameters:
  • scriptPath (str) - Path of the script to run.
  • args (list of str) - Script arguments, empty by default.

loadModule(filePath, compile=True, reload=False)

 

Loads a specific Python file as a module. If the file has already been loaded it will not be reloaded and the existing module is returned, unless specified otherwise.

Parameters:
  • filePath (str) - Path of the Python file to load.
  • compile (bool) - If set to True (default), a compiled version of the specified Python file will be created next to it (with a 'c' suffix), if the permissions on the directory allows for it.
  • reload (bool) - If set to True, the file is reloaded even if its module already exists. Defaults to False.
Returns: module
The imported module.
Raises:
  • IOError - If the specified file does not exist or cannot be read.
  • ImportError - If the specified file cannot be imported as a Python module.
  • SyntaxError - If there's a syntax error in the specified Python file.

getModuleTypesByExtension()

 

Gets the Python file extensions recognized by Composite indexed by module types.

Returns: dict
Dictionary with a file extension (dot included) as keys and a module type (imp.PY_SOURCE, imp.PY_COMPILED, imp.C_EXTENSION) as values.

getExtensionsByModuleType()

 

Gets the Python module types recognized by Composite indexed by file extensions.

Returns: dict
Dictionary with a module type (imp.PY_SOURCE, imp.PY_COMPILED, imp.C_EXTENSION) as keys and a list of file extensions (dot included) as values.

getSourceFileExtensions()

 

Gets the extensions of a Python source file.

Returns: list
List of file extensions (dot included).

getCompiledFileExtensions()

 

Gets the extensions of a Python compiled file (optimized included).

Returns: list
List of file extensions (dot included).

getModuleTypeFromExtension(ext)

 

Gets the module type associated with the given file extension or None if the extension is not recognized on this platform.

Parameters:
  • ext (str) - File extension.
Returns: imp.PY_SOURCE, imp.PY_COMPILED, imp.C_EXTENSION or None
Module type or None.

getCompiledFilePath(filePath)

 

Gets the path of the compiled Python file generated by the interpreter when the specified Python file is imported.

Parameters:
  • filePath (str) - Path of the Python file that gets compiled.
Returns: str
Path of the compiled Python file.

getOptimizedFilePath(filePath)

 

Gets the path of the compiled optimized Python file generated by the interpreter when the specified Python file is imported in optimize mode.

Parameters:
  • filePath (str) - Path of the Python file that gets optimized.
Returns: str
Path of the compiled optimized Python file.

getModuleNameFromPath(filePath)

 

Gets a unique module name from a file path which is composed of the file basename suffixed by an hex representation of the normalized absolute path of the file.

Parameters:
  • filePath (str #return: Module name.) - File path.
Returns: str

getValidVariableName(s)

 

Converts an arbitrary string to a valid Python variable name. Invalid characters are substituted by underscores.

Parameters:
  • s (str) - The string to convert.
Returns: str
Variable name

getObjectName(o)

 
Returns: str
The name of the given object (__name__ attribute). Since not all Python objects have a name, the empty string can be returned.

getObjectModuleName(o)

 
Returns: str
The name of the module in which the object was defined (__module__ attribute). Since not all Python objects are defined in a module, the empty string can be returned.

getModulePath(moduleName)

 
Returns: str
The path of the file from which the module with the given name was loaded. Returns an empty string if the specified module doesn't exist (cannot be found in sys.modules) or if the specified module doesn't originate from a file (a builtin module for example).