1
2 r"""
3 :Copyright:
4
5 Copyright 2006 - 2015
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 if __doc__:
35
36 __doc__ = __doc__.encode('ascii').decode('unicode_escape')
37 __author__ = r"Andr\xe9 Malo".encode('ascii').decode('unicode_escape')
38 __docformat__ = "restructuredtext en"
39
40 import os as _os
41
42
43
44
45
46
47
48 DEFAULT_ENV_OVERRIDE = 'TDI_NO_C_OVERRIDE'
49
50
51
52
53 DEFAULT_TPL = 'tdi.c._tdi_%s'
54
55
56 -def load(modname, env_override=None, tpl=None):
57 """
58 Module loading facade
59
60 :Parameters:
61 `modname` : ``str``
62 Module name part (like ``util`` for ``tdi.c._tdi_util``), see `tpl`
63
64 `env_override` : ``str``
65 Name of the environment variable, which can disable the c extension
66 import if set to ``1``. If omitted or ``None``,
67 `DEFAULT_ENV_OVERRIDE` is applied.
68
69 `tpl` : ``str``
70 Template for the fully qualified module name. It has to contain one
71 %s format specifier which takes the `modname` part. If omitted or
72 ``None``, `DEFAULT_TPL` is applied.
73
74 :Return: The requested module or ``None`` (either by env request or
75 ``ImportError``)
76 :Rtype: ``module``
77 """
78 if env_override is None:
79 env_override = DEFAULT_ENV_OVERRIDE
80 if _os.environ.get(env_override) != '1':
81 if tpl is None:
82 tpl = DEFAULT_TPL
83 try:
84 mod = __import__(tpl % modname, globals(), locals(), ['*'])
85 except ImportError:
86 mod = None
87 else:
88 mod = None
89 return mod
90