Advertising
Paste Description for splunk_showsource_to_xml.py
This is a simple utility to convert Splunk's simple dashboard/form XML format used in Splunk 4.0 and help you convert it into the advanced XML view format using the provided "showsource" feature.
- splunk_showsource_to_xml.py
- Wednesday, November 18th, 2009 at 9:12:48am MST
- #!/usr/bin/env python
- """
- A simple utility to convert Splunk's simple dashboard/form XML format used in
- Splunk 4.0 and help you convert it into the advanced XML view format using the
- provided "showsource" feature. In the future, an automated tool will most likely
- be available. However, in the mean time it is quite tedious to convert all of
- that by hand, so I wrote this to quickly convert the python dictionaries dumped
- on the web page into the advance XML form structure one module at a time.
- Basic instructions:
- * Point your browser to your view and add "?showsource=true" to the end of the
- URL, for example: http://localhost:8000/app/myapp/myview?showsource=true
- * View the source (or download the HTML). This is because of embedded "<" and ">"
- characters.
- * Run a single expression (Found between the <pre></pre> tags) or the entire
- file though this script. (Standard in / standard out. I found this works very
- nicely in Komodo Edit)
- Notes/Limitations:
- * This utility only converts a single dictionary ("module") into the
- corresponding XML syntax at a time. This script has no knowledge of any
- nested structuring of this modules: you will have to do that yourself. (Or
- update this script to figure the nesting out for you. This should be possible
- using the <div> tags in the HTML. But I didn't want to put the effort into it.
- * Minimal XML escaping is handled here. If "<>" are found then a CDATA element
- is used. And "&" is escaped in regular strings. This was good enough for my
- needs, but may need to be improved upon.
- This was written/tested with Splunk 4.0.6.
- *** Feel free to use this script however you would like. It is provided as-is.
- There is no warranty of any kind. ***
- """
- __author__ = "Lowell Alleman"
- __version__ = "0.2"
- import sys
- import re
- INDENT = " "
- def write_dict(d, stream, indent=0):
- " Write out a nested dictionary structure in the XML <param> format."
- prefix = INDENT * indent
- for key, value in d.items():
- if value is None:
- continue
- stream.write("%s<param name=\"%s\">" % (prefix,key))
- multiline = False
- if type(value) == dict:
- stream.write("\n")
- write_dict(value, stream, indent+1)
- multiline = True
- elif type(value) in (list, tuple):
- write_list(value, stream, indent+1)
- multiline = True
- elif type(value) == str and ("<" in value or ">" in value):
- stream.write("<![CDATA[%s]]>" % value)
- else:
- stream.write(str(value).replace("&", "&"))
- if multiline:
- stream.write(prefix)
- stream.write("</param>\n")
- def write_list(lst, stream, indent=0):
- prefix = INDENT * indent
- for item in lst:
- stream.write(prefix + "<list>\n")
- if type(item) == dict:
- write_dict(item, stream, indent+1)
- else:
- raise TypeError("Always expecting a list of dictionariy. "
- "Found type: %s" % type(item))
- stream.write(prefix + "</list>\n")
- def convert_to_xml(d, stream):
- " Write out a <module>/<param> XML structure for the given dictionary. "
- d = dict(d)
- className = d["className"]
- if "params" in d:
- params = d["params"]
- del d["params"]
- else:
- params = {}
- del d["className"]
- stream.write('<module name="%s" ' % className)
- for key,value in d.items():
- stream.write('%s="%s" ' % (key, value))
- stream.write(">\n")
- write_dict(params, stream)
- stream.write("\n")
- stream.write("</module>\n")
- stream.flush()
- def convert_py_to_xml(py_dict_string, xml_out):
- """
- Take a single python dictionary string expression and convert it to XML. Any
- errors are simply written to standard error and the process continues.
- """
- try:
- # Convert python dictionary string into a real python dictionary
- d = eval(py_dict_string)
- convert_to_xml(d, xml_out)
- except Exception, e:
- sys.stderr.write("Failed to convert to Splunk Advanced XML format"
- "(%s)\nInput expression:\n%s\n\n" % (e, py_dict_string))
- if __name__ == '__main__':
- # Read entire input stream at once
- chunk = sys.stdin.read()
- # Lazy approach to handle an entire HTML file based on the fact that the
- # each module has it's own dictionary within a "<pre>" block.
- if "<pre>" in chunk:
- chunks = re.findall(r"<pre>(.*?)</pre>", chunk, re.IGNORECASE | re.MULTILINE | re.DOTALL)
- else:
- chunks = [ chunk ]
- for chunk in chunks:
- convert_py_to_xml(chunk, sys.stdout)
Paste Details
Tags: python xml views splunk showsource
advertising
Update the Post
Either update this post and resubmit it with changes, or make a new post.
You may also comment on this post.
Please note that information posted here will expire by default in one month. If you do not want it to expire, please set the expiry time above. If it is set to expire, web search engines will not be allowed to index it prior to it expiring. Items that are not marked to expire will be indexable by search engines. Be careful with your passwords. All illegal activities will be reported and any information will be handed over to the authorities, so be good.