Skip to content

Commit 9255511

Browse files
committed
Refactor IOP Utils class to handle module loading and guessing full path
1 parent 20cdaa3 commit 9255511

File tree

3 files changed

+35
-28
lines changed

3 files changed

+35
-28
lines changed

src/iop/_utils.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,9 @@ def register_component(module:str,classname:str,path:str,overwrite:int=1,iris_cl
5151
:return: The return value is a string.
5252
"""
5353
path = os.path.abspath(os.path.normpath(path))
54+
fullpath = _Utils.guess_path(module,path)
5455
try:
55-
iris.cls('IOP.Utils').dispatchRegisterComponent(module,classname,path,overwrite,iris_classname)
56+
iris.cls('IOP.Utils').dispatchRegisterComponent(module,classname,path,fullpath,overwrite,iris_classname)
5657
except RuntimeError as e:
5758
# New message error : Make sure the iop package is installed in iris
5859
raise RuntimeError("Iris class : IOP.Utils not found. Make sure the iop package is installed in iris eg: iop --init.") from e
@@ -395,4 +396,26 @@ def string_to_stream(string:str,buffer=1000000):
395396
chunks = [string[i:i+n] for i in range(0, len(string), n)]
396397
for chunk in chunks:
397398
stream.Write(chunk)
398-
return stream
399+
return stream
400+
401+
@staticmethod
402+
def guess_path(module,path):
403+
if "." in module:
404+
if module.startswith("."):
405+
# count the number of dots at the beginning of the module name
406+
dots = 0
407+
for c in module:
408+
if c == ".":
409+
dots += 1
410+
else:
411+
break
412+
# remove the dots from the beginning of the module name
413+
module = module[dots:]
414+
# go to the parent directory dots times
415+
for i in range(dots)-1:
416+
path = os.path.dirname(path)
417+
return os.path.join(path, module.replace(".", os.sep) + ".py")
418+
else:
419+
return os.path.join(path, module.replace(".", os.sep) + ".py")
420+
else:
421+
return os.path.join(path, module + ".py")

src/iop/cls/IOP/Utils.cls

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ ClassMethod dispatchRegisterComponent(
1111
pModule As %String,
1212
pRemoteClassname As %String,
1313
pCLASSPATHS As %String = "",
14+
pFullpath As %String = "",
1415
pOverwrite As %Boolean = 0,
1516
pProxyClassname As %String = "") As %Status
1617
{
1718
set tSc = $$$OK
18-
$$$ThrowOnError(##class(IOP.Utils).RegisterComponent(pModule, pRemoteClassname, pCLASSPATHS, pOverwrite , pProxyClassname))
19+
$$$ThrowOnError(##class(IOP.Utils).RegisterComponent(pModule, pRemoteClassname, pCLASSPATHS, pFullpath, pOverwrite , pProxyClassname))
1920
return tSc
2021
}
2122

@@ -24,6 +25,7 @@ ClassMethod RegisterComponent(
2425
pModule As %String,
2526
pRemoteClassname As %String,
2627
pCLASSPATHS As %String = "",
28+
pFullpath As %String = "",
2729
pOverwrite As %Boolean = 0,
2830
pProxyClassname As %String = "") As %Status
2931
{
@@ -38,7 +40,7 @@ ClassMethod RegisterComponent(
3840

3941
Try {
4042

41-
$$$ThrowOnError(..GetRemoteClassInfo(pRemoteClassname,pModule,pCLASSPATHS,.tClassDetails,.tRemoteSettings))
43+
$$$ThrowOnError(..GetRemoteClassInfo(pRemoteClassname,pModule,pCLASSPATHS,pFullpath,.tClassDetails,.tRemoteSettings))
4244

4345
Set tConnectionSettings("Classpaths") = pCLASSPATHS
4446
Set tConnectionSettings("Module") = pModule
@@ -103,6 +105,7 @@ ClassMethod GetRemoteClassInfo(
103105
pRemoteClassname As %String,
104106
pModule As %String,
105107
pClasspaths As %String,
108+
pFullpath As %String = "",
106109
ByRef pClassDetails,
107110
ByRef pRemoteSettings) As %Status [ Internal ]
108111
{
@@ -124,18 +127,15 @@ ClassMethod GetRemoteClassInfo(
124127

125128
set importlib = ##class(%SYS.Python).Import("importlib")
126129
set builtins = ##class(%SYS.Python).Import("builtins")
127-
set sys = ##class(%SYS.Python).Import("sys")
128-
set os = ##class(%SYS.Python).Import("os")
129130
// Load the module form a specific path
130-
// Guess the full path to the module
131-
set path = ..GuessFullPath(pModule, onePath)
132131
Try {
133-
set spec = importlib.util."spec_from_file_location"(pModule, path)
132+
set spec = importlib.util."spec_from_file_location"(pModule, pFullpath)
134133
set module = importlib.util."module_from_spec"(spec)
135134
do sys.modules."__setitem__"(pModule, module)
136135
do spec.loader."exec_module"(module)
137136
}
138137
Catch ex {
138+
// If the module is not found, try to import the frist one found
139139
set module = importlib."import_module"(pModule)
140140
}
141141

@@ -160,21 +160,6 @@ ClassMethod GetRemoteClassInfo(
160160
Quit tSC
161161
}
162162

163-
ClassMethod GuessFullPath(
164-
module As %String,
165-
path As %String) As %String
166-
{
167-
If $Find(module, ".") {
168-
Set module = $Piece(module, ".", *)
169-
}
170-
If $Find(path, module) {
171-
Set path = $Piece(path, module, 1)
172-
}
173-
// append the module to the path
174-
Set path = path _ module _ ".py"
175-
Return path
176-
}
177-
178163
ClassMethod GenerateProxyClass(
179164
pClassname As %String,
180165
ByRef pConnectionSettings,

src/tests/test_iop_utils.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def test_migrate_then_register():
4343
path = os.path.join(path, 'registerFilesIop')
4444
overwrite = 1
4545
iris_classname = 'UnitTest.EmailOperation'
46-
result = _Utils.register_component(module, classname, path, overwrite, iris_classname)
46+
_Utils.register_component(module, classname, path, overwrite, iris_classname)
4747

4848

4949
def test_register_component():
@@ -117,7 +117,7 @@ def test_set_classes_settings_by_classe():
117117

118118
from bo import EmailOperation
119119
CLASSES = { 'UnitTest.Package.EmailOperation': EmailOperation }
120-
_Utils.set_classes_settings(CLASSES)
120+
_Utils.set_classes_settings(CLASSES,path)
121121

122122
def test_set_classes_settings_by_classe_with_sub_module():
123123
# set python path to the registerFilesIop folder
@@ -127,8 +127,7 @@ def test_set_classes_settings_by_classe_with_sub_module():
127127

128128
from registerFilesIop.bo import EmailOperation
129129
CLASSES = { 'UnitTest.Package.EmailOperation': EmailOperation }
130-
_Utils.set_classes_settings(CLASSES)
131-
print(CLASSES)
130+
_Utils.set_classes_settings(CLASSES,path)
132131

133132
def test_set_classes_settings_by_class_with_rootpath():
134133
# set python path to the registerFilesIop folder

0 commit comments

Comments
 (0)