def site_factory(request): """Application site factory On application startup, this factory checks configuration to get application name and load it from the ZODB; if the application can't be found, configuration is scanned to get application factory, create a new one and create a local site manager. """ conn = get_connection(request) root = conn.root() application_key = request.registry.settings.get(PYAMS_APPLICATION_SETTINGS_KEY, PYAMS_APPLICATION_DEFAULT_NAME) application = root.get(application_key) if application is None: factory = request.registry.settings.get(PYAMS_APPLICATION_FACTORY_KEY) if factory: resolver = DottedNameResolver() factory = resolver.maybe_resolve(factory) else: factory = request.registry.queryUtility(ISiteRootFactory, default=BaseSiteRoot) application = root[application_key] = factory() if IPossibleSite.providedBy(application): lsm = LocalSiteManager(application, default_folder=False) application.setSiteManager(lsm) try: # if some components require a valid and complete registry # with all registered utilities, they can subscribe to # INewLocalSiteCreatedEvent event interface set_local_registry(application.getSiteManager()) get_current_registry().notify(NewLocalSiteCreatedEvent(application)) finally: set_local_registry(None) import transaction # pylint: disable=import-outside-toplevel transaction.commit() return application
def importComponentRegistry(context): """Import local components. """ site = context.getSite() sm = None if IPossibleSite.providedBy(site): # All object managers are an IPossibleSite, but this # defines the getSiteManager method to be available try: sm = site.getSiteManager() except ComponentLookupError: sm = None if sm is None or not IComponentRegistry.providedBy(sm): logger = context.getLogger('componentregistry') logger.info("Can not register components, as no registry was found.") return importer = queryMultiAdapter((sm, context), IBody) if importer: body = context.readDataFile('componentregistry.xml') if body is not None: importer.body = body else: logger = context.getLogger('componentregistry') logger.debug("Nothing to import")
def create_app(db): conn = db.open() try: root = conn.root() if not name in root: with transaction_manager: app = root[name] = model() if (not ISite.providedBy(app) and IPossibleSite.providedBy(app)): LocalSiteManager(app) notify(events.ApplicationInitializedEvent(app)) finally: conn.close()
def enableSite(obj, iface=ISite): """Install __before_traverse__ hook for Local Site """ # We want the original object, not stuff in between, and no acquisition obj = aq_base(obj) if not IPossibleSite.providedBy(obj): raise TypeError('Must provide IPossibleSite') hook = NameCaller(HOOK_NAME) registerBeforeTraverse(obj, hook, HOOK_NAME, 1) if not hasattr(obj, HOOK_NAME): setattr(obj, HOOK_NAME, LocalSiteHook()) zope.interface.alsoProvides(obj, iface)
def exportComponentRegistry(context): """Export local components. """ site = context.getSite() sm = None if IPossibleSite.providedBy(site): # All object managers are an IPossibleSite, but this # defines the getSiteManager method to be available try: sm = site.getSiteManager() except ComponentLookupError: sm = None if sm is None or not IComponentRegistry.providedBy(sm): logger = context.getLogger('componentregistry') logger.debug("Nothing to export.") return exporter = queryMultiAdapter((sm, context), IBody) if exporter: body = exporter.body if body is not None: context.writeDataFile('componentregistry.xml', body, exporter.mime_type)
def initialize_applications(db, list_applications=list_applications, transaction_manager=transaction_manager): """ This utility setup applications in a database. The list of application to create is given by py:fun:list_applications For each application to create: - it creates the application thanks to the factory - it eventually makes it a LocalSiteManager You may use it as the :py:fun:init_db initializer parameter, or cook your own. :param db: the ZODB database :param list_applications: a callable that return a dict of applications factory indexed by their name :param transaction_manager: if not provided use :py:mod:transaction default one """ conn = db.open() try: root = conn.root() apps = list_applications() with transaction_manager: for name, factory in apps.items(): if not name in root: application = root[name] = factory() if (not ISite.providedBy(application) and IPossibleSite.providedBy(application)): LocalSiteManager(application) finally: conn.close()