view json_eval.xml @ 3:4d03df88688d draft default tip

Uploaded
author kellrott
date Tue, 24 Jul 2012 17:42:17 -0400
parents 1fce03693725
children
line wrap: on
line source

<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="&quot;"/>
				</valid>
				<mapping initial="none">
					<add source="&quot;" target="\&quot;"/>
				</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="&quot;"/>
					<remove value="\"/>
				</valid>
				<mapping initial="none">
					<add source="&quot;" target="\&quot;"/>
					<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>