Skip to content

Commit

Permalink
Refactor IOP Utils class to handle module loading and guessing full path
Browse files Browse the repository at this point in the history
  • Loading branch information
grongierisc committed Sep 16, 2024
1 parent 20cdaa3 commit 9255511
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 28 deletions.
27 changes: 25 additions & 2 deletions src/iop/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ def register_component(module:str,classname:str,path:str,overwrite:int=1,iris_cl
:return: The return value is a string.
"""
path = os.path.abspath(os.path.normpath(path))
fullpath = _Utils.guess_path(module,path)
try:
iris.cls('IOP.Utils').dispatchRegisterComponent(module,classname,path,overwrite,iris_classname)
iris.cls('IOP.Utils').dispatchRegisterComponent(module,classname,path,fullpath,overwrite,iris_classname)
except RuntimeError as e:
# New message error : Make sure the iop package is installed in iris
raise RuntimeError("Iris class : IOP.Utils not found. Make sure the iop package is installed in iris eg: iop --init.") from e
Expand Down Expand Up @@ -395,4 +396,26 @@ def string_to_stream(string:str,buffer=1000000):
chunks = [string[i:i+n] for i in range(0, len(string), n)]
for chunk in chunks:
stream.Write(chunk)
return stream
return stream

@staticmethod
def guess_path(module,path):
if "." in module:
if module.startswith("."):
# count the number of dots at the beginning of the module name
dots = 0
for c in module:
if c == ".":
dots += 1
else:
break
# remove the dots from the beginning of the module name
module = module[dots:]
# go to the parent directory dots times
for i in range(dots)-1:
path = os.path.dirname(path)
return os.path.join(path, module.replace(".", os.sep) + ".py")
else:
return os.path.join(path, module.replace(".", os.sep) + ".py")
else:
return os.path.join(path, module + ".py")
29 changes: 7 additions & 22 deletions src/iop/cls/IOP/Utils.cls
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ ClassMethod dispatchRegisterComponent(
pModule As %String,
pRemoteClassname As %String,
pCLASSPATHS As %String = "",
pFullpath As %String = "",
pOverwrite As %Boolean = 0,
pProxyClassname As %String = "") As %Status
{
set tSc = $$$OK
$$$ThrowOnError(##class(IOP.Utils).RegisterComponent(pModule, pRemoteClassname, pCLASSPATHS, pOverwrite , pProxyClassname))
$$$ThrowOnError(##class(IOP.Utils).RegisterComponent(pModule, pRemoteClassname, pCLASSPATHS, pFullpath, pOverwrite , pProxyClassname))
return tSc
}

Expand All @@ -24,6 +25,7 @@ ClassMethod RegisterComponent(
pModule As %String,
pRemoteClassname As %String,
pCLASSPATHS As %String = "",
pFullpath As %String = "",
pOverwrite As %Boolean = 0,
pProxyClassname As %String = "") As %Status
{
Expand All @@ -38,7 +40,7 @@ ClassMethod RegisterComponent(

Try {

$$$ThrowOnError(..GetRemoteClassInfo(pRemoteClassname,pModule,pCLASSPATHS,.tClassDetails,.tRemoteSettings))
$$$ThrowOnError(..GetRemoteClassInfo(pRemoteClassname,pModule,pCLASSPATHS,pFullpath,.tClassDetails,.tRemoteSettings))

Set tConnectionSettings("Classpaths") = pCLASSPATHS
Set tConnectionSettings("Module") = pModule
Expand Down Expand Up @@ -103,6 +105,7 @@ ClassMethod GetRemoteClassInfo(
pRemoteClassname As %String,
pModule As %String,
pClasspaths As %String,
pFullpath As %String = "",
ByRef pClassDetails,
ByRef pRemoteSettings) As %Status [ Internal ]
{
Expand All @@ -124,18 +127,15 @@ ClassMethod GetRemoteClassInfo(

set importlib = ##class(%SYS.Python).Import("importlib")
set builtins = ##class(%SYS.Python).Import("builtins")
set sys = ##class(%SYS.Python).Import("sys")
set os = ##class(%SYS.Python).Import("os")
// Load the module form a specific path
// Guess the full path to the module
set path = ..GuessFullPath(pModule, onePath)
Try {
set spec = importlib.util."spec_from_file_location"(pModule, path)
set spec = importlib.util."spec_from_file_location"(pModule, pFullpath)
set module = importlib.util."module_from_spec"(spec)
do sys.modules."__setitem__"(pModule, module)
do spec.loader."exec_module"(module)
}
Catch ex {
// If the module is not found, try to import the frist one found
set module = importlib."import_module"(pModule)
}

Expand All @@ -160,21 +160,6 @@ ClassMethod GetRemoteClassInfo(
Quit tSC
}

ClassMethod GuessFullPath(
module As %String,
path As %String) As %String
{
If $Find(module, ".") {
Set module = $Piece(module, ".", *)
}
If $Find(path, module) {
Set path = $Piece(path, module, 1)
}
// append the module to the path
Set path = path _ module _ ".py"
Return path
}

ClassMethod GenerateProxyClass(
pClassname As %String,
ByRef pConnectionSettings,
Expand Down
7 changes: 3 additions & 4 deletions src/tests/test_iop_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def test_migrate_then_register():
path = os.path.join(path, 'registerFilesIop')
overwrite = 1
iris_classname = 'UnitTest.EmailOperation'
result = _Utils.register_component(module, classname, path, overwrite, iris_classname)
_Utils.register_component(module, classname, path, overwrite, iris_classname)


def test_register_component():
Expand Down Expand Up @@ -117,7 +117,7 @@ def test_set_classes_settings_by_classe():

from bo import EmailOperation
CLASSES = { 'UnitTest.Package.EmailOperation': EmailOperation }
_Utils.set_classes_settings(CLASSES)
_Utils.set_classes_settings(CLASSES,path)

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

from registerFilesIop.bo import EmailOperation
CLASSES = { 'UnitTest.Package.EmailOperation': EmailOperation }
_Utils.set_classes_settings(CLASSES)
print(CLASSES)
_Utils.set_classes_settings(CLASSES,path)

def test_set_classes_settings_by_class_with_rootpath():
# set python path to the registerFilesIop folder
Expand Down

0 comments on commit 9255511

Please sign in to comment.