def Register(self, comp, name=None): """ Register an application or component. This is usually done in the pyramid app file. The registered component is automatically loaded and set up to work with url traversal. *comp* can be one of the following cases - AppConf object - AppConf string as python dotted name - python object. Requires *name* parameter or *comp.id* attribute *name* is used as the url path name to lookup the component. """ log = logging.getLogger("portal") iface, conf = ResolveConfiguration(comp) if not conf and isinstance(comp, basestring): raise ConfigurationError, "Portal registration failure. No name given (%s)" % (str(comp)) if isinstance(comp, basestring) or isinstance(comp, baseConf): # factory methods if IAppConf.providedBy(conf): comp = ClassFactory(conf)(conf) elif IModuleConf.providedBy(conf): comp = ClassFactory(conf)(conf) elif iface and iface.providedBy(comp): comp = ResolveName(conf.context)(conf) elif isinstance(comp, basestring): comp = ResolveName(conf.context)(conf) try: name = name or conf.id except AttributeError: pass if not name: raise ConfigurationError, "Portal registration failure. No name given (%s)" % (str(comp)) log.debug("Portal.Register: %s %s", name, repr(conf)) self.__dict__[name] = comp comp.__parent__ = self comp.__name__ = name self.components.append(name)
def ResolveConfiguration(conf, base=None): """ Lookup configuration object by dotted python name. Returns interface and configuration object. Extends pyramid.DottedNameResolver with .json file support for configuration objects. Supports the following cases: - Path and file name to .json file. requires `type` set to one of the configuration types: *AppConf, FieldConf, DatabaseConf, RootConf, ObjectConf, ViewModuleConf, ViewConf, ToolConf, GroupConf, CategoryConf* - Dotted python name for configuration object including attribute name of configuration instance. - Dotted python name for object. Uses the convention to load the configuration from the 'configuration' attribute of the referenced object. - Configuration instance. Will just return it. returns Interface, configuration """ # string instance if isinstance(conf, basestring): if not base: base = caller_package() # json file if conf.find(".json")!= -1: path = ResolveAsset(conf, base) s = LoadFromFile(path.abspath()) conf = json.loads(s) # resolve attribute name elif conf: c = ResolveName(conf, base=base) if hasattr(c, "configuration"): conf = c.configuration else: conf = c # dict instance if isinstance(conf, dict): # load by interface if not "type" in conf: raise TypeError, "Configuration type not defined" c = ResolveName(conf["type"], base="nive") del conf["type"] conf = c(**conf) # module and not configuration if not IConf.providedBy(conf): if hasattr(conf, "configuration"): conf = conf.configuration # object instance if IAppConf.providedBy(conf): return IAppConf, conf if IDatabaseConf.providedBy(conf): return IDatabaseConf, conf if IFieldConf.providedBy(conf): return IFieldConf, conf if IRootConf.providedBy(conf): return IRootConf, conf if IObjectConf.providedBy(conf): return IObjectConf, conf if IViewModuleConf.providedBy(conf): return IViewModuleConf, conf if IViewConf.providedBy(conf): return IViewConf, conf if IToolConf.providedBy(conf): return IToolConf, conf if IPortalConf.providedBy(conf): return IPortalConf, conf if IGroupConf.providedBy(conf): return IGroupConf, conf if ICategoryConf.providedBy(conf): return ICategoryConf, conf if IModuleConf.providedBy(conf): return IModuleConf, conf if IWidgetConf.providedBy(conf): return IWidgetConf, conf if IWfProcessConf.providedBy(conf): return IWfProcessConf, conf if IWfStateConf.providedBy(conf): return IWfStateConf, conf if IWfTransitionConf.providedBy(conf): return IWfTransitionConf, conf if IConf.providedBy(conf): return IConf, conf return None, conf