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

# Source Code for Module Products.ZenUtils.Skins

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