/
__init__.py
124 lines (102 loc) · 2.93 KB
/
__init__.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
##
#
# This module contains all the public symbols from the library.
#
import sys
import struct
import importlib
##
#
# Version
#
try:
from pr2modules.config.version import __version__
except ImportError:
__version__ = 'unknown'
##
#
# Windows platform specific: socket module monkey patching
#
# To use the library on Windows, run::
# pip install win-inet-pton
#
if sys.platform.startswith('win'): # noqa: E402
import win_inet_pton # noqa: F401
##
#
# Logging setup
#
# See the history:
# * https://github.com/svinota/pyroute2/issues/246
# * https://github.com/svinota/pyroute2/issues/255
# * https://github.com/svinota/pyroute2/issues/270
# * https://github.com/svinota/pyroute2/issues/573
# * https://github.com/svinota/pyroute2/issues/601
#
from pr2modules.config import log
##
#
from pr2modules.config import entry_points_aliases
#
#
try:
from importlib import metadata
except ImportError:
import importlib_metadata as metadata
try:
# probe, if the bytearray can be used in struct.unpack_from()
struct.unpack_from('I', bytearray((1, 0, 0, 0)), 0)
except Exception:
if sys.version_info[0] < 3:
# monkeypatch for old Python versions
log.warning('patching struct.unpack_from()')
def wrapped(fmt, buf, offset=0):
return struct._u_f_orig(fmt, str(buf), offset)
struct._u_f_orig = struct.unpack_from
struct.unpack_from = wrapped
else:
raise
# load entry_points
modules = []
namespace_inject = {}
for entry_point in metadata.entry_points().get('pr2modules', []):
loaded = entry_point.load()
modules.append(entry_point.name)
if len(entry_point.value.split(':')) == 1:
key = 'pyroute2.%s' % entry_point.name
namespace_inject[key] = loaded
else:
globals()[entry_point.name] = loaded
__all__ = []
__all__.extend(modules)
# alias exceptions
for key, value in entry_points_aliases.items():
if key in sys.modules:
sys.modules[value] = sys.modules[key]
class PyRoute2ModuleSpec(importlib.machinery.ModuleSpec):
def __init__(self, name, loader, *argv,
origin=None, loader_state=None, is_package=None):
self.name = name
self.loader = loader
self.origin = None
self.submodule_search_locations = None
self.loader_state = None
self.cached = None
self.has_location = False
class PyRoute2ModuleFinder(importlib.abc.MetaPathFinder):
@staticmethod
def find_spec(fullname, path, target=None):
if target is not None:
return None
if fullname not in namespace_inject:
return None
return PyRoute2ModuleSpec(fullname, PyRoute2ModuleFinder)
@staticmethod
def create_module(spec):
if spec.name not in namespace_inject:
return None
return namespace_inject[spec.name]
@staticmethod
def exec_module(spec):
pass
sys.meta_path.append(PyRoute2ModuleFinder())