Skip to content

Commit 34a6614

Browse files
committed
chore: Refactor _Utils.py to improve module import handling
1 parent 6303601 commit 34a6614

File tree

3 files changed

+31
-20
lines changed

3 files changed

+31
-20
lines changed

src/iop/_utils.py

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import os
2+
import sys
23
import ast
34
import iris
45
import inspect
56
import xmltodict
67
import pkg_resources
8+
import importlib
79

810
class _Utils():
911
@staticmethod
@@ -186,20 +188,17 @@ def migrate(filename=None,root_path=None):
186188
* key: the name of the production
187189
* value: a dictionary containing the settings for the production
188190
"""
189-
# try to load the settings file
190-
if filename:
191-
import sys
192-
path = None
193-
# check if the filename is absolute or relative
194-
if os.path.isabs(filename):
195-
path = os.path.dirname(filename)
191+
try:
192+
# if the filename is not provided
193+
if filename is None:
194+
settings = importlib.import_module('settings')
196195
else:
197-
raise ValueError("The filename must be absolute")
198-
# add the path to the system path to the beginning
199-
sys.path.append(path)
200-
import settings
201-
# get the path of the settings file
202-
path = os.path.dirname(inspect.getfile(settings))
196+
# import the settings file
197+
settings = _Utils.import_module_from_path('settings',filename)
198+
# get the path of the settings file
199+
path = os.path.dirname(inspect.getfile(settings))
200+
except ModuleNotFoundError as e:
201+
raise ModuleNotFoundError("settings.py not found") from e
203202
try:
204203
# set the classes settings
205204
_Utils.set_classes_settings(settings.CLASSES,path)
@@ -211,7 +210,22 @@ def migrate(filename=None,root_path=None):
211210
except AttributeError:
212211
print("No productions to register")
213212

214-
213+
@staticmethod
214+
def import_module_from_path(module_name, file_path):
215+
if not os.path.isabs(file_path):
216+
file_path = os.path.abspath(file_path)
217+
# check is a file is persent at the path
218+
if not os.path.isfile(file_path):
219+
# append settings.py to the path
220+
file_path = os.path.join(file_path,'settings.py')
221+
222+
spec = importlib.util.spec_from_file_location(module_name, file_path)
223+
if spec is None:
224+
raise ImportError(f"Cannot find module named {module_name} at {file_path}")
225+
226+
module = importlib.util.module_from_spec(spec)
227+
sys.modules[module_name] = module
228+
return module
215229

216230
@staticmethod
217231
def set_classes_settings(class_items,root_path=None):

src/iop/cls/IOP/Utils.cls

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ ClassMethod GetRemoteClassInfo(
120120
set onePath = $p(extraClasspaths,"|",i)
121121
set onePath = ##class(%File).NormalizeDirectory(onePath)
122122
if onePath?1"$$IRISHOME"1P.E set onePath = $e($system.Util.InstallDirectory(),1,*-1)_$e(onePath,11,*)
123-
if onePath'="" do sys.path.insert(0, onePath)
123+
if onePath'="" && '$FIND(sys.path,onePath) do sys.path.insert(0, onePath)
124124
}
125125
}
126126
;

src/tests/test_bench.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@ class TestBenchIoP:
1111
#before all tests
1212
@classmethod
1313
def setup_class(cls):
14-
# Add the 'bench' to sys.path
15-
sys.path.insert(0,os.path.abspath('src/tests/bench'))
1614
# migrate the production
17-
_Utils.migrate()
15+
_Utils.migrate('src/tests/bench')
1816
# stop all productions
1917
_Director.stop_production()
2018
# set the default production
@@ -45,5 +43,4 @@ def teardown_class(cls):
4543
_Director.stop_production()
4644
# set the default production
4745
_Director.set_default_production('test')
48-
# Remove the 'bench' from sys.path
49-
sys.path.remove(os.path.abspath('src/tests/bench'))
46+

0 commit comments

Comments
 (0)