1
2
3
4
5
6
7
8
9
10
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
26
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
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
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
88
89 return denewline(doc.toprettyxml().replace('\t', ' '*4))
90
91
108
109
110
111 if __name__ == '__main__':
112 ex = ExportDevices()
113 ex.export()
114