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 13, 2024
1 parent da54609 commit e15b673
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 12 deletions.
40 changes: 28 additions & 12 deletions src/iop/cls/IOP/Utils.cls
Original file line number Diff line number Diff line change
Expand Up @@ -125,16 +125,24 @@ 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
set spec = importlib.util."spec_from_file_location"(pModule, onePath_pModule_".py")
set module = importlib.util."module_from_spec"(spec)
do sys.modules."__setitem__"(pModule, module)
do spec.loader."exec_module"(module)
// Guess the full path to the module
set path = ..GuessFullPath(pModule, onePath)
Try {
set spec = importlib.util."spec_from_file_location"(pModule, path)
set module = importlib.util."module_from_spec"(spec)
do sys.modules."__setitem__"(pModule, module)
do spec.loader."exec_module"(module)
}
Catch ex {
set module = importlib."import_module"(pModule)
}

// Get the class
set class = builtins.getattr(module, pRemoteClassname)
set tClass = class."__new__"(class)


If $IsObject(tClass) {
#; List of information about the class as a whole - $lb(SuperClass, Description, InfoURL, IconURL, Adapter)
Set pClassDetails = tClass."_get_info"()
Expand All @@ -152,13 +160,21 @@ ClassMethod GetRemoteClassInfo(
Quit tSC
}

/// Set tConnectionSettings("Classpaths") = pCLASSPATHS
/// Set tConnectionSettings("Module") = pModule
/// Set tConnectionSettings("Classname") = pRemoteClassname
/// Set:(""=pProxyClassname) pProxyClassname = pRemoteClassname
///
/// Set tSC = ..GenerateProxyClass(pProxyClassname,.tConnectionSettings,tClassDetails,tRemoteSettings,pOverwrite)
/// "bo","Duplex","/irisdev/app/src/python/demo/duplex/",1,"Duplex.Duplex"
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
11 changes: 11 additions & 0 deletions src/tests/test_iop_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,17 @@ def test_set_classes_settings_by_classe():
CLASSES = { 'UnitTest.Package.EmailOperation': EmailOperation }
_Utils.set_classes_settings(CLASSES)

def test_set_classes_settings_by_classe_with_sub_module():
# set python path to the registerFilesIop folder
path = os.path.dirname(os.path.realpath(__file__))

sys.path.append(path)

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

def test_set_classes_settings_by_class_with_rootpath():
# set python path to the registerFilesIop folder
path = os.path.dirname(os.path.realpath(__file__))
Expand Down

0 comments on commit e15b673

Please sign in to comment.