Пример #1
0
    def make_webassets_env_from_config(self, options, config):
        settings = {}
        settings.update(options)
        settings.update(
            coerce_config(
                config, 'webassets.', {
                    'debug': asbool,
                    'auto_build': asbool,
                    'manifest': self.string_or_bool,
                    'url_expire': asbool,
                    'cache': self.string_or_bool,
                    'load_path': aslist
                }))

        static_files_path = config['paths']['static_files']
        asset_dir = settings.pop('base_dir', static_files_path)

        asset_url = settings.pop('base_url', '/')
        if not asset_url.startswith('/'):
            if urlparse(asset_url).scheme == '':
                asset_url = '/' + asset_url

        assets_env = Environment(asset_dir, asset_url, **settings)

        bundles = self.options.get('bundles', {})
        for name, value in bundles.items():
            if isinstance(value, Bundle):
                assets_env.register(name, value)
            else:
                # If it's not a bundle consider it a loader
                assets_env.register(value.load_bundles())

        return assets_env
Пример #2
0
    def setup_ming(self):
        """Setup MongoDB database engine using Ming"""
        try:
            from ming import create_datastore
            def create_ming_datastore(url, database, **kw):
                if database and url[-1] != '/':
                    url += '/'
                ming_url = url + database
                return create_datastore(ming_url, **kw)
        except ImportError: #pragma: no cover
            from ming.datastore import DataStore
            def create_ming_datastore(url, database, **kw):
                return DataStore(url, database=database, **kw)

        def mongo_read_pref(value):
            from pymongo.read_preferences import ReadPreference
            return getattr(ReadPreference, value)

        datastore_options = coerce_config(config, 'ming.connection.', {'max_pool_size':asint,
                                                                       'network_timeout':asint,
                                                                       'tz_aware':asbool,
                                                                       'safe':asbool,
                                                                       'journal':asbool,
                                                                       'wtimeout':asint,
                                                                       'fsync':asbool,
                                                                       'ssl':asbool,
                                                                       'read_preference':mongo_read_pref})
        datastore_options.pop('host', None)
        datastore_options.pop('port', None)

        datastore = create_ming_datastore(config['ming.url'], config.get('ming.db', ''), **datastore_options)
        config['pylons.app_globals'].ming_datastore = datastore
        self.package.model.init_model(datastore)
Пример #3
0
Файл: mako.py Проект: DINKIN/tg2
    def create(cls, config, app_globals):
        """
        Setup a renderer and loader for mako templates.
        """
        if mako is None:  # pragma: no cover
            return None

        use_dotted_templatenames = config.get('use_dotted_templatenames', True)

        options = coerce_config(config, 'templating.mako.', cls.CONFIG_OPTIONS)

        # If no dotted names support was required we will just setup
        # a file system based template lookup mechanism.
        compiled_dir = options.get('compiled_templates_dir', None)

        if not compiled_dir or compiled_dir.lower() in ('none', 'false'):
            # Cache compiled templates in-memory
            compiled_dir = None
        else:
            bad_path = None
            if os.path.exists(compiled_dir):
                if not os.access(compiled_dir, os.W_OK):
                    bad_path = compiled_dir
                    compiled_dir = None
            else:
                try:
                    os.makedirs(compiled_dir)
                except:
                    bad_path = compiled_dir
                    compiled_dir = None
            if bad_path:
                log.warn("Unable to write cached templates to %r; falling back "
                         "to an in-memory cache. Please set the `templating.mak"
                         "o.compiled_templates_dir` configuration option to a "
                         "writable directory." % bad_path)

        dotted_finder = app_globals.dotted_filename_finder
        if use_dotted_templatenames:
            # Support dotted names by injecting a slightly different template
            # lookup system that will return templates from dotted template notation.
            mako_lookup = DottedTemplateLookup(
                input_encoding='utf-8', output_encoding='utf-8',
                imports=['from markupsafe import escape_silent as escape'],
                package_name=config.package_name,
                dotted_finder=dotted_finder,
                module_directory=compiled_dir,
                default_filters=['escape'],
                auto_reload_templates=config.auto_reload_templates)

        else:
            mako_lookup = TemplateLookup(
                directories=config.paths['templates'],
                module_directory=compiled_dir,
                input_encoding='utf-8', output_encoding='utf-8',
                imports=['from markupsafe import escape_silent as escape'],
                default_filters=['escape'],
                filesystem_checks=config.auto_reload_templates)

        return {'mako': cls(dotted_finder, mako_lookup, use_dotted_templatenames)}
Пример #4
0
    def create(cls, config, app_globals):
        """Setup a renderer and loader for Genshi templates.

        Override this to customize the way that the internationalization
        filter, template loader

        """
        if genshi is None:  # pragma: no cover
            # Genshi not available
            return None

        options = coerce_config(config, 'templating.genshi.',
                                cls.CONFIG_OPTIONS)

        # Patch for Genshi on Python3.4
        if options.get('name_constant_patch', False):  # pragma: no cover
            from genshi.template.astutil import ASTCodeGenerator
            if not hasattr(ASTCodeGenerator, 'visit_NameConstant'):

                def _visit_NameConstant(self, node):
                    if node.value is None:
                        self._write('None')
                    elif node.value is True:
                        self._write('True')
                    elif node.value is False:
                        self._write('False')
                    else:
                        raise Exception("Unknown NameConstant %r" %
                                        (node.value, ))

                ASTCodeGenerator.visit_NameConstant = _visit_NameConstant

        if config.get('use_dotted_templatenames', True):
            TemplateLoader = DottedTemplateLoader
            template_loader_args = {
                'dotted_finder': app_globals.dotted_filename_finder
            }
        else:
            TemplateLoader = GenshiTemplateLoader
            template_loader_args = {}

        loader = TemplateLoader(search_path=config['paths'].templates,
                                max_cache_size=options.get(
                                    'max_cache_size',
                                    asint(
                                        config.get('genshi.max_cache_size',
                                                   30))),
                                auto_reload=config['auto_reload_templates'],
                                callback=cls.on_template_loaded,
                                **template_loader_args)

        return {'genshi': cls(loader, config)}
Пример #5
0
    def _configure_backlash(self, conf, app):
        trace_errors_config = coerce_config(conf, 'trace_errors.', {
            'enable': asbool,
            'smtp_use_tls': asbool,
            'dump_request_size': asint,
            'dump_request': asbool,
            'dump_local_frames': asbool,
            'dump_local_frames_count': asint
        })

        trace_errors_config.setdefault('debug', conf.get('debug', False))
        trace_errors_config.setdefault('error_subject_prefix', 'WebApp Error: ')
        trace_errors_config.setdefault('error_message', 'An internal server error occurred')
        conf['tg.errorware'] = trace_errors_config
Пример #6
0
    def _configure_backlash(self, conf, app):
        slowreqsware = coerce_config(conf, 'trace_slowreqs.', {'smtp_use_tls': asbool,
                                                               'dump_request_size': asint,
                                                               'dump_request': asbool,
                                                               'dump_local_frames': asbool,
                                                               'dump_local_frames_count': asint,
                                                               'enable': asbool,
                                                               'interval': asint,
                                                               'exclude': aslist})

        slowreqsware.setdefault('error_subject_prefix', 'Slow Request: ')
        slowreqsware.setdefault('error_message', 'A request is taking too much time')

        errorware = conf.get('tg.errorware', {})
        for erroropt in errorware:
            slowreqsware.setdefault(erroropt, errorware[erroropt])
        conf['tg.slowreqs'] = slowreqsware
    def _configure_backlash(self, conf, app):
        trace_errors_config = coerce_config(
            conf, 'trace_errors.', {
                'enable': asbool,
                'smtp_use_tls': asbool,
                'dump_request_size': asint,
                'dump_request': asbool,
                'dump_local_frames': asbool,
                'dump_local_frames_count': asint
            })

        trace_errors_config.setdefault('debug', conf.get('debug', False))
        trace_errors_config.setdefault('error_subject_prefix',
                                       'WebApp Error: ')
        trace_errors_config.setdefault('error_message',
                                       'An internal server error occurred')
        conf['tg.errorware'] = trace_errors_config
Пример #8
0
    def create(cls, config, app_globals):
        """Setup a renderer and loader for Genshi templates.

        Override this to customize the way that the internationalization
        filter, template loader

        """
        if genshi is None:  # pragma: no cover
            # Genshi not available
            return None

        options = coerce_config(config, 'templating.genshi.', cls.CONFIG_OPTIONS)

        # Patch for Genshi on Python3.4
        if options.get('name_constant_patch', False):
            from genshi.template.astutil import ASTCodeGenerator
            if not hasattr(ASTCodeGenerator, 'visit_NameConstant'):
                def _visit_NameConstant(self, node):
                    if node.value is None:
                        self._write('None')
                    elif node.value is True:
                        self._write('True')
                    elif node.value is False:
                        self._write('False')
                    else:
                        raise Exception("Unknown NameConstant %r" % (node.value,))
                ASTCodeGenerator.visit_NameConstant = _visit_NameConstant

        if config.get('use_dotted_templatenames', True):
            TemplateLoader = DottedTemplateLoader
            template_loader_args = {'dotted_finder': app_globals.dotted_filename_finder}
        else:
            TemplateLoader = GenshiTemplateLoader
            template_loader_args = {}

        loader = TemplateLoader(search_path=config['paths'].templates,
                                max_cache_size=options.get(
                                    'max_cache_size',
                                    asint(config.get('genshi.max_cache_size', 30))
                                ),
                                auto_reload=config['auto_reload_templates'],
                                callback=cls.on_template_loaded,
                                **template_loader_args)

        return {'genshi': cls(loader, config)}
Пример #9
0
    def _configure_backlash(self, conf, app):
        slowreqsware = coerce_config(
            conf, 'trace_slowreqs.', {
                'smtp_use_tls': asbool,
                'dump_request_size': asint,
                'dump_request': asbool,
                'dump_local_frames': asbool,
                'dump_local_frames_count': asint,
                'enable': asbool,
                'interval': asint,
                'exclude': aslist
            })

        slowreqsware.setdefault('error_subject_prefix', 'Slow Request: ')
        slowreqsware.setdefault('error_message',
                                'A request is taking too much time')

        errorware = conf.get('tg.errorware', {})
        for erroropt in errorware:
            slowreqsware.setdefault(erroropt, errorware[erroropt])
        conf['tg.slowreqs'] = slowreqsware
Пример #10
0
    def __init__(self, handler, config):
        super(MingApplicationWrapper, self).__init__(handler, config)

        options = {
            'autoflush': False,
        }
        options.update(coerce_config(config, 'ming.', {
            'autoflush': asbool,
        }))

        self.ThreadLocalODMSession = None
        self.enabled = options['autoflush']

        if self.enabled:
            try:
                from ming.odm import ThreadLocalODMSession
                self.ThreadLocalODMSession = ThreadLocalODMSession
            except ImportError:  # pragma: no cover
                log.exception('Unable to Enable Ming Application Wrapper')
                self.enabled = False

        log.debug('MingSessionFlush enabled: %s -> %s', self.enabled, options)
Пример #11
0
    def __init__(self, handler, config):
        super(MingApplicationWrapper, self).__init__(handler, config)

        options = {
            'autoflush': False,
        }
        options.update(coerce_config(config, 'ming.',  {
            'autoflush': asbool,
        }))

        self.ThreadLocalODMSession = None
        self.enabled = options['autoflush']

        if self.enabled:
            try:
                from ming.odm import ThreadLocalODMSession
                self.ThreadLocalODMSession = ThreadLocalODMSession
            except ImportError:  # pragma: no cover
                log.exception('Unable to Enable Ming Application Wrapper')
                self.enabled = False

        log.debug('MingSessionFlush enabled: %s -> %s',
                  self.enabled, options)
Пример #12
0
    def create(cls, config, app_globals):
        """
        Setup a renderer and loader for mako templates.
        """
        if mako is None:  # pragma: no cover
            return None

        use_dotted_templatenames = config.get('use_dotted_templatenames', True)

        options = coerce_config(config, 'templating.mako.', cls.CONFIG_OPTIONS)

        # If no dotted names support was required we will just setup
        # a file system based template lookup mechanism.
        compiled_dir = options.get('compiled_templates_dir', None)

        if not compiled_dir or compiled_dir.lower() in ('none', 'false'):
            # Cache compiled templates in-memory
            compiled_dir = None
        else:
            bad_path = None
            if os.path.exists(compiled_dir):
                if not os.access(compiled_dir, os.W_OK):
                    bad_path = compiled_dir
                    compiled_dir = None
            else:
                try:
                    os.makedirs(compiled_dir)
                except:
                    bad_path = compiled_dir
                    compiled_dir = None
            if bad_path:
                log.warn(
                    "Unable to write cached templates to %r; falling back "
                    "to an in-memory cache. Please set the `templating.mak"
                    "o.compiled_templates_dir` configuration option to a "
                    "writable directory." % bad_path)

        dotted_finder = app_globals.dotted_filename_finder
        if use_dotted_templatenames:
            # Support dotted names by injecting a slightly different template
            # lookup system that will return templates from dotted template notation.
            mako_lookup = DottedTemplateLookup(
                input_encoding='utf-8',
                output_encoding='utf-8',
                imports=['from markupsafe import escape_silent as escape'],
                package_name=config['package_name'],
                dotted_finder=dotted_finder,
                module_directory=compiled_dir,
                default_filters=['escape'],
                auto_reload_templates=config['auto_reload_templates'])

        else:
            mako_lookup = TemplateLookup(
                directories=config['paths']['templates'],
                module_directory=compiled_dir,
                input_encoding='utf-8',
                output_encoding='utf-8',
                imports=['from markupsafe import escape_silent as escape'],
                default_filters=['escape'],
                filesystem_checks=config['auto_reload_templates'])

        return {
            'mako': cls(dotted_finder, mako_lookup, use_dotted_templatenames)
        }
Пример #13
0
def test_coerce_config():
    conf = coerce_config({'ming.connection.max_pool_size':'5'}, 'ming.connection.', {'max_pool_size':asint})
    assert conf['max_pool_size'] == 5
Пример #14
0
def test_coerce_config():
    conf = coerce_config({'ming.connection.max_pool_size': '5'},
                         'ming.connection.', {'max_pool_size': asint})
    assert conf['max_pool_size'] == 5
Пример #15
0
    def create(cls, config, app_globals):
        """
        Setup a renderer and loader for mako templates.
        """
        if mako is None:  # pragma: no cover
            return None

        use_dotted_templatenames = config.get("use_dotted_templatenames", True)

        options = coerce_config(config, "templating.mako.", cls.CONFIG_OPTIONS)

        # If no dotted names support was required we will just setup
        # a file system based template lookup mechanism.
        compiled_dir = options.get("compiled_templates_dir", None)

        if not compiled_dir or compiled_dir.lower() in ("none", "false"):
            # Cache compiled templates in-memory
            compiled_dir = None
        else:
            bad_path = None
            if os.path.exists(compiled_dir):
                if not os.access(compiled_dir, os.W_OK):
                    bad_path = compiled_dir
                    compiled_dir = None
            else:
                try:
                    os.makedirs(compiled_dir)
                except:
                    bad_path = compiled_dir
                    compiled_dir = None
            if bad_path:
                log.warn(
                    "Unable to write cached templates to %r; falling back "
                    "to an in-memory cache. Please set the `templating.mak"
                    "o.compiled_templates_dir` configuration option to a "
                    "writable directory." % bad_path
                )

        template_extension = options.get("template_extension", ".mak")

        # Support dotted names by using a slightly different template
        # lookup system that will return templates from dotted template notation.
        dotted_loader = DottedTemplateLookup(
            input_encoding="utf-8",
            output_encoding="utf-8",
            imports=["from markupsafe import escape_silent as escape"],
            package_name=config["package_name"],
            find_template_file=lambda t: app_globals.dotted_filename_finder.get_dotted_filename(
                t, template_extension=template_extension
            ),
            template_extension=template_extension,
            module_directory=compiled_dir,
            default_filters=["escape"],
            auto_reload_templates=config["auto_reload_templates"],
        )

        normal_loader = TemplateLookup(
            directories=config["paths"]["templates"],
            module_directory=compiled_dir,
            input_encoding="utf-8",
            output_encoding="utf-8",
            imports=["from markupsafe import escape_silent as escape"],
            default_filters=["escape"],
            filesystem_checks=config["auto_reload_templates"],
        )

        return {"mako": cls(use_dotted_templatenames, template_extension, dotted_loader, normal_loader)}