Part of Slepp's ProjectsPastebinTURLImagebinFilebin
Feedback -- English French German Japanese
Create Upload Newest Tools Donate
Sign In | Create Account

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 

  1. #!/usr/bin/env python
  2. """
  3. A simple utility to convert Splunk's simple dashboard/form XML format used in
  4. Splunk 4.0 and help you convert it into the advanced XML view format using the
  5. provided "showsource" feature. In the future, an automated tool will most likely
  6. be available. However, in the mean time it is quite tedious to convert all of
  7. that by hand, so I wrote this to quickly convert the python dictionaries dumped
  8. on the web page into the advance XML form structure one module at a time.
  9.  
  10.  
  11. Basic instructions:
  12.  
  13. * Point your browser to your view and add "?showsource=true" to the end of the
  14.    URL, for example: http://localhost:8000/app/myapp/myview?showsource=true
  15. * View the source (or download the HTML). This is because of embedded "<" and ">"
  16.    characters.
  17. * Run a single expression (Found between the <pre></pre> tags) or the entire
  18.    file though this script. (Standard in / standard out. I found this works very
  19.    nicely in Komodo Edit)
  20.  
  21. Notes/Limitations:
  22.  
  23. * This utility only converts a single dictionary ("module") into the
  24.    corresponding XML syntax at a time. This script has no knowledge of any
  25.    nested structuring of this modules: you will have to do that yourself. (Or
  26.    update this script to figure the nesting out for you. This should be possible
  27.    using the <div> tags in the HTML.  But I didn't want to put the effort into it.
  28. * Minimal XML escaping is handled here. If "<>" are found then a CDATA element
  29.    is used. And "&" is escaped in regular strings.  This was good enough for my
  30.    needs, but may need to be improved upon.
  31.  
  32. This was written/tested with Splunk 4.0.6.
  33.  
  34. *** Feel free to use this script however you would like. It is provided as-is.
  35. There is no warranty of any kind. ***
  36.  
  37. """
  38.  
  39. __author__ = "Lowell Alleman"
  40. __version__ = "0.2"
  41.  
  42. import sys
  43. import re
  44.  
  45. INDENT = "  "
  46.  
  47. def write_dict(d, stream, indent=0):
  48.     " Write out a nested dictionary structure in the XML <param> format."
  49.     prefix = INDENT * indent
  50.     for key, value in d.items():
  51.         if value is None:
  52.             continue
  53.         stream.write("%s<param name=\"%s\">" % (prefix,key))
  54.         multiline = False
  55.         if type(value) == dict:
  56.             stream.write("\n")
  57.             write_dict(value, stream, indent+1)
  58.             multiline = True
  59.         elif type(value) in (list, tuple):
  60.             write_list(value, stream, indent+1)
  61.             multiline = True
  62.         elif type(value) == str and ("<" in value or ">" in value):
  63.             stream.write("<![CDATA[%s]]>" % value)
  64.         else:
  65.             stream.write(str(value).replace("&", "&amp;"))
  66.         if multiline:
  67.             stream.write(prefix)
  68.         stream.write("</param>\n")
  69.  
  70. def write_list(lst, stream, indent=0):
  71.     prefix = INDENT * indent
  72.     for item in lst:
  73.         stream.write(prefix + "<list>\n")
  74.         if type(item) == dict:
  75.             write_dict(item, stream, indent+1)
  76.         else:
  77.             raise TypeError("Always expecting a list of dictionariy. "
  78.                             "Found type: %s" % type(item))
  79.         stream.write(prefix + "</list>\n")
  80.  
  81. def convert_to_xml(d, stream):
  82.     " Write out a <module>/<param> XML structure for the given dictionary. "
  83.     d = dict(d)
  84.     className = d["className"]
  85.     if "params" in d:
  86.         params = d["params"]
  87.         del d["params"]
  88.     else:
  89.         params = {}
  90.     del d["className"]
  91.    
  92.     stream.write('<module name="%s" ' % className)
  93.     for key,value in d.items():
  94.         stream.write('%s="%s" ' % (key, value))
  95.     stream.write(">\n")
  96.     write_dict(params, stream)
  97.     stream.write("\n")
  98.     stream.write("</module>\n")
  99.     stream.flush()
  100.  
  101.  
  102. def convert_py_to_xml(py_dict_string, xml_out):
  103.     """
  104.     Take a single python dictionary string expression and convert it to XML. Any
  105.     errors are simply written to standard error and the process continues.
  106.     """
  107.     try:
  108.         # Convert python dictionary string into a real python dictionary
  109.         d = eval(py_dict_string)
  110.         convert_to_xml(d, xml_out)
  111.     except Exception, e:
  112.         sys.stderr.write("Failed to convert to Splunk Advanced XML format"
  113.                          "(%s)\nInput expression:\n%s\n\n" % (e, py_dict_string))
  114.  
  115.  
  116. if __name__ == '__main__':
  117.     # Read entire input stream at once
  118.     chunk = sys.stdin.read()
  119.    
  120.     # Lazy approach to handle an entire HTML file based on the fact that the
  121.     # each module has it's own dictionary within a "<pre>" block.
  122.     if "<pre>" in chunk:
  123.         chunks = re.findall(r"<pre>(.*?)</pre>", chunk, re.IGNORECASE | re.MULTILINE | re.DOTALL)
  124.     else:
  125.         chunks = [ chunk ]
  126.    
  127.     for chunk in chunks:
  128.         convert_py_to_xml(chunk, sys.stdout)

Paste Details

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.

update paste below
details of the post (optional)

Note: Only the paste content is required, though the following information can be useful to others.

Save name / title?

(space separated, optional)



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.

worth-right
worth-right