Beispiel #1
0
    def __init__(self, config, application, **settings):
        super(RepozeTMMiddleware, self).__init__(config, application, **settings)

        commit_veto = settings.get('commit_veto')
        if commit_veto:
            commit_veto = get_object_on_path(commit_veto)

        self.repozetm = TM(self.application, commit_veto=commit_veto)
Beispiel #2
0
    def __init__(self, config, application, **settings):
        super(RepozeTMMiddleware, self).__init__(config, application,
                                                 **settings)

        commit_veto = settings.get('commit_veto')
        if commit_veto:
            commit_veto = get_object_on_path(commit_veto)

        self.repozetm = TM(self.application, commit_veto=commit_veto)
Beispiel #3
0
    def __init__(self, *args, **kwargs):
        super(BasePolicySessionManager, self).__init__(*args, **kwargs)

        if 'token.session_reference_path' not in self.settings:
            self.settings['token.session_reference_path'] = join_paths(
                self.settings['token.path'],
                'reference')

        # Jobs settings
        authorization = self.settings.get('authorization_session')
        if authorization:
            self.authorization_session = get_object_on_path(authorization)
        else:
            self.authorization_session = AuthenticatedSession
Beispiel #4
0
    def __init__(self, *args, **kwargs):
        super(BaseSQLSessionManager, self).__init__(*args, **kwargs)

        self.transaction = _import_module('transaction')

        session_extension = self.settings.get('session_extension')
        if session_extension is not None:
            session_extension = get_object_on_path(session_extension)

        self.db_session = initialize_sql(
            self.__database_name__,
            self.settings['sql_path'],
            encoding=self.settings.get('encoding', 'utf8'),
            mysql_engine=self.settings.get('mysql_engine') or 'InnoDB',
            session_extension=session_extension,
            debug=asbool(self.settings.get('debug', False)))
Beispiel #5
0
    def make_wsgi_app(self, install_middlewares=True):
        # Find for possible configuration extensions
        self.lookup_extensions()

        # Scan all package routes
        self.scan(self.package_name, categories=['pyramid'])
        app = super(APIConfigurator, self).make_wsgi_app()

        if install_middlewares:
            # Look for middlewares in API Sessions
            for name, extension in (
                    self.registry
                    .getUtilitiesFor(IBaseSessionManager)):
                if hasattr(extension, '__middlewares__'):
                    for extension_middleware in extension.__middlewares__:
                        self.install_middleware(
                            extension_middleware.name,
                            extension_middleware)

            # Define middleware settings
            middlewares_settings = MissingDict()
            for key, value in self.settings.items():
                if key.startswith('middleware.'):
                    maybe_name = key.split('middleware.', 1)[1]
                    if '.' in maybe_name:
                        name, setting_key = maybe_name.split('.', 1)
                        middlewares_settings[name][setting_key] = value
                    else:
                        # Install settings middlewares
                        middleware_class = get_object_on_path(value)
                        self.install_middleware(maybe_name, middleware_class)

            # Install middlewares with reversed order. Lower position first
            if self.middlewares:
                middlewares = []
                for name, middleware in self.middlewares:
                    settings = middlewares_settings[name]
                    default_position = (
                        getattr(middleware, 'position', DEFAULT_MIDDLEWARE_POSITION.get(name)))
                    position = settings.get('position', default_position) or 0
                    middlewares.append((position, name, middleware, settings))
                middlewares.sort(reverse=True)
                for position, name, middleware, settings in middlewares:
                    app = middleware(self, app, **settings)
                    app.name = name

        return app
Beispiel #6
0
    def make_wsgi_app(self, install_middlewares=True):
        # Find for possible configuration extensions
        self.lookup_extensions()

        # Scan all package routes
        self.scan(self.package_name, categories=['pyramid'])
        app = super(APIConfigurator, self).make_wsgi_app()

        if install_middlewares:
            # Look for middlewares in API Sessions
            for name, extension in (
                    self.registry.getUtilitiesFor(IBaseSessionManager)):
                if hasattr(extension, '__middlewares__'):
                    for extension_middleware in extension.__middlewares__:
                        self.install_middleware(extension_middleware.name,
                                                extension_middleware)

            # Define middleware settings
            middlewares_settings = MissingDict()
            for key, value in self.settings.items():
                if key.startswith('middleware.'):
                    maybe_name = key.split('middleware.', 1)[1]
                    if '.' in maybe_name:
                        name, setting_key = maybe_name.split('.', 1)
                        middlewares_settings[name][setting_key] = value
                    else:
                        # Install settings middlewares
                        middleware_class = get_object_on_path(value)
                        self.install_middleware(maybe_name, middleware_class)

            # Install middlewares with reversed order. Lower position first
            if self.middlewares:
                middlewares = []
                for name, middleware in self.middlewares:
                    settings = middlewares_settings[name]
                    default_position = (getattr(
                        middleware, 'position',
                        DEFAULT_MIDDLEWARE_POSITION.get(name)))
                    position = settings.get('position', default_position) or 0
                    middlewares.append((position, name, middleware, settings))
                middlewares.sort(reverse=True)
                for position, name, middleware, settings in middlewares:
                    app = middleware(self, app, **settings)
                    app.name = name

        return app
Beispiel #7
0
    def __init__(
            self,
            application_name=None,
            global_settings=None,
            **kwargs):

        if 'registry' in kwargs:
            for application_config in APPLICATIONS.values():
                if application_config.registry is kwargs['registry']:
                    # Nothing to do where. .scan() Configuration
                    super(Configurator, self).__init__(**kwargs)
                    return  # Nothing else to do where

        if 'package' in kwargs:
            # Nothing to do where.
            super(Configurator, self).__init__(**kwargs)
            return

        kwargs['package'] = caller_package()
        settings = kwargs['settings'] = dict(kwargs.get('settings') or {})
        kwargs['settings'].update(global_settings or {})

        # Define pyramid debugs
        settings['debug'] = asbool(settings.get('debug', False))
        if 'reload_all' not in settings:
            settings['reload_all'] = settings['debug']
        if 'debug_all' not in settings:
            settings['debug_all'] = settings['debug']
        if 'reload_templates' not in settings:
            settings['reload_templates'] = settings['debug']

        if 'root_factory' not in kwargs:
            kwargs['root_factory'] = RootFactory
        if 'request_factory' not in kwargs:
            kwargs['request_factory'] = InesRequest

        super(Configurator, self).__init__(**kwargs)

        self.registry.config = self
        self.registry.package_name = self.registry.__name__

        # Define application_name
        self.application_name = application_name or self.package_name
        self.registry.application_name = self.application_name

        # Define global cache
        cache_settings = dict(
            (key[6:], value)
            for key, value in self.settings.items()
            if key.startswith('cache.'))

        cache_type = cache_settings.pop('type', None)
        if cache_type == 'memcached':
            self.cache = SaveMeMemcached(**cache_settings)
        else:
            if 'path' not in cache_settings:
                cache_settings['path'] = DEFAULT_CACHE_DIRPATH
            self.cache = SaveMe(**cache_settings)

        # Find extensions on settings
        bases = APIWarningDict('Duplicated name "{key}" for API Class')
        sessions = APIWarningDict('Duplicated name "{key}" for API Session')
        for key, value in self.settings.items():
            if key.startswith('api.'):
                options = key.split('.', 2)[1:]
                if len(options) == 1:
                    name, option = options[0], 'session_path'
                else:
                    name, option = options
                if option == 'session_path':
                    if isinstance(value, string_types):
                        sessions[name] = get_object_on_path(value)
                    else:
                        sessions[name] = value
                elif option == 'class_path':
                    if isinstance(value, string_types):
                        bases[name] = get_object_on_path(value)
                    else:
                        bases[name] = value

        # Find sessions on module
        for session in find_class_on_module(self.package, BaseSession):
            app_name = getattr(session, '__app_name__', None)
            if not app_name or app_name == application_name:
                sessions[session.__api_name__] = session

        # Find session manager on module
        for session_manager in find_class_on_module(
                self.package,
                BaseSessionManager):
            app_name = getattr(session_manager, '__app_name__', None)
            if not app_name or app_name == application_name:
                bases[session_manager.__api_name__] = session_manager

        # Find default session manager
        default_bases = defaultdict(list)
        for session_manager in find_class_on_module('ines.api', BaseSessionManager):
            api_name = getattr(session_manager, '__api_name__', None)
            default_bases[api_name].append(session_manager)

        # Define extensions
        for api_name, session in sessions.items():
            session_manager = bases.get(api_name)
            if session_manager is None:
                session_manager = getattr(session, '__default_session_manager__', None)
                if session_manager is None:
                    default_session_managers = default_bases.get(api_name)
                    if not default_session_managers:
                        session_manager = BaseSessionManager
                    else:
                        session_manager = default_session_managers[0]

            self.registry.registerUtility(
                session_manager(self, session, api_name),
                provided=IBaseSessionManager,
                name=api_name)

        # Middlewares
        self.middlewares = []

        # Register package
        APPLICATIONS[self.application_name] = self

        # Default translations dirs
        self.add_translation_dirs('colander:locale')
        self.add_translation_dirs('ines:locale')
Beispiel #8
0
    def make_wsgi_app(self, install_middlewares=True):
        # Find for possible configuration extensions
        self.lookup_extensions()

        # Scan all package routes
        self.scan(self.package_name, categories=['pyramid'])

        # Scan package jobs
        scan_jobs = False
        jobs_manager = None
        for name, extension in self.registry.getUtilitiesFor(IBaseSessionManager):
            if issubclass(extension.session, BaseMailerSession) and 'queue_path' in extension.settings:
                scan_jobs = True
            elif issubclass(extension.session, BaseJobsSession):
                scan_jobs = True
                jobs_manager = extension
            elif isinstance(extension, BaseJobsManager):
                jobs_manager = extension
        if scan_jobs:
            if jobs_manager is None:
                raise ValueError('Please define module for jobs.')
            self.scan(self.package_name, categories=['ines.jobs'], jobs_manager=jobs_manager)
            self.scan('ines', categories=['ines.jobs'], jobs_manager=jobs_manager)

        app = super(Configurator, self).make_wsgi_app()

        if install_middlewares:
            # Look for middlewares in API Sessions
            for name, extension in self.registry.getUtilitiesFor(IBaseSessionManager):
                if hasattr(extension, '__middlewares__'):
                    for extension_middleware in extension.__middlewares__:
                        self.install_middleware(
                            extension_middleware.name,
                            extension_middleware,
                            settings={'api_manager': extension})

            # Define middleware settings
            middlewares_settings = defaultdict(dict)
            for key, value in self.settings.items():
                if key.startswith('middleware.'):
                    maybe_name = key.split('middleware.', 1)[1]
                    if '.' in maybe_name:
                        parts = maybe_name.split('.')
                        setting_key = parts[-1]
                        name = string_join('.', parts[:-1])
                        middlewares_settings[name][setting_key] = value
                    else:
                        # Install settings middlewares
                        middleware_class = get_object_on_path(value)
                        self.install_middleware(maybe_name, middleware_class)

            # Install middlewares with reversed order. Lower position first
            if self.middlewares:
                middlewares = []
                for name, middleware, settings in self.middlewares:
                    middlewares_settings[name].update(settings)

                    default_position = getattr(middleware, 'position', DEFAULT_MIDDLEWARE_POSITION.get(name))
                    position = settings.get('position', default_position) or 0
                    middlewares.append((position, name, middleware))
                middlewares.sort(reverse=True)

                for position, name, middleware in middlewares:
                    app = middleware(self, app, **middlewares_settings[name])
                    app.name = name

        return app
Beispiel #9
0
    def __init__(self, application_name=None, global_settings=None, **kwargs):

        if 'registry' in kwargs:
            for application_config in APPLICATIONS.values():
                if application_config.registry is kwargs['registry']:
                    # Nothing to do where. .scan() Configuration
                    super(APIConfigurator, self).__init__(**kwargs)
                    return  # Nothing else to do where

        if 'package' not in kwargs:
            kwargs['package'] = caller_package()

        settings = kwargs['settings'] = dict(kwargs.get('settings') or {})
        kwargs['settings'].update(global_settings or {})

        # Define pyramid debugs
        settings['debug'] = asbool(settings.get('debug', False))
        if 'reload_all' not in settings:
            settings['reload_all'] = settings['debug']
        if 'debug_all' not in settings:
            settings['debug_all'] = settings['debug']
        if 'reload_templates' not in settings:
            settings['reload_templates'] = settings['debug']

        if 'root_factory' not in kwargs:
            kwargs['root_factory'] = RootFactory
        if 'request_factory' not in kwargs:
            kwargs['request_factory'] = inesRequest

        super(APIConfigurator, self).__init__(**kwargs)

        self.registry.config = self
        self.registry.package_name = self.registry.__name__

        # Define application_name
        self.application_name = application_name or self.package_name
        self.registry.application_name = self.application_name

        # Find extensions on settings
        bases = WarningDict('Duplicated name "{key}" for API Class')
        sessions = WarningDict('Duplicated name "{key}" for API Session')
        for key, value in self.settings.items():
            if key.startswith('api.extension.'):
                options = key.split('.', 3)[2:]
                if len(options) == 1:
                    name, option = options[0], 'session_path'
                else:
                    name, option = options

                if option == 'session_path':
                    if isinstance(value, basestring):
                        sessions[name] = get_object_on_path(value)
                    else:
                        sessions[name] = value
                elif option == 'class_path':
                    if isinstance(value, basestring):
                        bases[name] = get_object_on_path(value)
                    else:
                        bases[name] = value

        # Find sessions on module
        for session in find_class_on_module(self.package, BaseSession):
            app_name = getattr(session, '__app_name__', None)
            if not app_name or app_name == application_name:
                sessions[session.__api_name__] = session

        # Find class on module
        for session_class in find_class_on_module(self.package,
                                                  BaseSessionManager):
            app_name = getattr(session_class, '__app_name__', None)
            if not app_name or app_name == application_name:
                bases[session_class.__api_name__] = session_class

        # Find default session class
        for session_class in find_class_on_module('ines.api',
                                                  BaseSessionManager):
            if session_class.__api_name__ not in bases:
                app_name = getattr(session_class, '__app_name__', None)
                if not app_name or app_name == application_name:
                    bases[session_class.__api_name__] = session_class

        # Define extensions
        for api_name, session in sessions.items():
            session_class = (bases.get(api_name,
                                       BaseSessionManager)(self, session,
                                                           api_name))

            self.registry.registerUtility(session_class,
                                          provided=IBaseSessionManager,
                                          name=api_name)

        # Middlewares
        self.middlewares = []

        # Register package
        APPLICATIONS[self.application_name] = self

        # Default translations dirs
        self.add_translation_dirs('colander:locale')
Beispiel #10
0
    def __init__(
            self,
            application_name=None,
            global_settings=None,
            **kwargs):

        if 'registry' in kwargs:
            for application_config in APPLICATIONS.values():
                if application_config.registry is kwargs['registry']:
                    # Nothing to do where. .scan() Configuration
                    super(APIConfigurator, self).__init__(**kwargs)
                    return  # Nothing else to do where

        if 'package' not in kwargs:
            kwargs['package'] = caller_package()

        settings = kwargs['settings'] = dict(kwargs.get('settings') or {})
        kwargs['settings'].update(global_settings or {})

        # Define pyramid debugs
        settings['debug'] = asbool(settings.get('debug', False))
        if 'reload_all' not in settings:
            settings['reload_all'] = settings['debug']
        if 'debug_all' not in settings:
            settings['debug_all'] = settings['debug']
        if 'reload_templates' not in settings:
            settings['reload_templates'] = settings['debug']

        if 'root_factory' not in kwargs:
            kwargs['root_factory'] = RootFactory
        if 'request_factory' not in kwargs:
            kwargs['request_factory'] = inesRequest

        super(APIConfigurator, self).__init__(**kwargs)

        self.registry.config = self
        self.registry.package_name = self.registry.__name__

        # Define application_name
        self.application_name = application_name or self.package_name
        self.registry.application_name = self.application_name

        # Find extensions on settings
        bases = WarningDict('Duplicated name "{key}" for API Class')
        sessions = WarningDict('Duplicated name "{key}" for API Session')
        for key, value in self.settings.items():
            if key.startswith('api.extension.'):
                options = key.split('.', 3)[2:]
                if len(options) == 1:
                    name, option = options[0], 'session_path'
                else:
                    name, option = options

                if option == 'session_path':
                    if isinstance(value, basestring):
                        sessions[name] = get_object_on_path(value)
                    else:
                        sessions[name] = value
                elif option == 'class_path':
                    if isinstance(value, basestring):
                        bases[name] = get_object_on_path(value)
                    else:
                        bases[name] = value

        # Find sessions on module
        for session in find_class_on_module(self.package, BaseSession):
            app_name = getattr(session, '__app_name__', None)
            if not app_name or app_name == application_name:
                sessions[session.__api_name__] = session

        # Find class on module
        for session_class in find_class_on_module(
                self.package,
                BaseSessionManager):
            app_name = getattr(session_class, '__app_name__', None)
            if not app_name or app_name == application_name:
                bases[session_class.__api_name__] = session_class

        # Find default session class
        for session_class in find_class_on_module(
                'ines.api',
                BaseSessionManager):
            if session_class.__api_name__ not in bases:
                app_name = getattr(session_class, '__app_name__', None)
                if not app_name or app_name == application_name:
                    bases[session_class.__api_name__] = session_class

        # Define extensions
        for api_name, session in sessions.items():
            session_class = (
                bases.get(api_name, BaseSessionManager)
                (self, session, api_name))

            self.registry.registerUtility(
                session_class,
                provided=IBaseSessionManager,
                name=api_name)

        # Middlewares
        self.middlewares = []

        # Register package
        APPLICATIONS[self.application_name] = self

        # Default translations dirs
        self.add_translation_dirs('colander:locale')