Skip to content

Commit a6d3e8e

Browse files
committed
GetModuleInfo and ImportModule functions
1 parent 9e6914b commit a6d3e8e

File tree

1 file changed

+55
-2
lines changed

1 file changed

+55
-2
lines changed

isc/py/Main.cls

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,64 @@ ClassMethod GetVariableInfo(variable As %String = "", serialization As %Integer
157157
quit sc
158158
}
159159

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+
160213
/// Returns last occured exception in Python and clears it
161214
/// zwrite ##class(isc.py.Main).GetStatus()
162215
ClassMethod GetStatus() As %Status
163216
{
164-
do ##class(isc.py.Callout).SimpleString("import sys, traceback;")
217+
do ##class(isc.py.Callout).SimpleString("import sys, traceback")
165218
set haxExc = ##class(isc.py.Callout).SimpleString("zzzerr = hasattr(sys, 'last_type')", "zzzerr")
166219
do ##class(isc.py.Callout).SimpleString("del zzzerr")
167220
quit:haxExc="False" $$$OK
@@ -181,7 +234,7 @@ ClassMethod GetVariableJson(variable As %String, ByRef stream As %Stream.Object,
181234
quit:$$$ISERR(sc) sc
182235
quit:'defined $$$ERROR($$$GeneralError, "Variable '" _ variable _ "' is not defined")
183236

184-
do ##class(isc.py.Callout).SimpleString("import json;")
237+
do ##class(isc.py.Callout).SimpleString("import json")
185238
do ##class(isc.py.Callout).SimpleString("def zzzempty(obj):" _ $c(10) _ " return ''")
186239

187240
if type = "DataFrame" {

0 commit comments

Comments
 (0)