| Trees | Indices | Help |
|
|---|
|
|
1 ##############################################################################
2 #
3 # Copyright (C) Zenoss, Inc. 2007, all rights reserved.
4 #
5 # This content is made available according to terms specified in
6 # License.zenoss under the directory where your Zenoss product is installed.
7 #
8 ##############################################################################
9
10
11 """ZenScriptBase
12
13 Scripts with classes who extend ZenScriptBase have a zope instance with a
14 dmd root and loaded ZenPacks, like zendmd.
15 """
16
17 from zope.component import getUtility
18
19 from AccessControl.SecurityManagement import newSecurityManager
20 from AccessControl.SecurityManagement import noSecurityManager
21 from transaction import commit
22 from Products.ZenUtils.Utils import getObjByPath, zenPath, set_context
23 from Products.ZenUtils.CmdBase import CmdBase
24 from Products.ZenUtils.ZodbFactory import IZodbFactoryLookup
25
26 from Products.ZenRelations.ZenPropertyManager import setDescriptors
27 from Products.ZenUtils.Exceptions import ZentinelException
28
29 defaultCacheDir = zenPath('var')
30
32
34
36 CmdBase.__init__(self, noopts)
37 self.dataroot = None
38 self.app = app
39 self.db = None
40 if connect:
41 self.connect()
42
44 if not self.app:
45 connectionFactory = getUtility(IZodbFactoryLookup).get()
46 self.db, self.storage = connectionFactory.getConnection(**self.options.__dict__)
47 self.getDataRoot()
48 self.login()
49 if getattr(self.dmd, 'propertyTransformers', None) is None:
50 self.dmd.propertyTransformers = {}
51 commit()
52 setDescriptors(self.dmd)
53
54
56 """Logs in."""
57 if userfolder is None:
58 userfolder = self.app.acl_users
59 user = userfolder.getUserById(name)
60 if user is None: return
61 if not hasattr(user, 'aq_base'):
62 user = user.__of__(userfolder)
63 newSecurityManager(None, user)
64
65
69
70
72 """Return a wrapped app connection from the connection pool.
73 """
74 if not self.db:
75 raise ZentinelException(
76 "running inside zope can't open connections.")
77 with self.poollock:
78 connection=self.db.open()
79 root=connection.root()
80 app=root['Application']
81 app = set_context(app)
82 app._p_jar.sync()
83 return app
84
85
90
91
93 if self.app: return
94 self.connection=self.db.open()
95 root=self.connection.root()
96 app = root['Application']
97 self.app = set_context(app)
98 self.app._p_jar.sync()
99
100
102 self.connection.sync()
103
104
106 self.connection.close()
107 #self.db.close()
108 self.app = None
109 self.dataroot = None
110 self.dmd = None
111
112
114 if not self.app: self.opendb()
115 if not self.dataroot:
116 self.dataroot = getObjByPath(self.app, self.options.dataroot)
117 self.dmd = self.dataroot
118
119
121 """return an object based on a path starting from the dmd"""
122 return getObjByPath(self.app, self.options.dataroot+path)
123
124
126 """return a device based on its FQDN"""
127 devices = self.dataroot.getDmdRoot("Devices")
128 return devices.findDevice(name)
129
130
132 """basic options setup sub classes can add more options here"""
133 CmdBase.buildOptions(self)
134
135 connectionFactory = getUtility(IZodbFactoryLookup).get()
136 connectionFactory.buildOptions(self.parser)
137
| Trees | Indices | Help |
|
|---|
| Generated by Epydoc 3.0.1.1812 on Mon Jul 30 17:11:36 2012 | http://epydoc.sourceforge.net |