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

Source Code for Module 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 as published by 
 8  # 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 if baseModule: classpath = ".".join((baseModule, classpath)) 18 try: 19 mod = __import__(classpath) 20 except ImportError: 21 try: 22 mod = __import__(".".join(classpath.split(".")[:-1])) 23 except: 24 raise ZenImportError("failed importing class %s" % classpath) 25 for comp in classpath.split(".")[1:]: 26 mod = getattr(mod, comp) 27 classdef = getattr(mod, comp, None) 28 if classdef: mod = classdef 29 return mod
30 31
32 -def importClasses(basemodule=None, skipnames=()):
33 """ 34 import all classes listed in baseModule in the variable productNames 35 and return them in a list. Assume that classes are defined in a file 36 with the same name as the class. 37 """ 38 classList = [] 39 mod = __import__(basemodule) 40 for comp in basemodule.split(".")[1:]: 41 mod = getattr(mod, comp) 42 for prodname in mod.productNames: 43 if prodname in skipnames: continue 44 classdef = importClass(prodname, basemodule) 45 classList.append(classdef) 46 return classList
47