Package ZenReports :: Module ReportLoader
[hide private]
[frames] | no frames]

Source Code for Module ZenReports.ReportLoader

 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 os 
14  import transaction 
15  import Globals 
16   
17  from Products.ZenUtils.ZCmdBase import ZCmdBase 
18  from Products.ZenModel.Report import Report 
19   
20 -class ReportLoader(ZCmdBase):
21
22 - def buildOptions(self):
23 ZCmdBase.buildOptions(self) 24 self.parser.add_option('-f', '--force', dest='force', 25 action='store_true', default=0, 26 help="Force load all the reports")
27
28 - def loadDatabase(self):
29 repdir = os.path.join(os.path.dirname(__file__),"reports") 30 self.loadDirectory(repdir) 31 transaction.commit()
32
33 - def reports(self, directory):
34 def normalize(f): 35 return f.replace("_", " ")
36 def toOrg(path): 37 path = normalize(path).split("/") 38 path = path[path.index("reports") + 1:] 39 return "/" + "/".join(path)
40 return [(toOrg(p), normalize(f[:-4]), os.path.join(p, f)) 41 for p, ds, fs in os.walk(directory) 42 for f in fs 43 if f.endswith(".rpt")] 44
45 - def unloadDirectory(self, repdir):
46 self.log.info("removing reports from:%s", repdir) 47 reproot = self.dmd.Reports 48 for orgpath, fid, fullname in self.reports(repdir): 49 rorg = reproot.createOrganizer(orgpath) 50 if getattr(rorg, fid, False): 51 rorg._delObject(fid) 52 while rorg.id != 'Reports': 53 if not rorg.objectValues(): 54 id = rorg.id 55 rorg = rorg.getPrimaryParent() 56 rorg._delObject(id)
57 58
59 - def loadDirectory(self, repdir):
60 self.log.info("loading reports from:%s", repdir) 61 reproot = self.dmd.Reports 62 for orgpath, fid, fullname in self.reports(repdir): 63 rorg = reproot.createOrganizer(orgpath) 64 if getattr(rorg, fid, False): 65 if self.options.force: 66 rorg._delObject(fid) 67 else: 68 continue 69 self.log.info("loading: %s/%s", orgpath, fid) 70 self.loadFile(rorg, fid, fullname)
71 72
73 - def loadFile(self, root, id, fullname):
74 fdata = file(fullname).read() 75 rpt = Report(id, text=fdata) 76 root._setObject(id, rpt) 77 return rpt
78 79 80 if __name__ == "__main__": 81 rl = ReportLoader() 82 rl.loadDatabase() 83