Bladeren bron

leveleditor: Fix use of removed imp module (untested)

rdb 1 dag geleden
bovenliggende
commit
c0bf7e075c

+ 8 - 7
direct/src/leveleditor/FileMgr.py

@@ -1,5 +1,5 @@
 import os
-import imp
+import importlib.util
 
 
 class FileMgr:
@@ -32,10 +32,10 @@ class FileMgr:
                 f.write(data)
                 f.write('\n')
             f.close()
-            self.editor.updateStatusReadout('Sucessfully saved to %s'%fileName)
+            self.editor.updateStatusReadout(f'Sucessfully saved to {fileName}')
             self.editor.fNeedToSave = False
         except IOError:
-            print('failed to save %s'%fileName)
+            print(f'failed to save {fileName}')
             if f:
                 f.close()
 
@@ -43,10 +43,11 @@ class FileMgr:
         dirname, moduleName = os.path.split(fileName)
         if moduleName.endswith('.py'):
             moduleName = moduleName[:-3]
-        file, pathname, description = imp.find_module(moduleName, [dirname])
+        spec = importlib.util.spec_from_file_location(moduleName, fileName)
         try:
-            module = imp.load_module(moduleName, file, pathname, description)
-            self.editor.updateStatusReadout('Sucessfully opened file %s'%fileName)
+            module = importlib.util.module_from_spec(spec)
+            spec.loader.exec_module(module)
+            self.editor.updateStatusReadout(f'Sucessfully opened file {fileName}')
             self.editor.fNeedToSave = False
         except Exception:
-            print('failed to load %s'%fileName)
+            print(f'failed to load {fileName}')

+ 9 - 4
direct/src/leveleditor/LevelLoaderBase.py

@@ -1,4 +1,4 @@
-import imp
+import importlib.util
 
 
 class LevelLoaderBase:
@@ -30,10 +30,15 @@ class LevelLoaderBase:
 
         if fileName.endswith('.py'):
             fileName = fileName[:-3]
-        file, pathname, description = imp.find_module(fileName, [filePath])
+
         try:
-            module = imp.load_module(fileName, file, pathname, description)
+            spec = importlib.util.spec_from_file_location(fileName, filePath)
+            if spec is None or spec.loader is None:
+                raise ImportError
+
+            module = importlib.util.module_from_spec(spec)
+            spec.loader.exec_module(module)
             return True
         except Exception:
-            print('failed to load %s'%fileName)
+            print(f'failed to load {fileName}')
             return None

+ 8 - 3
direct/src/leveleditor/ProtoObjs.py

@@ -2,7 +2,7 @@
 Palette for Prototyping
 """
 import os
-import imp
+import importlib.util
 
 
 class ProtoObjs:
@@ -15,8 +15,13 @@ class ProtoObjs:
     def populate(self):
         moduleName = self.name
         try:
-            file, pathname, description = imp.find_module(moduleName, [self.dirname])
-            module = imp.load_module(moduleName, file, pathname, description)
+            pathname = self.dirname + self.filename
+            spec = importlib.util.spec_from_file_location(moduleName, pathname)
+            if spec is None or spec.loader is None:
+                raise ImportError
+
+            module = importlib.util.module_from_spec(spec)
+            spec.loader.exec_module(module)
             self.data = module.protoData
         except Exception:
             print(f"{self.name} doesn't exist")

+ 11 - 3
direct/src/leveleditor/ProtoPaletteBase.py

@@ -1,7 +1,8 @@
 """
 Palette for Prototyping
 """
-import imp
+import importlib.util
+import os, sys
 
 from .ObjectPaletteBase import ObjectBase, ObjectPaletteBase
 
@@ -23,8 +24,15 @@ class ProtoPaletteBase(ObjectPaletteBase):
     def populate(self):
         moduleName = 'protoPaletteData'
         try:
-            file, pathname, description = imp.find_module(moduleName, [self.dirname])
-            module = imp.load_module(moduleName, file, pathname, description)
+            pathname = os.path.join(self.dirname, moduleName + ".py")
+            spec = importlib.util.spec_from_file_location(moduleName, pathname)
+            if spec is None or spec.loader is None:
+                raise ImportError
+
+            module = importlib.util.module_from_spec(spec)
+            sys.modules[module_name] = module
+            spec.loader.exec_module(module)
+
             self.data = module.protoData
             self.dataStruct = module.protoDataStruct
         except: