1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
36
37
38
39
40
41 DEFAULT_ENV_OVERRIDE = 'TDI_NO_C_OVERRIDE'
42
43
44
45
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