I just thought I'd share this script which lists all of the functions included within a script library. I'm sure it could be improved (open to enhancements!) like including the list of arguments, and not using a string for the ScriptManager type... But it works for my purpose.
from com.inductiveautomation.ignition.designer.gui.tools.jythonconsole import JythonConsole
SYSTEM_FUNC_TYPE_STR = "<type 'com.inductiveautomation.ignition.common.script.ScriptManager$ReflectedInstanceFunction'>"
FUNC_TYPE_STR = "<type 'function'>"
def pyDocumenter(library, parentLibStr=''):
"""
This function prints the paths to all functions included within a script library.
Example call:
pyDocumenter(system)
"""
if parentLibStr == '':
parentLibStr = library.name + '.'
for obj_str in dir(library):
obj = getattr(library, obj_str)
obj_t_str = str(type(obj))
# loop through script packages
if isinstance(obj, JythonConsole.ConsolePackage):
pyDocumenter(obj, parentLibStr + obj_str + '.')
# document user functions
elif isinstance(obj, JythonConsole.ConsoleModule):
for mod_obj_str in dir(obj):
mod_obj = getattr(obj, mod_obj_str)
mod_obj_t_str = str(type(mod_obj))
if mod_obj_t_str in (FUNC_TYPE_STR, SYSTEM_FUNC_TYPE_STR):
print '{}{}.{}(...)'.format(parentLibStr, obj_str, mod_obj_str)
# document `system` functions
elif obj_t_str == SYSTEM_FUNC_TYPE_STR:
print '{}{}(...)'.format(parentLibStr, obj_str)
pyDocumenter(shared)
Maybe build an AST from the script code, get def tokens, and from there you can get arguments easily.
The ast module and its NodeVisitor class makes this simpler than it sounds.
I moved a number of script functions around, reorganising, and wanted to have a lookup from old location to new, without having to manually document it. So I could then manually refactor their references with find replace
Hmm, I did this already but for script library functions they don’t have __code__ or func_code which apparently will give you the arg names. Inspect also doesn’t like them because they’re not python functions but JythonConsole objects
Any way to convert them to py function objects?
I found it strange also that the standard system library functions are different objects to user script library functions.
I also found it odd you can get the entire function definition for user functions as a string with function.code. That might come in handy one day, but then again, that’s what source control on the files themselves is for, so maybe not
Ok, I get the same thing, but it's when I use getattr(inspect, 'introspect') that it becomes a JythonConsole object which no longer has func_code
Should that not return the same type of object?
Wait...... I stuffed up... I didn't go deep enough - my script only reports script modules, not the functions themselves.
Paul, does this just get me the library names so I can run them through my function, or does this do more than that? (this is still useful, so thanks!)
Each entry in cache is a CompletionDescriptor, which has a variety of meta information, including a type. That type value (if it's not null) is itself a rich type description, with various attributes; e.g. name, description, and also a set of methods, attributes, and items (as in dictionary keys).
Each of those members are themselves instances of CompletionDescriptor, so they also might have a type (if the parsing is smart enough).
I expect TypeDescriptor & CompletionDescriptor to be the backbone of how script hints are populated in 8.3, @pturmel Currently the designer has to do some fairly fragile parsing of the HTML output of the PropertiesFileDocProvider to turn it into something useful, which is obviously limited in what it can do. Having proper return types from system functions would be a big bonus.