def get_instances(module_name): instances = [] module = import_module(module_name) members = inspect.getmembers(module, inspect.isclass) for cls_name, instance in members: if inspect.getmodule(instance) == module and \ cls_name.endswith('Model'): instances.append(instance) return instances
def load_url_sub_node(path, package_name, expect_attr="_url_sub_node_name"): sub_nodes = {} for mod_name in listPathModules(path): if mod_name.startswith("_"): continue module = import_module(package_name + '.' + mod_name) for node in [getattr(module, x) for x in dir(module)]: if not hasattr(node, expect_attr): continue name = getattr(node, expect_attr)["name"] sub_nodes.update({name: node}) return sub_nodes
def __init__(self, objstore_loc=None): self.objstore = ObjectStore(objstore_loc) kargs = {'objstore': self.objstore} this = os.path.basename(__file__) this_mod = os.path.splitext(this)[0] models = [] for mod_name in listPathModules(os.path.dirname(__file__)): if mod_name.startswith("_") or mod_name == this_mod: continue module = import_module('wok.model.' + mod_name) members = inspect.getmembers(module, inspect.isclass) for cls_name, instance in members: if inspect.getmodule(instance) == module: if cls_name.endswith('Model'): models.append(instance(**kargs)) return super(Model, self).__init__(models)