All pastes #1676548 Raw Edit

splunk_showsource_to_xml.py

public python v1 · immutable
#1676548 ·published 2009-11-18 16:12 UTC
rendered paste body
#!/usr/bin/env python"""A simple utility to convert Splunk's simple dashboard/form XML format used inSplunk 4.0 and help you convert it into the advanced XML view format using theprovided "showsource" feature. In the future, an automated tool will most likelybe available. However, in the mean time it is quite tedious to convert all ofthat by hand, so I wrote this to quickly convert the python dictionaries dumpedon 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 sysimport reINDENT = "  "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("&", "&amp;"))        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)