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

# Source Code for Module Products.ZenUtils.Skins

      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  import os
     12  import string
     13  import warnings
     14
     15  warnings.filterwarnings('ignore', '.*non-existing path.*',
     16                          UserWarning,
     17                          '.*DirectoryView.*')
     18
     19 -def findZenPackRoot(base):
     20      """
     21      Search upwards for the root of a ZenPack.
     22
     23      >>> import os, tempfile; root = os.path.realpath(tempfile.mkdtemp())
     24      >>> skindir = os.path.join(root, 'ZenPacks/ZenPacks.zenoss.NotAPack-1.2.3-py2.6.egg/ZenPacks/zenoss/NotAPack/skins')
     25      >>> os.makedirs(skindir)
     26      >>> findZenPackRoot(skindir).replace(root, '/opt/zenoss')
     27      '/opt/zenoss/ZenPacks/ZenPacks.zenoss.NotAPack'
     28      """
     29      p = d = os.path.realpath(base)
     30      while d:
     31          if os.path.isdir(os.path.join(p, 'ZenPacks')):
     32              # Ditch version and extension if an egg
     33              if p.endswith('.egg'):
     34                  fullpath = p.split(os.sep)
     35                  name = fullpath.pop().split('-')[0]
     36                  fullpath.append(name)
     37                  p = os.sep.join(fullpath)
     38              return p
     39          p, d = os.path.split(p)
     40      return None
     41
     42
     43 -def skinDirs(base):
     44      layers = []
     45      for p, ds, fs in os.walk(os.path.join(base, 'skins')):
     46          for d in ds:
     47              if not d.startswith('.'):
     48                  layers.append(d)
     49          # stop at one level
     50          break
     51      return layers
     52
     53
     54 -def registerSkin(self, base, positionAfter='custom'):
     55      """setup the skins in a product"""
     56      layers = skinDirs(base)
     57      try:
     58          from Products.CMFCore.utils import getToolByName
     59          from Products.CMFCore.DirectoryView import addDirectoryViews
     60          skinstool = getToolByName(self, 'portal_skins')
     61          for layer in layers:
     62              if layer not in skinstool.objectIds():
     63                  path = os.path.join(base, 'skins')
     64                  if not os.path.exists(path): os.mkdir(path, mode=0755)
     65                  root = findZenPackRoot(path).split('/')[-1]
     66                  addDirectoryViews(skinstool, path, dict(__name__=root))
     67          skins = skinstool.getSkinSelections()
     68          for skin in skins:
     69              path = skinstool.getSkinPath(skin)
     70              path = map(string.strip, string.split(path,','))
     71              for layer in layers:
     72                  if layer not in path:
     73                      try:
     74                          path.insert(path.index(positionAfter)+1, layer)
     75                      except ValueError:
     76                          path.append(layer)
     77              path = ','.join(path)
     78              skinstool.addSkinSelection(skin, path)
     79      except ImportError, e:
     80          if "Products.CMFCore.utils" in e.args: pass
     81          else: raise
     82      except AttributeError, e:
     83          if "portal_skin" in e.args: pass
     84          else: raise
     85
     86 -def unregisterSkin(self, base, positionAfter='custom'):
     87      """setup the skins in a product"""
     88      from Products.ZenUtils.Utils import unused
     89      unused(positionAfter)
     90      layers = skinDirs(base)
     91      if layers is None: return
     92      try:
     93          from Products.CMFCore.utils import getToolByName
     94          skinstool = getToolByName(self, 'portal_skins')
     95          for layer in layers:
     96              if layer in skinstool.objectIds():
     97                  try:
     98                      skinstool._delOb(layer)
     99                  except AttributeError:
    100                      pass
    101          obs = skinstool._objects
    102          goodlayers = filter(lambda x:getattr(skinstool, x['id'], False), obs)
    103          skinstool._objects = tuple(goodlayers)
    104      except ImportError, e:
    105          if "Products.CMFCore.utils" in e.args: pass
    106          else: raise
    107      except AttributeError, e:
    108          if "portal_skin" in e.args: pass
    109          else: raise
    110