annotate json_eval.xml @ 2:1fce03693725 draft

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