| 1 |
From: Stuart Prescott <[email protected]> |
| 2 |
Date: Thu, 8 Aug 2024 09:30:39 +1000 |
| 3 |
Subject: Switch to importlib from imp |
| 4 |
|
| 5 |
Patch from Vincent Meille <[email protected]> |
| 6 |
|
| 7 |
Closes: #1078148 |
| 8 |
--- |
| 9 |
bkchem/main.py | 17 ++++++++++++++--- |
| 10 |
1 file changed, 14 insertions(+), 3 deletions(-) |
| 11 |
|
| 12 |
diff --git a/bkchem/main.py b/bkchem/main.py |
| 13 |
index 9467544..b323406 100644 |
| 14 |
--- a/bkchem/main.py |
| 15 |
+++ b/bkchem/main.py |
| 16 |
@@ -487,21 +487,32 @@ class BKChem( Tk): |
| 17 |
'plus', 'text', 'bracket', 'rotate', 'bondalign', 'vector', 'misc']# 'reaction', 'externaldata'] #, 'rapiddraw'] |
| 18 |
|
| 19 |
# import plugin modes |
| 20 |
- import imp |
| 21 |
+ import importlib.util |
| 22 |
+ import importlib.machinery |
| 23 |
+ |
| 24 |
+ def load_source(modname, filename): |
| 25 |
+ loader = importlib.machinery.SourceFileLoader(modname, filename) |
| 26 |
+ spec = importlib.util.spec_from_file_location(modname, filename, loader=loader) |
| 27 |
+ module = importlib.util.module_from_spec(spec) |
| 28 |
+ loader.exec_module(module) |
| 29 |
+ return module |
| 30 |
+ |
| 31 |
for plug_name in self.plug_man.get_names( type="mode"): |
| 32 |
plug = self.plug_man.get_plugin_handler( plug_name) |
| 33 |
module_name = plug.get_module_name() |
| 34 |
# no invalid characters in mode_name |
| 35 |
mode_name = ''.join(x in string.ascii_letters and x or "X" for x in module_name) |
| 36 |
try: |
| 37 |
- module = imp.load_source( module_name, plug.filename) |
| 38 |
+ module = load_source( module_name, plug.filename) |
| 39 |
except ImportError: |
| 40 |
continue |
| 41 |
else: |
| 42 |
self.modes[ module_name.replace("_","")] = module.plugin_mode() |
| 43 |
self.modes_sort.append( module_name.replace("_","")) |
| 44 |
|
| 45 |
- del imp |
| 46 |
+ del load_source |
| 47 |
+ del importlib.machinery |
| 48 |
+ del importlib.util |
| 49 |
|
| 50 |
|
| 51 |
def init_mode_buttons( self): |