Skip to content

Commit f3fd60d

Browse files
committed
Enhance Python class initialization and connection handling; refactor methods for clarity and maintainability
1 parent c82a4df commit f3fd60d

File tree

2 files changed

+70
-46
lines changed

2 files changed

+70
-46
lines changed

requirements-dev.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,7 @@ jsonpath-ng
1010
pydantic>=2.0.0
1111
mkdocs
1212
pymdown-extensions
13-
debugpy
13+
debugpy
14+
openai
15+
https://github.com/intersystems-community/intersystems-irispython/releases/download/3.9.3/intersystems_iris-3.9.3-py3-none-any.whl
16+

src/iop/cls/IOP/Common.cls

Lines changed: 66 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -164,58 +164,22 @@ Method Connect() As %Status
164164
{
165165
set tSC = $$$OK
166166
try {
167+
// Initialize Python class instance
168+
$$$ThrowOnError(..InitializePythonClass())
167169

168-
set container = $this
169-
170-
// Before anything, try to check if the module is already present in the sys.modules
171-
set sys = ##class(%SYS.Python).Import("sys")
172-
set moduleName = ..%module
173-
set module = sys.modules.get(moduleName, $$$NULLOREF)
174-
if $isObject(module) {
175-
$$$LOGINFO("Module "_moduleName_" is already imported in sys.modules")
176-
// If the module is already present, we can skip the import
177-
set builtins = ##class(%SYS.Python).Import("builtins")
178-
set class = builtins.getattr(module, ..%classname)
179-
set ..%class = class."__new__"(class)
180-
}
181-
else {
182-
183-
//set classpass
184-
if ..%classpaths '="" {
185-
set delimiter = $s($system.Version.GetOS()="Windows":";",1:":")
186-
set extraClasspaths = $tr(container.%classpaths,delimiter,"|")
187-
for i=1:1:$l(extraClasspaths,"|") {
188-
set onePath = $p(extraClasspaths,"|",i)
189-
set onePath = ##class(%File).NormalizeDirectory(onePath)
190-
do ..SetPythonPath(onePath)
191-
}
192-
}
193-
194-
if $isObject(..%class)=0 {
195-
set builtins = ##class(%SYS.Python).Import("builtins")
196-
set module = ##class(%SYS.Python).Import(..%module)
197-
set class = builtins.getattr(module, ..%classname)
198-
set ..%class = class."__new__"(class)
199-
}
200-
}
201-
202-
;
203-
if ..%Extends("IOP.InboundAdapter") || ..%Extends("IOP.OutboundAdapter") {
204-
do ..%class."_set_iris_handles"($this,..BusinessHost)
205-
} elseif $this.%Extends("IOP.BusinessProcess") {
206-
do ..%class."_set_iris_handles"($this,$$$NULLOREF)
207-
} else {
208-
do ..%class."_set_iris_handles"($this,..Adapter)
209-
}
210-
;
170+
// Set IRIS handles based on component type
171+
do ..SetIrisHandles()
172+
173+
// Apply property values to Python instance
211174
do ..SetPropertyValues()
212-
;
175+
176+
// Notify Python class of connection
213177
try {
214178
do ..%class."_dispatch_on_connected"($this)
215179
} catch ex {
216180
$$$LOGWARNING(ex.DisplayString())
217181
}
218-
;
182+
219183
} catch ex {
220184
set msg = $System.Status.GetOneStatusText(ex.AsStatus(),1)
221185
set tSC = $$$ERROR($$$EnsErrGeneral,msg)
@@ -545,4 +509,61 @@ Method OnMsgGeneratorPoll(
545509
quit tSC
546510
}
547511

512+
Method InitializePythonClass() As %Status [ Private ]
513+
{
514+
set tSC = $$$OK
515+
try {
516+
// Check if module is already imported
517+
set sys = ##class(%SYS.Python).Import("sys")
518+
set module = sys.modules.get(..%module, $$$NULLOREF)
519+
520+
if $isObject(module) {
521+
$$$LOGINFO("Module "_..%module_" is already imported in sys.modules")
522+
set ..%class = ..CreateClassInstance(module)
523+
} else {
524+
// Setup classpaths if specified
525+
if ..%classpaths '= "" {
526+
do ..SetupClasspaths()
527+
}
528+
529+
// Import module and create class instance
530+
set module = ##class(%SYS.Python).Import(..%module)
531+
set ..%class = ..CreateClassInstance(module)
532+
}
533+
} catch ex {
534+
set tSC = ex.AsStatus()
535+
}
536+
quit tSC
537+
}
538+
539+
Method CreateClassInstance(module As %SYS.Python) As %SYS.Python [ Private ]
540+
{
541+
set builtins = ##class(%SYS.Python).Import("builtins")
542+
set class = builtins.getattr(module, ..%classname)
543+
quit class."__new__"(class)
544+
}
545+
546+
Method SetupClasspaths() [ Private ]
547+
{
548+
set delimiter = $s($system.Version.GetOS()="Windows":";",1:":")
549+
set extraClasspaths = $tr(..%classpaths, delimiter, "|")
550+
551+
for i=1:1:$l(extraClasspaths,"|") {
552+
set onePath = $p(extraClasspaths,"|",i)
553+
set onePath = ##class(%File).NormalizeDirectory(onePath)
554+
do ..SetPythonPath(onePath)
555+
}
556+
}
557+
558+
Method SetIrisHandles() [ Private ]
559+
{
560+
if ..%Extends("IOP.InboundAdapter") || ..%Extends("IOP.OutboundAdapter") {
561+
do ..%class."_set_iris_handles"($this, ..BusinessHost)
562+
} elseif $this.%Extends("IOP.BusinessProcess") {
563+
do ..%class."_set_iris_handles"($this, $$$NULLOREF)
564+
} else {
565+
do ..%class."_set_iris_handles"($this, ..Adapter)
566+
}
567+
}
568+
548569
}

0 commit comments

Comments
 (0)