Skip to content

Commit e15b673

Browse files
committed
Refactor IOP Utils class to handle module loading and guessing full path
1 parent da54609 commit e15b673

File tree

2 files changed

+39
-12
lines changed

2 files changed

+39
-12
lines changed

src/iop/cls/IOP/Utils.cls

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -125,16 +125,24 @@ ClassMethod GetRemoteClassInfo(
125125
set importlib = ##class(%SYS.Python).Import("importlib")
126126
set builtins = ##class(%SYS.Python).Import("builtins")
127127
set sys = ##class(%SYS.Python).Import("sys")
128+
set os = ##class(%SYS.Python).Import("os")
128129
// Load the module form a specific path
129-
set spec = importlib.util."spec_from_file_location"(pModule, onePath_pModule_".py")
130-
set module = importlib.util."module_from_spec"(spec)
131-
do sys.modules."__setitem__"(pModule, module)
132-
do spec.loader."exec_module"(module)
130+
// Guess the full path to the module
131+
set path = ..GuessFullPath(pModule, onePath)
132+
Try {
133+
set spec = importlib.util."spec_from_file_location"(pModule, path)
134+
set module = importlib.util."module_from_spec"(spec)
135+
do sys.modules."__setitem__"(pModule, module)
136+
do spec.loader."exec_module"(module)
137+
}
138+
Catch ex {
139+
set module = importlib."import_module"(pModule)
140+
}
141+
133142
// Get the class
134143
set class = builtins.getattr(module, pRemoteClassname)
135144
set tClass = class."__new__"(class)
136145

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

155-
/// Set tConnectionSettings("Classpaths") = pCLASSPATHS
156-
/// Set tConnectionSettings("Module") = pModule
157-
/// Set tConnectionSettings("Classname") = pRemoteClassname
158-
/// Set:(""=pProxyClassname) pProxyClassname = pRemoteClassname
159-
///
160-
/// Set tSC = ..GenerateProxyClass(pProxyClassname,.tConnectionSettings,tClassDetails,tRemoteSettings,pOverwrite)
161-
/// "bo","Duplex","/irisdev/app/src/python/demo/duplex/",1,"Duplex.Duplex"
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+
162178
ClassMethod GenerateProxyClass(
163179
pClassname As %String,
164180
ByRef pConnectionSettings,

src/tests/test_iop_utils.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,17 @@ def test_set_classes_settings_by_classe():
119119
CLASSES = { 'UnitTest.Package.EmailOperation': EmailOperation }
120120
_Utils.set_classes_settings(CLASSES)
121121

122+
def test_set_classes_settings_by_classe_with_sub_module():
123+
# set python path to the registerFilesIop folder
124+
path = os.path.dirname(os.path.realpath(__file__))
125+
126+
sys.path.append(path)
127+
128+
from registerFilesIop.bo import EmailOperation
129+
CLASSES = { 'UnitTest.Package.EmailOperation': EmailOperation }
130+
_Utils.set_classes_settings(CLASSES)
131+
print(CLASSES)
132+
122133
def test_set_classes_settings_by_class_with_rootpath():
123134
# set python path to the registerFilesIop folder
124135
path = os.path.dirname(os.path.realpath(__file__))

0 commit comments

Comments
 (0)