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