Package Products :: Package ZenRelations :: Module utils |
[hide private]
[frames] | no frames]

# Source Code for Module Products.ZenRelations.utils

     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  from Exceptions import ZenImportError
    14
    15 -def importClass(classpath, baseModule=None):
    16      """lookup a class by its path use baseModule path if passed"""
    17      import sys
    18      if baseModule: classpath = ".".join((baseModule, classpath))
    19      parts = classpath.split('.')
    20      try:
    21          mod = __import__(classpath)
    22          mod = sys.modules[classpath]
    23      except (ImportError, KeyError):
    24          try:
    25              base = ".".join(parts[:-1])
    26              mod = __import__(base)
    27              mod = sys.modules[base]
    28          except:
    29              raise ZenImportError("failed importing class %s" % classpath)
    30      return getattr(mod, parts[-1], mod)
    31
    32
    33 -def importClasses(basemodule=None, skipnames=()):
    34      """
    35      import all classes listed in baseModule in the variable productNames
    36      and return them in a list.  Assume that classes are defined in a file
    37      with the same name as the class.
    38      """
    39      classList = []
    40      mod = __import__(basemodule)
    41      for comp in basemodule.split(".")[1:]:
    42          mod = getattr(mod, comp)
    43      for prodname in mod.productNames:
    44          if prodname in skipnames: continue
    45          classdef = importClass(prodname, basemodule)
    46          classList.append(classdef)
    47      return classList
    48
    49
    50 -class ZenRelationshipNameChooser(object):
    51      """
    52      Adapts a ZenRelation to find a unique id.
    53      """
    54 -    def __init__(self, context):
    55          self.context = context
    56
    57 -    def chooseName(self, name):
    58          """
    59          Create an id.
    60          """
    61          dot = name.rfind('.')
    62          if dot >= 0:
    63              suffix = name[dot:]
    64              name = name[:dot]
    65          else:
    66              suffix = ''
    67          n = name + suffix
    68          i = 1
    69          inuse = self.context.objectIdsAll()
    70          while n in inuse:
    71              i += 1
    72              n = name + str(i) + suffix
    73          return str(n)
    74