Skip to content

Commit da9a91b

Browse files
committed
Refactor code and add _dispatch_on_get_connections method
1 parent d8ed21d commit da9a91b

File tree

2 files changed

+146
-75
lines changed

2 files changed

+146
-75
lines changed

src/grongier/cls/Grongier/PEX/Common.cls

Lines changed: 114 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -22,31 +22,31 @@ Property %class As %SYS.Python;
2222
/// Get Class
2323
Method GetClass() As %SYS.Python
2424
{
25-
Return ..%class
25+
Return ..%class
2626
}
2727

2828
/// Get Classname
2929
Method GetClassname() As %String
3030
{
31-
Return ..%classname
31+
Return ..%classname
3232
}
3333

3434
/// Get Classname
3535
Method GetModule() As %String
3636
{
37-
Return ..%module
37+
Return ..%module
3838
}
3939

4040
Method OnInit() As %Status
4141
{
42-
set tSC = $$$OK
43-
try {
42+
set tSC = $$$OK
43+
try {
4444
$$$ThrowOnError(..Connect())
45-
do ..%class."_dispatch_on_init"($this)
46-
} catch ex {
47-
set tSC = ex.AsStatus()
48-
}
49-
quit tSC
45+
do ..%class."_dispatch_on_init"($this)
46+
} catch ex {
47+
set tSC = ex.AsStatus()
48+
}
49+
quit tSC
5050
}
5151

5252
ClassMethod SetPythonPath(pClasspaths)
@@ -69,73 +69,73 @@ ClassMethod SetPythonPath(pClasspaths)
6969

7070
Method Connect() As %Status
7171
{
72-
set tSC = $$$OK
73-
try {
74-
75-
set container = $this
76-
77-
//set classpass
78-
if ..%classpaths '="" {
79-
set delimiter = $s($system.Version.GetOS()="Windows":";",1:":")
80-
set extraClasspaths = $tr(container.%classpaths,delimiter,"|")
81-
for i=1:1:$l(extraClasspaths,"|") {
82-
set onePath = $p(extraClasspaths,"|",i)
83-
set onePath = ##class(%File).NormalizeDirectory(onePath)
84-
do ..SetPythonPath(onePath)
85-
}
86-
}
87-
if $isObject(..%class)=0 {
88-
set importlib = ##class(%SYS.Python).Import("importlib")
89-
set builtins = ##class(%SYS.Python).Import("builtins")
90-
set module = importlib."import_module"(..%module)
91-
set class = builtins.getattr(module, ..%classname)
92-
set ..%class = class."__new__"(class)
93-
}
94-
;
95-
if ..%Extends("Grongier.PEX.InboundAdapter") || ..%Extends("Grongier.PEX.OutboundAdapter") {
96-
do ..%class."_set_iris_handles"($this,..BusinessHost)
97-
} elseif $this.%Extends("Grongier.PEX.BusinessProcess") {
98-
do ..%class."_set_iris_handles"($this,$$$NULLOREF)
99-
} else {
100-
do ..%class."_set_iris_handles"($this,..Adapter)
101-
}
102-
;
103-
do ..SetPropertyValues()
104-
;
105-
try {
106-
do ..%class."_dispatch_on_connected"($this)
107-
} catch ex {
108-
$$$LOGWARNING(ex.DisplayString())
109-
}
110-
;
111-
} catch ex {
112-
set msg = $System.Status.GetOneStatusText(ex.AsStatus(),1)
113-
set tSC = $$$ERROR($$$EnsErrGeneral,msg)
114-
}
115-
quit tSC
72+
set tSC = $$$OK
73+
try {
74+
75+
set container = $this
76+
77+
//set classpass
78+
if ..%classpaths '="" {
79+
set delimiter = $s($system.Version.GetOS()="Windows":";",1:":")
80+
set extraClasspaths = $tr(container.%classpaths,delimiter,"|")
81+
for i=1:1:$l(extraClasspaths,"|") {
82+
set onePath = $p(extraClasspaths,"|",i)
83+
set onePath = ##class(%File).NormalizeDirectory(onePath)
84+
do ..SetPythonPath(onePath)
85+
}
86+
}
87+
if $isObject(..%class)=0 {
88+
set importlib = ##class(%SYS.Python).Import("importlib")
89+
set builtins = ##class(%SYS.Python).Import("builtins")
90+
set module = importlib."import_module"(..%module)
91+
set class = builtins.getattr(module, ..%classname)
92+
set ..%class = class."__new__"(class)
93+
}
94+
;
95+
if ..%Extends("Grongier.PEX.InboundAdapter") || ..%Extends("Grongier.PEX.OutboundAdapter") {
96+
do ..%class."_set_iris_handles"($this,..BusinessHost)
97+
} elseif $this.%Extends("Grongier.PEX.BusinessProcess") {
98+
do ..%class."_set_iris_handles"($this,$$$NULLOREF)
99+
} else {
100+
do ..%class."_set_iris_handles"($this,..Adapter)
101+
}
102+
;
103+
do ..SetPropertyValues()
104+
;
105+
try {
106+
do ..%class."_dispatch_on_connected"($this)
107+
} catch ex {
108+
$$$LOGWARNING(ex.DisplayString())
109+
}
110+
;
111+
} catch ex {
112+
set msg = $System.Status.GetOneStatusText(ex.AsStatus(),1)
113+
set tSC = $$$ERROR($$$EnsErrGeneral,msg)
114+
}
115+
quit tSC
116116
}
117117

118118
Method OnTearDown() As %Status
119119
{
120-
set tSC = $$$OK
120+
set tSC = $$$OK
121121
do ..%class."_dispatch_on_tear_down"()
122-
quit tSC
122+
quit tSC
123123
}
124124

125125
Method SetPropertyValues()
126126
{
127-
set remoteSettings = $tr(..%settings,$c(13))
128-
for i=1:1:$l(remoteSettings,$c(10)) {
129-
set oneLine = $p(remoteSettings,$c(10),i)
130-
set property = $p(oneLine,"=",1) continue:property=""
131-
set value = $p(oneLine,"=",2,*)
132-
try {
133-
set $property(..%class,property) = value
134-
} catch ex {
135-
$$$LOGWARNING(ex.DisplayString())
136-
}
137-
}
138-
quit
127+
set remoteSettings = $tr(..%settings,$c(13))
128+
for i=1:1:$l(remoteSettings,$c(10)) {
129+
set oneLine = $p(remoteSettings,$c(10),i)
130+
set property = $p(oneLine,"=",1) continue:property=""
131+
set value = $p(oneLine,"=",2,*)
132+
try {
133+
set $property(..%class,property) = value
134+
} catch ex {
135+
$$$LOGWARNING(ex.DisplayString())
136+
}
137+
}
138+
quit
139139
}
140140

141141
Method dispatchSendRequestSync(
@@ -144,19 +144,59 @@ Method dispatchSendRequestSync(
144144
timeout,
145145
description) As %String
146146
{
147-
set tSC = ..SendRequestSync(target,request,.objResponse,timeout,description)
148-
if $$$ISERR(tSC) throw ##class(%Exception.StatusException).CreateFromStatus(tSC)
149-
quit $g(objResponse)
147+
set tSC = ..SendRequestSync(target,request,.objResponse,timeout,description)
148+
if $$$ISERR(tSC) throw ##class(%Exception.StatusException).CreateFromStatus(tSC)
149+
quit $g(objResponse)
150150
}
151151

152152
Method dispatchSendRequestAsync(
153153
target,
154154
request,
155155
description)
156156
{
157-
set tSC = ..SendRequestAsync(target,request,description)
158-
if $$$ISERR(tSC) throw ##class(%Exception.StatusException).CreateFromStatus(tSC)
159-
quit
157+
set tSC = ..SendRequestAsync(target,request,description)
158+
if $$$ISERR(tSC) throw ##class(%Exception.StatusException).CreateFromStatus(tSC)
159+
quit
160+
}
161+
162+
ClassMethod OnGetConnections(
163+
Output pArray As %String,
164+
pItem As Ens.Config.Item)
165+
{
166+
// finds any settings of type Ens.DataType.ConfigName
167+
do ..GetPropertyConnections(.pArray,pItem)
168+
169+
// Get settings
170+
do pItem.GetModifiedSetting("%classpaths", .tClasspaths)
171+
do pItem.GetModifiedSetting("%classname", .tClassname)
172+
do pItem.GetModifiedSetting("%module", .tModule)
173+
174+
// try to instantiate class
175+
if tClasspaths '="" {
176+
set sys = ##class(%SYS.Python).Import("sys")
177+
set delimiter = $s($system.Version.GetOS()="Windows":";",1:":")
178+
set extraClasspaths = $tr(tClasspaths,delimiter,"|")
179+
for i=1:1:$l(extraClasspaths,"|") {
180+
set onePath = $p(extraClasspaths,"|",i)
181+
set onePath = ##class(%File).NormalizeDirectory(onePath)
182+
if onePath?1"$$IRISHOME"1P.E set onePath = $e($system.Util.InstallDirectory(),1,*-1)_$e(onePath,11,*)
183+
if onePath'="" do sys.path.append(onePath)
184+
}
185+
}
186+
set importlib = ##class(%SYS.Python).Import("importlib")
187+
set builtins = ##class(%SYS.Python).Import("builtins")
188+
set module = importlib."import_module"(tModule)
189+
set class = builtins.getattr(module, tClassname)
190+
set tClass = class."__new__"(class)
191+
192+
set tPythonList = tClass."_dispatch_on_get_connections"()
193+
set tPythonListLen = tPythonList."__len__"()
194+
for i=0:1:(tPythonListLen-1) {
195+
set tPythonItem = tPythonList."__getitem__"(i)
196+
set pArray(tPythonItem) = ""
197+
}
198+
199+
quit
160200
}
161201

162202
}

src/grongier/pex/_business_host.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import importlib
99
import iris
1010

11-
from inspect import signature
11+
from inspect import signature, getsource
1212

1313
from dacite import from_dict, Config
1414

@@ -21,6 +21,7 @@ class _BusinessHost(_Common):
2121
"""
2222

2323
buffer:int = 64000
24+
DISPATCH = []
2425

2526
def input_serialzer(fonction):
2627
"""
@@ -352,6 +353,36 @@ def get_adapter_type():
352353
Name of the registred Adapter
353354
"""
354355
return
356+
357+
def _dispatch_on_get_connections(self) -> list:
358+
"""
359+
The OnGetConnections() method returns all of the targets of any SendRequestSync or SendRequestAsync
360+
calls for the class. Implement this method to allow connections between components to show up in
361+
the interoperability UI.
362+
363+
Returns:
364+
An IRISList containing all targets for this class. Default is None.
365+
"""
366+
## Parse the class code to find all invocations of send_request_sync and send_request_async
367+
## and return the targets
368+
targer_list = []
369+
# get the source code of the class
370+
source = getsource(self.__class__)
371+
# find all invocations of send_request_sync and send_request_async
372+
for method in ['send_request_sync','send_request_async']:
373+
i = source.find(method)
374+
while i != -1:
375+
j = source.find("(",i)
376+
if j != -1:
377+
k = source.find(",",j)
378+
if k != -1:
379+
target = source[j+1:k]
380+
# trim " and ' from target
381+
target = target.strip('\'').strip('\"')
382+
if target not in targer_list:
383+
targer_list.append(target)
384+
i = source.find(method,i+1)
385+
return targer_list
355386

356387
# It's a subclass of the standard JSONEncoder class that knows how to encode date/time, decimal types,
357388
# and UUIDs.

0 commit comments

Comments
 (0)