dynamic

Module dynamic |
[hide private]
[frames] | no frames]

## Source Code for Module dynamic

     1
     2  # Module wrapper into which we're going to import anything
     3  # defined in configfiles
     4  from utils import getFirstData, elementType
     5  import sys
     6
     7 -def makeObjectFromDom(session, topNode, parentObject):
     8      # Lots of indirections from xml to object
     9      objectType = None
    10      for c in topNode.childNodes:
    11          if (c.nodeType == elementType and c.localName == "objectType"):
    12              # Here's what we want to instantiate
    13              objectType = getFirstData(c)
    14              break
    15      if objectType == None:
    16          raise(ConfigFileException('No objectType set in config file.'))
    17
    18      # XXX Fix for a.b.c  (?)
    19      return buildObject(session, objectType, [topNode, parentObject])
    20
    21
    22 -def buildObject(session, objectType, args):
    23      objs = objectType.split('.')
    24      globalImport(objs[0], [objs[1]])
    25      parentClass = globals()[objs[0]]
    26      for o in objs[1:]:
    27          parentClass = getattr(parentClass, o)
    28      return parentClass(session, *args)
    29
    30
    31 -def globalImport(module, objects=[], name=None):
    32      # With thanks to:
    33      # http://pleac.sourceforge.net/pleac_python/packagesetc.html
    34
    35      nname = module
    36      loaded = __import__(module)
    37      for mod in (module.split(".")[1:]):
    38          nname = mod
    39          loaded = getattr(loaded, mod)
    40      if name <> None:
    41          globals()[name] = loaded
    42      else:
    43          globals()[nname] = loaded
    44
    45      if (len(objects) == 1 and objects[0] == "*"):
    46          objects = dir(loaded)
    47
    48      for o in objects:
    49          if (o[0] <> "_"):
    50              globals()[o] = getattr(loaded, o)
    51
    52      return loaded
    53