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

# Source Code for Module Products.ZenRelations.utils

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