Package logilab :: Package common :: Module modutils
[show private | hide private]
[frames | no frames]

Module logilab.common.modutils

Python modules manipulation utility functions.


Version:

$Revision: 1.2 $

Author:

Logilab

Contact:

http://www.logilab.fr/ -- mailto:python-projects@logilab.org

Copyright:

2003-2005 LOGILAB S.A. (Paris, FRANCE)

Exceptions
NoSourceFile exception raised when we are not able to get a python source file for a precompiled file

Function Summary

str or None

file_from_modpath(modpath, path, context_file)
given a mod path (ie splited module / package name), return the corresponding file, giving priority to source file over precompiled file if it exists

list

get_module_files(src_directory, blacklist)
given a package directory return a list of all available python module's files in the package and its subpackages

str or None

get_module_part(dotted_name, context_file)
given a dotted name return the module part of the name :

list

get_modules(package, src_directory, blacklist)
given a package directory return a list of all available python modules in the package and its subpackages

str

get_source_file(filename, include_no_ext)
given a python module's file name return the matching source file name (the filename will be returned identically if it's a already an absolute path to a python source file...)
  is_python_source(filename)
rtype: bool return: True if the filename is a python source file

bool

is_relative(modname, from_file)
return true if the given module name is relative to the given file name

bool

is_standard_module(modname, std_path)
try to guess if a module is a standard python module (by default, see std_path parameter's description)

module

load_module_from_modpath(parts, path, use_sys, _prefix)
load a python module from it's splitted name

module

load_module_from_name(dotted_name, path, use_sys)
load a Python module from it's name

module

load_module_from_parts(parts, path, use_sys, _prefix)
load a python module from it's splitted name

list(str)

modpath_from_file(filename)
given a file path return the corresponding splitted module's name (i.e name of a module or package splitted on '.')
  _file_from_modpath(modpath, path, context)
given a mod path (ie splited module / package name), return the corresponding file
  _has_init(directory)
if the given directory has a valid __init__ file, return its path, else return None
  _is_python_file(filename)
return true if the given filename should be considered as a python file

tuple(int, str)

_module_file(modpath, path)
get a module type / file path

Variable Summary
str __revision__ = '$Id: modutils.py,v 1.47 2005/04/07 15:42...

dict

BUILTIN_MODULES: dictionary with builtin module names has key

tuple(str)

PY_SOURCE_EXTS: list of possible python source file extension

str

STD_LIB_DIR: directory where standard modules are located

Function Details

file_from_modpath(modpath, path=None, context_file=None)

given a mod path (ie splited module / package name), return the corresponding file, giving priority to source file over precompiled file if it exists

Parameters:
modpath -

splitted module's name (i.e name of a module or package splitted on '.')


           (type=

list or tuple

)
path -

optional list of path where the module or package should be searched (use sys.path if nothing or None is given)


           (type=

list or None

)
context_file -

context file to consider, necessary if the identifier has been introduced using a relative import unresolvable in the actual context (i.e. modutils)


           (type=

str or None

)
Returns:

the path to the module's file or None if it's an integrated builtin module such as 'sys'


           (type=

str or None

)
Raises:
ImportError -

if there is no such module in the directory

get_module_files(src_directory, blacklist=('CVS', '.svn', 'debian'))

given a package directory return a list of all available python module's files in the package and its subpackages

Parameters:
src_directory -

path of the directory corresponding to the package


           (type=

str

)
blacklist -

optional list of files or directory to ignore, default to 'CVS', '.svn' and 'debian'


           (type=

list(str) or tuple(str)

)
Returns:

the list of all available python module's files in the package and its subpackages


           (type=

list

)

get_module_part(dotted_name, context_file=None)

given a dotted name return the module part of the name :

>>> get_module_part('logilab.common.modutils.get_module_part')
'logilab.common.modutils'
Parameters:
dotted_name -

full name of the identifier we are interested in


           (type=

str

)
context_file -

context file to consider, necessary if the identifier has been introduced using a relative import unresolvable in the actual context (i.e. modutils)


           (type=

str or None

)
Returns:

the module part of the name or None if we have not been able at all to import the given name


           (type=

str or None

)
Raises:
ImportError -

if there is no such module in the directory

get_modules(package, src_directory, blacklist=('CVS', '.svn', 'debian'))

given a package directory return a list of all available python modules in the package and its subpackages

Parameters:
package -

the python name for the package


           (type=

str

)
src_directory -

path of the directory corresponding to the package


           (type=

str

)
blacklist -

optional list of files or directory to ignore, default to 'CVS', '.svn' and 'debian'


           (type=

list or tuple

)
Returns:

the list of all available python modules in the package and its subpackages


           (type=

list

)

get_source_file(filename, include_no_ext=False)

given a python module's file name return the matching source file name (the filename will be returned identically if it's a already an absolute path to a python source file...)

Parameters:
filename -

python module's file name


           (type=

str

)
Returns:

the absolute path of the source file if it exists


           (type=

str

)
Raises:
NoSourceFile -

if no source file exists on the file system

is_python_source(filename)

rtype: bool return: True if the filename is a python source file

is_relative(modname, from_file)

return true if the given module name is relative to the given file name

Parameters:
modname -

name of the module we are interested in


           (type=

str

)
from_file -

path of the module from which modname has been imported


           (type=

str

)
Returns:

true if the module has been imported relativly to from_file


           (type=

bool

)

is_standard_module(modname, std_path=('/usr/lib/python2.3',))

try to guess if a module is a standard python module (by default, see std_path parameter's description)

Parameters:
modname -

name of the module we are interested in


           (type=

str

)
std_path -

list of path considered has standard


           (type=

list(str) or tuple(str)

)
Returns:

true if the module: - is located on the path listed in one of the directory in std_path - is a built-in module


           (type=

bool

)

load_module_from_modpath(parts, path=None, use_sys=1, _prefix=None)

load a python module from it's splitted name

Parameters:
parts -

python name of a module or package splitted on '.'


           (type=

list(str) or tuple(str)

)
path -

optional list of path where the module or package should be searched (use sys.path if nothing or None is given)


           (type=

list or None

)
use_sys -

boolean indicating whether the sys.modules dictionary should be used or not


           (type=

bool

)
_prefix -

used internally, should not be specified

Returns:

the loaded module


           (type=

module

)
Raises:
ImportError -

if the module or package is not found

load_module_from_name(dotted_name, path=None, use_sys=1)

load a Python module from it's name

Parameters:
dotted_name -

python name of a module or package


           (type=

str

)
path -

optional list of path where the module or package should be searched (use sys.path if nothing or None is given)


           (type=

list or None

)
use_sys -

boolean indicating whether the sys.modules dictionary should be used or not


           (type=

bool

)
Returns:

the loaded module


           (type=

module

)
Raises:
ImportError -

if the module or package is not found

load_module_from_parts(parts, path=None, use_sys=1, _prefix=None)

load a python module from it's splitted name

Parameters:
parts -

python name of a module or package splitted on '.'


           (type=

list(str) or tuple(str)

)
path -

optional list of path where the module or package should be searched (use sys.path if nothing or None is given)


           (type=

list or None

)
use_sys -

boolean indicating whether the sys.modules dictionary should be used or not


           (type=

bool

)
_prefix -

used internally, should not be specified

Returns:

the loaded module


           (type=

module

)
Raises:
ImportError -

if the module or package is not found

modpath_from_file(filename)

given a file path return the corresponding splitted module's name (i.e name of a module or package splitted on '.')

Parameters:
filename -

file's path for which we want the module's name


           (type=

str

)
Returns:

the corresponding splitted module's name


           (type=

list(str)

)
Raises:
ImportError -

if the corresponding module's name has not been found

_file_from_modpath(modpath, path=None, context=None)

given a mod path (ie splited module / package name), return the corresponding file

this function is used internally, see file_from_modpath's documentation for more information

_has_init(directory)

if the given directory has a valid __init__ file, return its path, else return None

_is_python_file(filename)

return true if the given filename should be considered as a python file

.pyc and .pyo are ignored

_module_file(modpath, path=None)

get a module type / file path

Parameters:
modpath -

splitted module's name (i.e name of a module or package splitted on '.')


           (type=

list or tuple

)
path -

optional list of path where the module or package should be searched (use sys.path if nothing or None is given)


           (type=

list or None

)
Returns:

the module type flag and the file path for a module


           (type=

tuple(int, str)

)

Variable Details

__revision__

Type:
str
Value:
'$Id: logilab.common.modutils-module.html,v 1.2 2005/04/19 14:39:10 fabioz Exp $'                

BUILTIN_MODULES

dictionary with builtin module names has key

Type:

dict

Value:
{'__builtin__': 1,
 '__main__': 1,
 '_codecs': 1,
 '_sre': 1,
 '_symtable': 1,
 'errno': 1,
 'exceptions': 1,
 'gc': 1,
...                                                                    

PY_SOURCE_EXTS

list of possible python source file extension

Type:

tuple(str)

Value:
('py',)                                                                

STD_LIB_DIR

directory where standard modules are located

Type:

str

Value:
'/usr/lib/python2.3'                                                   

Generated by Epydoc 2.1 on Thu Apr 14 11:37:32 2005 http://epydoc.sf.net