3
Initialize mrv system assisting development, debugging and maintenance
5
- install general `decorator` into __builtin__ namespace
10
log = logging.getLogger("mrv")
12
__all__ = ("init_modules", )
20
def init_modules( filepath, moduleprefix, recurse=False, self_module = None):
21
""" Call '__initialize' functions in submodules of module at filepath if they exist
22
These functions should setup the module to be ready for work, its a callback informing
23
the submodules that the super module is being requested. They return a True value if
24
the initialization was performed, or a False one if they weren't for some reason.
25
Throw to indicate error.
26
:param filepath: your module module.__file__ value
27
:param moduleprefix: prefix like "super.yourmodule." leading to the submodules from
28
an available system include path
29
:param recurse: if True, method will recursively initialize submodules
30
:param self_module: if not None, it must be the module that called this function.
31
It will be given to the __initialize functions as first arguments allowing
32
them to operate on functions of their own module - importing their own
33
module is not yet possible as it is in the course of being intialized itself.
34
The module will be given only to intermediate submodules in case recurse is True.
35
:note: in this moment, all submodules will be 'pulled' in"""
36
moduledir = Path( filepath ).parent()
37
moduleitems = moduledir.listdir( )
38
moduleitems.sort() # assure we have the same order on every system
39
extensions = ( ".py", ".pyc", ".pyo" )
40
initialized_modules = set()
42
if not moduleprefix.endswith( "." ):
46
for path in moduleitems:
53
packageinitfile = None
54
for ext in extensions:
55
testpackageinitfile = path / "__init__%s" % ext
56
if testpackageinitfile.exists():
57
packageinitfile = testpackageinitfile
59
# END if packageinit file exists
60
# END for each possible extension
62
# skip non-existing ones
63
if not packageinitfile:
66
init_modules( packageinitfile, moduleprefix + path.basename(), recurse=True )
70
if path.ext() not in extensions:
73
modulename = path.namebase()
74
if modulename.startswith( "_" ) or modulename.startswith( "." ) or modulename in ('all', 'mdb'):
77
fullModuleName = moduleprefix + modulename
78
if fullModuleName in initialized_modules:
80
# END prevent duplicate initialization due to different endings
81
initialized_modules.add(fullModuleName)
82
module = __import__( fullModuleName , globals(), locals(), [ modulename ] )
85
args = ( self_module and [ self_module ] ) or tuple()
86
if hasattr( module, "__initialize" ):
87
res = module.__initialize( *args )
89
log.info("Initialized " + module.__name__)
91
# END for each file or dir
98
def _remove_empty_syspath_entries():
99
"""fix sys.path: if there are empty entries and our cwd is the mrvroot
100
we will be in trouble as we try to import our own 'maya' module which
101
will not provide the original maya packages of course
103
:note: only for internal use - code was moved into a method as it needs
104
to be called again from maya.__init__"""
105
while '' in sys.path:
107
# END while we have whitespace
110
""" Initialize the path such that additional modules can be found"""
112
mrvroot = mrvroot = os.path.dirname( __file__ )
114
_remove_empty_syspath_entries()
116
# process additional site-packackes
117
# The startup script may add additional site-package paths, but if these
118
# are non-default, they will not be handled which leads to an incomplete
119
# environment. Hence we process them.
120
# Fortunately, the function handles multiple initializations gracefully
121
for syspath in sys.path[:]:
122
if syspath.endswith('site-packages'):
123
site.addsitedir(syspath, set(sys.path))
124
# END found site-packages path
125
# END for each path to possibly initialize
127
if sys.platform == 'darwin':
128
# in order to have a chance to get the setuptools going,
129
# add the default python library to the path.
130
sys.path.append("/System/Library/Frameworks/Python.framework/Versions/%s/Extras/lib/python"%sys.version[:3])
134
extbase = os.path.join( mrvroot, "ext" )
137
pyparsing = os.path.join( extbase, "pyparsing", "src" )
140
pydot = os.path.join( extbase, "pydot" )
143
networkxpath = os.path.join( extbase, "networkx" )
145
# add all to the path
146
sys.path.append( pyparsing )
147
sys.path.append( pydot )
148
sys.path.append( networkxpath )
154
def _init_configProvider( ):
155
""" Install the configuration provider system
157
This allows values and settings to be stored in a convenient way. """
160
def _init_internationalization( ):
161
"""Install internationalization module
163
Using the default python gettext module, internationalization compatibility
166
Will map the '_' function to translate enclosed strings """
168
gettext.install( "mrv" )
172
""" Initialize the default mrv logging interface
174
The logging interface unifies the way messages for the end user are handled
175
and assure a flexible message handling.
177
:note: will not raise even if the logging module could not be setup
179
:note: in the current implementation, it is based on the default python logging
181
logcfgfile = os.environ.get('MRV_LOGGING_INI', None)
182
if logcfgfile is None:
186
logcfgfile = Path(logcfgfile).expand_or_raise()
187
logging.config.fileConfig(logcfgfile)
189
print "Failed to apply logging configuration at %s with error: %s" % (logcfgfile, str(e))
191
log.debug("Initialized logging configuration from file at %s" % logcfgfile)
192
# END exception handling
198
Assure that certain python classes have the least possible amount of compatablity
199
so that we can work with them
208
_init_configProvider( )
209
_init_internationalization( )