@@ -157,11 +157,64 @@ ClassMethod GetVariableInfo(variable As %String = "", serialization As %Integer
157
157
quit sc
158
158
}
159
159
160
+ /// Import module with alias.
161
+ /// If module is already imported, imported value would be set to 1 and alias to existing alias.
162
+ /// If module is already imported, actual import would be skipped
163
+ ClassMethod ImportModule (module As %String , Output imported As %Boolean , Output alias As %String ) As %Status
164
+ {
165
+ kill imported
166
+ set sc = ..GetModuleInfo (module , .imported , .existingAlias )
167
+ quit :$$$ISERR(sc ) sc
168
+
169
+ if imported {
170
+ set alias = existingAlias
171
+ } else {
172
+ if $g (alias )'=" " {
173
+ set sc = ..SimpleString (" import " _ module _ " as " _ alias )
174
+ } else {
175
+ set sc = ..SimpleString (" import " _ module )
176
+ set alias = module
177
+ }
178
+ }
179
+
180
+ quit sc
181
+ }
182
+
183
+ /// Get information about modules.
184
+ /// If it was imported without alias, then alias is the module name
185
+ /// set sc = ##class(isc.py.Main).GetModuleInfo("", .imported, .alias)
186
+ ClassMethod GetModuleInfo (module As %String , Output imported As %Boolean , Output alias As %String ) As %Status
187
+ {
188
+ #dim sc As %Status = $$$OK
189
+ kill imported , alias
190
+
191
+ quit :module =" " $$$ERROR($$$GeneralError, " Module argument should be set" )
192
+ set imported = ##class (isc.py.Callout ).SimpleString (" zzzdef='" _ module _ " ' in dir()" , " zzzdef" )
193
+ set imported = $case (imported , " True" :$$$YES, " False" :$$$NO, :" " )
194
+
195
+ if imported {
196
+ do ..SimpleString (" import types" )
197
+ do ##class (isc.py.Callout ).SimpleString (" def zzzgetalias(module):" _ $c (10 ) _
198
+ " for name, val in globals().items():" _ $c (10 ) _
199
+ " if isinstance(val, types.ModuleType):" _ $c (10 ) _
200
+ " if val.__name__ == module:" _ $c (10 ) _
201
+ " return name" _ $c (10 ) _
202
+ " return None" )
203
+
204
+ set alias = ##class (isc.py.Callout ).SimpleString (" zzzalias=zzzgetalias('" _ module _ " ')" , " zzzalias" )
205
+ do ..SimpleString (" del zzzalias" )
206
+ }
207
+
208
+ do ..SimpleString (" del zzzdef" )
209
+ set sc = ..GetStatus ()
210
+ quit sc
211
+ }
212
+
160
213
/// Returns last occured exception in Python and clears it
161
214
/// zwrite ##class(isc.py.Main).GetStatus()
162
215
ClassMethod GetStatus () As %Status
163
216
{
164
- do ##class (isc.py.Callout ).SimpleString (" import sys, traceback; " )
217
+ do ##class (isc.py.Callout ).SimpleString (" import sys, traceback" )
165
218
set haxExc = ##class (isc.py.Callout ).SimpleString (" zzzerr = hasattr(sys, 'last_type')" , " zzzerr" )
166
219
do ##class (isc.py.Callout ).SimpleString (" del zzzerr" )
167
220
quit :haxExc =" False" $$$OK
@@ -181,7 +234,7 @@ ClassMethod GetVariableJson(variable As %String, ByRef stream As %Stream.Object,
181
234
quit :$$$ISERR(sc ) sc
182
235
quit :'defined $$$ERROR($$$GeneralError, " Variable '" _ variable _ " ' is not defined" )
183
236
184
- do ##class (isc.py.Callout ).SimpleString (" import json; " )
237
+ do ##class (isc.py.Callout ).SimpleString (" import json" )
185
238
do ##class (isc.py.Callout ).SimpleString (" def zzzempty(obj):" _ $c (10 ) _ " return ''" )
186
239
187
240
if type = " DataFrame" {
0 commit comments