Package Products :: Package ZenUtils :: Module ZenScriptBase |
[hide private]
[frames] | no frames]

# Source Code for Module Products.ZenUtils.ZenScriptBase

      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
     31 -class DataRootError(Exception):pass
     32
     33 -class ZenScriptBase(CmdBase):
     34
     35 -    def __init__(self, noopts=0, app=None, connect=False):
     36          CmdBase.__init__(self, noopts)
     37          self.dataroot = None
     38          self.app = app
     39          self.db = None
     40          if connect:
     41              self.connect()
     42
     43 -    def connect(self):
     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
     55 -    def login(self, name='admin', userfolder=None):
     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
     66 -    def logout(self):
     67          """Logs out."""
     68          noSecurityManager()
     69
     70
     71 -    def getConnection(self):
     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
     86 -    def closeAll(self):
     87          """Close all connections in both free an inuse pools.
     88          """
     89          self.db.close()
     90
     91
     92 -    def opendb(self):
     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
    101 -    def syncdb(self):
    102          self.connection.sync()
    103
    104
    105 -    def closedb(self):
    106          self.connection.close()
    107          #self.db.close()
    108          self.app = None
    109          self.dataroot = None
    110          self.dmd = None
    111
    112
    113 -    def getDataRoot(self):
    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
    120 -    def getDmdObj(self, path):
    121          """return an object based on a path starting from the dmd"""
    122          return getObjByPath(self.app, self.options.dataroot+path)
    123
    124
    125 -    def findDevice(self, name):
    126          """return a device based on its FQDN"""
    127          devices = self.dataroot.getDmdRoot("Devices")
    128          return devices.findDevice(name)
    129
    130
    131 -    def buildOptions(self):
    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