Package tdi :: Package c
[frames] | no frames]

Source Code for Package tdi.c

 1  # -*- coding: ascii -*- 
 2  # 
 3  # Copyright 2006, 2007, 2008, 2009, 2010 
 4  # Andr\xe9 Malo or his licensors, as applicable 
 5  # 
 6  # Licensed under the Apache License, Version 2.0 (the "License"); 
 7  # you may not use this file except in compliance with the License. 
 8  # You may obtain a copy of the License at 
 9  # 
10  #     http://www.apache.org/licenses/LICENSE-2.0 
11  # 
12  # Unless required by applicable law or agreed to in writing, software 
13  # distributed under the License is distributed on an "AS IS" BASIS, 
14  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
15  # See the License for the specific language governing permissions and 
16  # limitations under the License. 
17  """ 
18  ========================== 
19   Modules implemented in C 
20  ========================== 
21   
22  The modules in this package implement (or reimplement) various functionality 
23  in C for reasons of performance or availability. The performance 
24  implementations are always re-implementations of accompanying python 
25  functions. 
26   
27  The standard way to import these modules is to use the `load` function. It 
28  catches ImportError and disabled C overrides via environment. 
29  """ 
30  __author__ = u"Andr\xe9 Malo" 
31  __docformat__ = "restructuredtext en" 
32   
33  import os as _os 
34   
35  #: Default environment variable name 
36  #: 
37  #: This variable can be set to ``1`` in order to disable loading of tdi's c 
38  #: extensions 
39  #: 
40  #: :Type: ``str`` 
41  DEFAULT_ENV_OVERRIDE = 'TDI_NO_C_OVERRIDE' 
42   
43  #: Default template for the fully qualified module name 
44  #: 
45  #: :Type: ``str`` 
46  DEFAULT_TPL = 'tdi.c._tdi_%s' 
47   
48   
49 -def load(modname, env_override=None, tpl=None):
50 """ 51 Module loading facade 52 53 :Parameters: 54 `modname` : ``str`` 55 Module name part (like ``util`` for ``tdi.c._tdi_util``), see `tpl` 56 57 `env_override` : ``str`` 58 Name of the environment variable, which can disable the c extension 59 import if set to ``1``. If omitted or ``None``, 60 `DEFAULT_ENV_OVERRIDE` is applied. 61 62 `tpl` : ``str`` 63 Template for the fully qualified module name. It has to contain one 64 %s format specifier which takes the `modname` part. If omitted or 65 ``None``, `DEFAULT_TPL` is applied. 66 67 :Return: The requested module or ``None`` (either by env request or 68 ``ImportError``) 69 :Rtype: ``module`` 70 """ 71 if env_override is None: 72 env_override = DEFAULT_ENV_OVERRIDE 73 if _os.environ.get(env_override) != '1': 74 if tpl is None: 75 tpl = DEFAULT_TPL 76 try: 77 mod = __import__(tpl % modname, globals(), locals(), ['*']) 78 except ImportError: 79 mod = None 80 else: 81 mod = None 82 return mod
83