1 # The MIT License (MIT)
3 # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
5 # Permission is hereby granted, free of charge, to any person obtaining a copy
6 # of this software and associated documentation files (the "Software"), to deal
7 # in the Software without restriction, including without limitation the rights
8 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 # copies of the Software, and to permit persons to whom the Software is
10 # furnished to do so, subject to the following conditions:
12 # The above copyright notice and this permission notice shall be included in
13 # all copies or substantial portions of the Software.
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 def plugin_component_class(component_class
):
28 if not issubclass(component_class
, bt2
.component
._UserComponent
):
29 raise TypeError('component class is not a subclass of a user component class')
31 component_class
._bt
_plugin
_component
_class
= None
32 return component_class
36 module_name
, name
, description
=None, author
=None, license
=None, version
=None
40 if module_name
not in sys
.modules
:
42 "cannot find module '{}' in loaded modules".format(module_name
)
45 utils
._check
_str
(name
)
47 if description
is not None:
48 utils
._check
_str
(description
)
50 if author
is not None:
51 utils
._check
_str
(author
)
53 if license
is not None:
54 utils
._check
_str
(license
)
56 if version
is not None:
57 if not _validate_version(version
):
59 'wrong version: expecting a tuple: (major, minor, patch) or (major, minor, patch, extra)'
62 sys
.modules
[module_name
]._bt
_plugin
_info
= _PluginInfo(
63 name
, description
, author
, license
, version
67 def _validate_version(version
):
71 if not isinstance(version
, tuple):
74 if len(version
) < 3 or len(version
) > 4:
77 if not isinstance(version
[0], int):
80 if not isinstance(version
[1], int):
83 if not isinstance(version
[2], int):
87 if not isinstance(version
[3], str):
94 def __init__(self
, name
, description
, author
, license
, version
):
96 self
.description
= description
98 self
.license
= license
99 self
.version
= version
100 self
.comp_class_addrs
= None
103 # called by the BT plugin system
104 def _try_load_plugin_module(path
):
105 import importlib
.machinery
110 raise TypeError('missing path')
112 # In order to load the module uniquely from its path, even from
113 # different files which have the same basename, we hash the path
114 # and prefix with `bt_plugin_`. This is its key in sys.modules.
116 h
.update(path
.encode())
117 module_name
= 'bt_plugin_{}'.format(h
.hexdigest())
119 # try loading the module: any raised exception is catched by the caller
120 mod
= importlib
.machinery
.SourceFileLoader(module_name
, path
).load_module()
122 # we have the module: look for its plugin info first
123 if not hasattr(mod
, '_bt_plugin_info'):
124 raise RuntimeError("missing '_bt_plugin_info' module attribute")
126 plugin_info
= mod
._bt
_plugin
_info
128 # search for user component classes
129 def is_user_comp_class(obj
):
130 if not inspect
.isclass(obj
):
133 if not hasattr(obj
, '_bt_plugin_component_class'):
138 comp_class_entries
= inspect
.getmembers(mod
, is_user_comp_class
)
139 plugin_info
.comp_class_addrs
= [entry
[1].addr
for entry
in comp_class_entries
]
This page took 0.031446 seconds and 4 git commands to generate.