Package ZenRelations :: Module ExportDevices
[hide private]
[frames] | no frames]

Source Code for Module ZenRelations.ExportDevices

  1  ########################################################################### 
  2  # 
  3  # This program is part of Zenoss Core, an open source monitoring platform. 
  4  # Copyright (C) 2007, Zenoss Inc. 
  5  # 
  6  # This program is free software; you can redistribute it and/or modify it 
  7  # under the terms of the GNU General Public License version 2 as published by 
  8  # the Free Software Foundation. 
  9  # 
 10  # For complete information please visit: http://www.zenoss.com/oss/ 
 11  # 
 12  ########################################################################### 
 13  import sys 
 14  from xml.dom.minidom import parseString 
 15  import StringIO 
 16  import re 
 17   
 18  import Globals 
 19   
 20  from Products.ZenUtils.ZCmdBase import ZCmdBase 
 21  from Products.ZenRelations.RelationshipManager import RelationshipManager 
 22   
 23  _newlines = re.compile('\n[\t \r]*\n', re.M) 
 24   
25 -class ExportDevices(ZCmdBase):
26
27 - def __init__(self):
28 ZCmdBase.__init__(self) 29 if not self.options.outfile: 30 self.outfile = sys.stdout 31 else: 32 self.outfile = open(self.options.outfile, 'w')
33 34
35 - def buildOptions(self):
36 """basic options setup sub classes can add more options here""" 37 ZCmdBase.buildOptions(self) 38 self.parser.add_option('-o', '--outfile', 39 dest="outfile", 40 help="output file for export default is stdout") 41 self.parser.add_option('--ignore', action="append", 42 dest="ignorerels", default=[], 43 help="relations that should be ignored can be many")
44 45
46 - def stripUseless(self, doc):
47 48 _retain_class = ( 49 "Products.ZenModel.DeviceClass", 50 "Products.ZenModel.Device", 51 ) 52 53 _retain_props = ( 54 "description", 55 "productionState", 56 "priority", 57 "monitors", 58 ) 59 60 def denewline(s): 61 while re.search(_newlines, s): 62 s = re.sub(_newlines, '\n', s) 63 return s
64 65 def clearObjects(node): 66 def keepDevice(dev): 67 try: return not dev.getAttribute('module') in _retain_class 68 except: return True
69 try: devs = node.getElementsByTagName('object') 70 except AttributeError: pass 71 else: 72 devs = filter(keepDevice, devs) 73 map(node.parentNode.removeChild, devs) 74 75 def clearProps(node): 76 try: props = node.getElementsByTagName('property') 77 except AttributeError: pass 78 else: 79 for prop in props: 80 if prop.getAttribute('module') not in _retain_props: 81 prop.parentNode.removeChild(prop) 82 83 root = doc.getElementsByTagName('objects')[0] 84 clearObjects(root) 85 clearProps(root) 86 87 #for dev in getDevices(doc): print dev.getAttribute('id') 88 89 return denewline(doc.toprettyxml().replace('\t', ' '*4)) 90 91
92 - def export(self):
93 root = self.dmd.Devices 94 buffer = StringIO.StringIO() 95 if hasattr(root, "exportXml"): 96 buffer = self.outfile 97 buffer.write("""<?xml version="1.0"? encoding='latin-1'>\n""") 98 buffer.write("<objects>\n") 99 root.exportXml(buffer,self.options.ignorerels,True) 100 buffer.write("</objects>\n") 101 doc = parseString(buffer.getvalue()) 102 finalxml = self.stripUseless(doc) 103 self.outfile.write(finalxml) 104 doc.unlink() 105 buffer.close() 106 else: 107 print "ERROR: root object not a exportable (exportXml not found)"
108 109 110 111 if __name__ == '__main__': 112 ex = ExportDevices() 113 ex.export() 114