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

Commit

Permalink
Fix json-get script (#957)
Browse files Browse the repository at this point in the history
* Fix json-get script

* Update Json-get.md

* Update gridlabd-json-get

Co-authored-by: Alyona Teyber <[email protected]>
  • Loading branch information
David P. Chassin and aivanova5 authored Jul 31, 2021
1 parent d7af229 commit 5627921
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 20 deletions.
6 changes: 4 additions & 2 deletions docs/Subcommand/Json-get.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
GLM:

~~~
#json-get [keys ...] [-k|--keys] [-j|--json|-r|--raw|-r|--csv] <INPUT >OUTPUT
#json-get [keys ...] [-k|--keys] [-j|--json|-r|--raw|-c|--csv] [-i|--input INPUT]
[-o|--output OUTPUT]
~~~

Shell:

~~~
$ gridlabd json-get [keys ...] [-k|--keys] [-j|--json|-r|--raw|-r|--csv] [<INPUT] [>OUTPUT]
$ gridlabd json-get [keys ...] [-k|--keys] [-j|--json|-r|--raw|-r|--csv] [-i|--input INPUT]
[-o|--output OUTPUT]
~~~

# Description
Expand Down
54 changes: 36 additions & 18 deletions gldcore/scripts/gridlabd-json-get
Original file line number Diff line number Diff line change
Expand Up @@ -3,52 +3,70 @@
import sys
import json

input_file = sys.stdin
output_file = sys.stdout

def error(code,msg):
print("ERROR %d: %s" % (code,msg), file=sys.stderr);
quit(code);
exit(code);

def output_json(data):
if type(data) is dict or type(data) is list :
print(json.dumps(data,indent=4))
print(json.dumps(data,indent=4),file=output_file)
else:
print(data)
print(data,file=output_file)

def output_raw(data):
print(data)
print(data,file=output_file)

def output_csv(data):
if type(data) is dict:
for key,value in data.items():
print(f"{key},{value}")
print(f"{key},{value}",file=output_file)
elif type(data) is list:
print("\n".join(list))
print("\n".join(data),file=output_file)
else:
print(data)
print(data,file=output_file)

output_format=output_json
output_format = output_json
keys = []
keys_only = False

if len(sys.argv) > 1 and sys.argv[1] in ['-h','--help','help'] :
print("Syntax: gridlabd json-get [keys ...] [-k|--keys] [-j|--json|-r|--raw|-r|--csv]");
print("Syntax: gridlabd json-get [keys ...] [-k|--keys] [-j|--json|-r|--raw|-r|--csv] [-i|--input INTPUT] [-o|--output OUTPUT]");
quit(0);
try:
data = json.load(sys.stdin);
if len(sys.argv) > 1 :
for item in sys.argv[1::] :
n = 1
while n < len(sys.argv):
item = sys.argv[n]
if item in ['-k','--keys'] :
data = list(data.keys());
break;
keys_only = True
elif item in ['-r','--raw']:
output_format = output_raw
elif item in ['-j','--json']:
output_format = output_json
elif item in ['-c','--csv']:
output_format = output_csv
elif type(data) is dict and item in data.keys() :
data = data[item];
elif type(data) is list and int(item) >=0 and int(item) <= len(data) :
data = data[int(item)];
elif item in ['-i','--input']:
input_file = open(sys.argv[n+1],'r')
n += 1
elif item in ['-o','--output']:
output_file = open(sys.argv[n+1],'w')
n += 1
else:
keys.append(item)
n += 1
data = json.load(input_file);
for key in keys:
if type(data) is dict and key in data.keys() :
data = data[key];
elif type(data) is list and int(key) >=0 and int(key) <= len(data) :
data = data[int(key)];
else :
error(2,"%s is not valid"%item);
error(2,"%s is not valid"%key);
if keys_only:
data = list(data.keys())
output_format(data)
except:
error(1,"%s %s" % (sys.exc_info()[0],sys.exc_info()[1]));

0 comments on commit 5627921

Please sign in to comment.