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