leveleditor: Fix use of removed imp module (untested)

This commit is contained in:
rdb 2026-01-28 16:12:45 +01:00
parent c1a2458f8e
commit c0bf7e075c
4 changed files with 36 additions and 17 deletions

View File

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

View File

@ -1,4 +1,4 @@
import imp import importlib.util
class LevelLoaderBase: class LevelLoaderBase:
@ -30,10 +30,15 @@ class LevelLoaderBase:
if fileName.endswith('.py'): if fileName.endswith('.py'):
fileName = fileName[:-3] fileName = fileName[:-3]
file, pathname, description = imp.find_module(fileName, [filePath])
try: 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 return True
except Exception: except Exception:
print('failed to load %s'%fileName) print(f'failed to load {fileName}')
return None return None

View File

@ -2,7 +2,7 @@
Palette for Prototyping Palette for Prototyping
""" """
import os import os
import imp import importlib.util
class ProtoObjs: class ProtoObjs:
@ -15,8 +15,13 @@ class ProtoObjs:
def populate(self): def populate(self):
moduleName = self.name moduleName = self.name
try: try:
file, pathname, description = imp.find_module(moduleName, [self.dirname]) pathname = self.dirname + self.filename
module = imp.load_module(moduleName, file, pathname, description) 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 self.data = module.protoData
except Exception: except Exception:
print(f"{self.name} doesn't exist") print(f"{self.name} doesn't exist")

View File

@ -1,7 +1,8 @@
""" """
Palette for Prototyping Palette for Prototyping
""" """
import imp import importlib.util
import os, sys
from .ObjectPaletteBase import ObjectBase, ObjectPaletteBase from .ObjectPaletteBase import ObjectBase, ObjectPaletteBase
@ -23,8 +24,15 @@ class ProtoPaletteBase(ObjectPaletteBase):
def populate(self): def populate(self):
moduleName = 'protoPaletteData' moduleName = 'protoPaletteData'
try: try:
file, pathname, description = imp.find_module(moduleName, [self.dirname]) pathname = os.path.join(self.dirname, moduleName + ".py")
module = imp.load_module(moduleName, file, pathname, description) 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.data = module.protoData
self.dataStruct = module.protoDataStruct self.dataStruct = module.protoDataStruct
except: except: