Mercurial > repos > kellrott > nosql_interface
diff json_eval.xml @ 2:1fce03693725 draft
Uploaded
author | kellrott |
---|---|
date | Tue, 24 Jul 2012 17:41:32 -0400 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/json_eval.xml Tue Jul 24 17:41:32 2012 -0400 @@ -0,0 +1,105 @@ +<tool id="json_eval" name="JSON Eval" version="1.0"> + <description>Evalutate and Format JSON</description> + <command interpreter="python">$script_file</command> + <inputs> + <param name="json_txt" type="text" area="True" size="5x35" label="JSON Text" optional="True"> + <sanitizer> + <valid initial="string.printable"> + <remove value="""/> + </valid> + <mapping initial="none"> + <add source=""" target="\""/> + </mapping> + </sanitizer> + </param> + <param name="json_data" type="data" label="JSON File" optional="True"/> + <param name="multi_line" type="boolean" label="MultiLine file" checked="False"/> + <param name="end_line" type="boolean" label="Add endfiles" checked="True"/> + <param name="eval_txt" type="text" area="True" size="5x35" label="PYTHON Eval"> + <sanitizer> + <valid initial="string.printable"> + <remove value="""/> + <remove value="\"/> + </valid> + <mapping initial="none"> + <add source=""" target="\""/> + <add source="\" target="\\"/> + </mapping> + </sanitizer> + </param> + </inputs> + <outputs> + <data format="txt" name="outfile" /> + </outputs> + <configfiles> + <configfile name="script_file"><![CDATA[#!/usr/bin/env python +import os +import sys +import json +from StringIO import StringIO +json_txt = """${json_txt}""" +json_path = """${json_data}""" +eval_txt = """${eval_txt}""" +out_path = """${outfile}""" +multi_line = "${multi_line}" +add_endline = "${end_line}" + +print eval_txt + +if len(json_path) and json_path != "None": + handle = open(json_path) +else: + handle = StringIO(json_txt) + +ohandle = open(out_path, "w") +if multi_line == "true": + for line in handle: + funcmap = { + "len":len, + "value" : json.loads(line) + } + ohandle.write( eval(eval_txt,{"__builtins__":None},funcmap) ) + if add_endline == "true": + ohandle.write("\n") + +else: + line = handle.read() + funcmap = { + "len":len, + "value" : json.loads(line) + } + ohandle.write( eval(eval_txt,{"__builtins__":None},funcmap) ) + if add_endline == "true": + ohandle.write("\n") + +handle.close() +ohandle.close() + + +]]></configfile> + </configfiles> + <help> +This is a utility to decode JSON data into text info. + +The 'multiline' mode assumes that each line in an input is a seperate and complete +JSON record (ie no return characters in the middle of a record) + +Given a files with + +{ "@id" : "Test_1" } +{ "@id" : "Test_2" } +{ "@id" : "Test_3" } + +Example: + value['@id'] + +Prints out the file: + Test_1 + Test_2 + Test_3 + + + + + </help> +</tool>