1
2
3
4
5
6
7
8
9
10
11
12
13 __doc__="""BatchDeviceLoader.py
14
15 BatchDeviceLoader.py loads a list of devices read from a file.
16 The file can be formatted like this:
17
18 device0
19 /Path/To/Device
20 device1
21 device2
22 /Path/To/Other
23 device3
24 device4"""
25
26 import Globals
27 import transaction
28 import re
29 import sys
30
31 from Products.ZenUtils.ZCmdBase import ZCmdBase
32 from Products.ZenUtils.CmdBase import CmdBase
33
34
36
41
50
52 ZCmdBase.buildOptions(self)
53 self.parser.add_option('-i', '--infile',
54 dest="infile", default="",
55 help="input file for import (default stdin)")
56
58 _slash = re.compile(r'\s*/', re.M)
59 _sp = re.compile(r'\s+', re.M)
60 _r = re.compile(r'\s+/', re.M)
61 if not _slash.match(rawDevices):
62 rawDevices = "/Discovered\n" + rawDevices
63 sections = [re.split(_sp, x) for x in re.split(_r, rawDevices)]
64 finalList = []
65 for s in sections:
66 path, devs = s[0], s[1:]
67 if not path.startswith('/'): path = '/' + path
68 finalList += [{'deviceName':x,'devicePath':path} for x in devs]
69 return finalList
70
71 if __name__=='__main__':
72 b = BatchDeviceLoader()
73 b.loadDevices()
74