Skip to content
This repository has been archived by the owner on Sep 27, 2023. It is now read-only.

Add json format output for modhelp #995

Merged
merged 3 commits into from
Sep 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/Command/Modhelp.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
# Synopsis

~~~
bash$ gridlabd --modhelp module[:class]
bash$ gridlabd --modhelp[=json] module[:class]
~~~

# Description

Display structure of a class or all classes in a module.
Display structure of a class or all classes in a module. If the `=json` option is included, the output is generated in JSON format.

The comment section includes whether the property is a required input, optional, dynamic, output, or deprecated.

Expand Down
12 changes: 10 additions & 2 deletions gldcore/cmdarg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1066,9 +1066,17 @@ int GldCmdarg::modhelp(int argc, const char *argv[])
*/
return FAILED;
}
if ( options && strcmp(options,"md") == 0 )
if ( options )
{
module_help_md(mod,oclass);
if ( strcmp(options,"md") == 0 )
{
module_help_md(mod,oclass);
}
else if ( strcmp(options,"json") == 0 )
{
GldJsonWriter writer("/dev/stdout");
return writer.dump_modules() > 0 ? 1 : CMDERR;
}
}
else if ( oclass != NULL )
{
Expand Down
50 changes: 36 additions & 14 deletions gldcore/json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ int GldJsonWriter::write_properties(FILE *fp)
}


int GldJsonWriter::write_classes(FILE *fp)
int GldJsonWriter::write_classes(FILE *fp,bool noattr)
{
int len = 0;

Expand All @@ -206,23 +206,33 @@ int GldJsonWriter::write_classes(FILE *fp)
PROPERTY *prop;
if ( oclass != class_get_first_class() )
len += write(",");
len += write("\n\t\t\"%s\" : {",oclass->name);
FIRST("object_size","%u",oclass->size);
if ( oclass->parent )
if ( ! noattr || oclass->module == NULL )
{
TUPLE("parent","%s",oclass->parent->name );
len += write("\n\t\t\"%s\" : {",oclass->name);
}
TUPLE("trl","%u",oclass->trl);
if ( oclass->module )
else
{
TUPLE("module","%s",oclass->module->name);
len += write("\n\t\t\"%s.%s\" : {",oclass->module->name,oclass->name);
}
if ( ! noattr )
{
FIRST("object_size","%u",oclass->size);
if ( oclass->parent )
{
TUPLE("parent","%s",oclass->parent->name );
}
TUPLE("trl","%u",oclass->trl);
if ( oclass->module )
{
TUPLE("module","%s",oclass->module->name);
}
TUPLE("profiler.numobjs","%u",oclass->profiler.numobjs);
TUPLE("profiler.clocks","%llu",oclass->profiler.clocks);
TUPLE("profiler.count","%u",oclass->profiler.count);
if ( oclass->has_runtime ) TUPLE("runtime","%s",oclass->runtime);
if ( oclass->pmap != NULL )
len += write(",");
}
TUPLE("profiler.numobjs","%u",oclass->profiler.numobjs);
TUPLE("profiler.clocks","%llu",oclass->profiler.clocks);
TUPLE("profiler.count","%u",oclass->profiler.count);
if ( oclass->has_runtime ) TUPLE("runtime","%s",oclass->runtime);
if ( oclass->pmap != NULL )
len += write(",");
for ( prop = oclass->pmap ; prop != NULL && prop->oclass == oclass ; prop=prop->next ) // note: do not output parent classes properties
{
KEYWORD *key;
Expand Down Expand Up @@ -726,6 +736,18 @@ int GldJsonWriter::dump()
return len > 0;
}

int GldJsonWriter::dump_modules()
{
int len = 0;
json = stdout;
len += write("{\t\"application\": \"gridlabd\",\n");
len += write("\t\"version\" : \"%u.%u.%u\"",global_version_major,global_version_minor,global_version_patch);
len += write_modules(json);
len += write_classes(json,true);
len += write("\n}\n");
return len;
}

int json_to_glm(const char *jsonfile, char *glmfile)
{
// TODO: convert JSON file to GLM
Expand Down
3 changes: 2 additions & 1 deletion gldcore/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@ class GldJsonWriter
~GldJsonWriter(void);
public:
int dump();
int dump_modules();
int write_output(FILE *fp);
private:
int write_modules(FILE *fp);
int write_properties(FILE *fp);
int write_classes(FILE *fp);
int write_classes(FILE *fp,bool noattr=false);
int write_globals(FILE *fp);
int write_objects(FILE *fp);
int write_schedules(FILE *fp);
Expand Down