Mercurial > repos > kellrott > nosql_interface
comparison json_eval.xml @ 2:1fce03693725 draft
Uploaded
| author | kellrott |
|---|---|
| date | Tue, 24 Jul 2012 17:41:32 -0400 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 1:bda961c955d5 | 2:1fce03693725 |
|---|---|
| 1 <tool id="json_eval" name="JSON Eval" version="1.0"> | |
| 2 <description>Evalutate and Format JSON</description> | |
| 3 <command interpreter="python">$script_file</command> | |
| 4 <inputs> | |
| 5 <param name="json_txt" type="text" area="True" size="5x35" label="JSON Text" optional="True"> | |
| 6 <sanitizer> | |
| 7 <valid initial="string.printable"> | |
| 8 <remove value="""/> | |
| 9 </valid> | |
| 10 <mapping initial="none"> | |
| 11 <add source=""" target="\""/> | |
| 12 </mapping> | |
| 13 </sanitizer> | |
| 14 </param> | |
| 15 <param name="json_data" type="data" label="JSON File" optional="True"/> | |
| 16 <param name="multi_line" type="boolean" label="MultiLine file" checked="False"/> | |
| 17 <param name="end_line" type="boolean" label="Add endfiles" checked="True"/> | |
| 18 <param name="eval_txt" type="text" area="True" size="5x35" label="PYTHON Eval"> | |
| 19 <sanitizer> | |
| 20 <valid initial="string.printable"> | |
| 21 <remove value="""/> | |
| 22 <remove value="\"/> | |
| 23 </valid> | |
| 24 <mapping initial="none"> | |
| 25 <add source=""" target="\""/> | |
| 26 <add source="\" target="\\"/> | |
| 27 </mapping> | |
| 28 </sanitizer> | |
| 29 </param> | |
| 30 </inputs> | |
| 31 <outputs> | |
| 32 <data format="txt" name="outfile" /> | |
| 33 </outputs> | |
| 34 <configfiles> | |
| 35 <configfile name="script_file"><![CDATA[#!/usr/bin/env python | |
| 36 import os | |
| 37 import sys | |
| 38 import json | |
| 39 from StringIO import StringIO | |
| 40 json_txt = """${json_txt}""" | |
| 41 json_path = """${json_data}""" | |
| 42 eval_txt = """${eval_txt}""" | |
| 43 out_path = """${outfile}""" | |
| 44 multi_line = "${multi_line}" | |
| 45 add_endline = "${end_line}" | |
| 46 | |
| 47 print eval_txt | |
| 48 | |
| 49 if len(json_path) and json_path != "None": | |
| 50 handle = open(json_path) | |
| 51 else: | |
| 52 handle = StringIO(json_txt) | |
| 53 | |
| 54 ohandle = open(out_path, "w") | |
| 55 if multi_line == "true": | |
| 56 for line in handle: | |
| 57 funcmap = { | |
| 58 "len":len, | |
| 59 "value" : json.loads(line) | |
| 60 } | |
| 61 ohandle.write( eval(eval_txt,{"__builtins__":None},funcmap) ) | |
| 62 if add_endline == "true": | |
| 63 ohandle.write("\n") | |
| 64 | |
| 65 else: | |
| 66 line = handle.read() | |
| 67 funcmap = { | |
| 68 "len":len, | |
| 69 "value" : json.loads(line) | |
| 70 } | |
| 71 ohandle.write( eval(eval_txt,{"__builtins__":None},funcmap) ) | |
| 72 if add_endline == "true": | |
| 73 ohandle.write("\n") | |
| 74 | |
| 75 handle.close() | |
| 76 ohandle.close() | |
| 77 | |
| 78 | |
| 79 ]]></configfile> | |
| 80 </configfiles> | |
| 81 <help> | |
| 82 This is a utility to decode JSON data into text info. | |
| 83 | |
| 84 The 'multiline' mode assumes that each line in an input is a seperate and complete | |
| 85 JSON record (ie no return characters in the middle of a record) | |
| 86 | |
| 87 Given a files with | |
| 88 | |
| 89 { "@id" : "Test_1" } | |
| 90 { "@id" : "Test_2" } | |
| 91 { "@id" : "Test_3" } | |
| 92 | |
| 93 Example: | |
| 94 value['@id'] | |
| 95 | |
| 96 Prints out the file: | |
| 97 Test_1 | |
| 98 Test_2 | |
| 99 Test_3 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 </help> | |
| 105 </tool> |
