/
__conf__.py
80 lines (68 loc) · 2.16 KB
/
__conf__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# -*- coding: UTF-8 -*-
"""Deprecation warnings, for use when no backward-compatibility provided.
"""
import builtins as bi
import warnings
from importlib import import_module
from inspect import currentframe
from lazy_object_proxy import Proxy
from functools import wraps
warnings.filterwarnings("always")
def deprecate(old, new=None):
def _warn(o, n):
msg = "'%s' has been deprecated" % o
if new is not None:
msg += ", please use '%s' instead" % n
warnings.warn(msg, DeprecationWarning)
# react differently in function of the input
if isinstance(old, type(lambda: 0)):
n = old.__name__
def _old(old):
@wraps(old)
def _wrapper(*a, **kw):
_warn(n, new)
return old(*a, **kw)
return _wrapper
currentframe().f_back.f_globals[n] = _old(old)
else:
_warn(old, new)
bi.deprecate = deprecate
bi.warn = warnings.warn
bi.lazy_object = Proxy
def lazy_load_module(module, relative=None, alias=None, preload=None, postload=None):
""" Lazily load a module. """
glob = currentframe().f_back.f_globals
def _load():
if callable(preload):
preload()
glob[alias or module] = m = import_module(*((module, ) if relative is None else ("." + module, relative)))
m.__name__ = alias or module
if callable(postload):
try:
postload()
except TypeError:
postload(m)
return m
glob[alias or module] = m = Proxy(_load)
return m
bi.lazy_load_module = lazy_load_module
def lazy_load_object(name, load_func, preload=None, postload=None):
""" Lazily load an object. """
glob = currentframe().f_back.f_globals
def _load():
if callable(preload):
preload()
glob[name] = o = load_func()
try:
o._instance = o
except (AttributeError, TypeError):
pass
if callable(postload):
try:
postload()
except TypeError:
postload(o)
return o
glob[name] = o = Proxy(_load)
return o
bi.lazy_load_object = lazy_load_object