예제 #1
0
def initialize_jinja_env(cache_dir):
    jinja_env = jinja2.Environment(
        loader=jinja2.FileSystemLoader(TEMPLATES_DIR),
        # Bytecode cache is not concurrency-safe unless pre-cached:
        # if pre-cached this is read-only, but writing creates a race condition.
        bytecode_cache=jinja2.FileSystemBytecodeCache(cache_dir),
        keep_trailing_newline=True,  # newline-terminate generated files
        lstrip_blocks=True,  # so can indent control flow tags
        trim_blocks=True)
    jinja_env.filters.update({
        'blink_capitalize':
        capitalize,
        'exposed':
        exposed_if,
        'format_blink_cpp_source_code':
        format_blink_cpp_source_code,
        'format_remove_duplicates':
        format_remove_duplicates,
        'origin_trial_enabled':
        origin_trial_enabled_if,
        'runtime_enabled':
        runtime_enabled_if,
        'runtime_enabled_function':
        v8_utilities.runtime_enabled_function,
        'secure_context':
        secure_context_if
    })
    jinja_env.filters.update(constant_filters())
    jinja_env.filters.update(method_filters())
    return jinja_env
예제 #2
0
 def build_environment(self):
     """
     Buids a :class:`jinja2.Environment` and registers all functions,
     filters and global variables. No need to call this function manually,
     an environment will be created automatically if it does not exist when
     accessing :attr:`.env`.
     """
     kwargs = {}
     if self._jinja2_conf.cachedir:
         kwargs['bytecode_cache'] =\
             jinja2.FileSystemBytecodeCache(self._jinja2_conf.cachedir)
     can_escape = self.filetype.mimetype in ('text/xml', 'text/html')
     env = jinja2.Environment(autoescape=can_escape,
                              extensions=self.get_extensions(),
                              undefined=jinja2.StrictUndefined,
                              loader=Jinja2Loader(self._jinja2_conf,
                                                  self._tpl_conf),
                              **kwargs)
     if can_escape:
         for name, value, escape in self.filetype.globals:
             if not escape:
                 if callable(value):
                     value = _wrap_callable(value)
                     if name in self._jinja2_conf.filters:
                         env.filters[name] = value
                         continue
                 elif isinstance(value, str):
                     value = jinja2.Markup(value)
             env.globals[name] = value
     else:
         for name, value, escape in self.filetype.globals:
             env.globals[name] = value
     return env
예제 #3
0
 def __init__(self, routing: Routing):
     self._routing = routing
     self._template_dir: str = os.path.realpath(os.path.join(
         os.path.dirname(__file__), '..', '..', 'templates'))
     tpl_cache_path = settings.get('global', 'template_engine_cache_path', None)
     cache = jinja2.FileSystemBytecodeCache(tpl_cache_path) if tpl_cache_path else None
     self._template_env: jinja2.Environment = jinja2.Environment(
         loader=jinja2.FileSystemLoader(searchpath=self._template_dir),
         bytecode_cache=cache,
         trim_blocks=True,
         lstrip_blocks=True)
     self._template_env.filters.update(
         to_json=val_to_js,
         shorten=strings.shorten,
         camelize=l10n.camelize,
         _=translat_filter,
         xmle=escape,
         create_action=lambda a, p=None: self._routing.create_url(a, p if p is not None else {})
     )
     self._redirect_safe_domains: Tuple[str, ...] = (
         urlparse(self._routing.get_root_url()).netloc,
         *settings.get('global', 'redirect_safe_domains', ())
     )
     self._status: int = 200
     self._headers: Dict[str, str] = {'Content-Type': 'text/html'}
     self._new_cookies: KonTextCookie = KonTextCookie()
예제 #4
0
def initialize_jinja_env(jinja_dir, cache_dir, config):
    try:
        # We don't need to modify |sys.path| if we build with bazel.
        import jinja2
    except ImportError:
        # pylint: disable=F0401
        sys.path.insert(1, os.path.abspath(jinja_dir))
        import jinja2

    jinja_env = jinja2.Environment(
        loader=jinja2.FileSystemLoader(module_path),
        # Bytecode cache is not concurrency-safe unless pre-cached:
        # if pre-cached this is read-only, but writing creates a race condition.
        bytecode_cache=jinja2.FileSystemBytecodeCache(cache_dir),
        keep_trailing_newline=True,  # newline-terminate generated files
        lstrip_blocks=True,  # so can indent control flow tags
        trim_blocks=True)
    jinja_env.filters.update({
        "to_title_case":
        to_title_case,
        "dash_to_camelcase":
        dash_to_camelcase,
        "to_method_case":
        functools.partial(to_method_case, config)
    })
    jinja_env.add_extension("jinja2.ext.loopcontrols")
    return jinja_env
예제 #5
0
def _get_jinja_environment(substitutions_dict):
    if _get_jinja_environment.env is None:
        bytecode_cache = None
        if substitutions_dict.get("jinja2_cache_enabled") == "true":
            bytecode_cache = jinja2.FileSystemBytecodeCache(
                required_key(substitutions_dict, "jinja2_cache_dir"))

        # TODO: Choose better syntax?
        _get_jinja_environment.env = jinja2.Environment(
            block_start_string="{{%",
            block_end_string="%}}",
            variable_start_string="{{{",
            variable_end_string="}}}",
            comment_start_string="{{#",
            comment_end_string="#}}",
            loader=AbsolutePathFileSystemLoader(),
            bytecode_cache=bytecode_cache)
        _get_jinja_environment.env.filters['banner_regexify'] = banner_regexify
        _get_jinja_environment.env.filters[
            'banner_anchor_wrap'] = banner_anchor_wrap
        _get_jinja_environment.env.filters['escape_regex'] = escape_regex
        _get_jinja_environment.env.filters['escape_id'] = escape_id
        _get_jinja_environment.env.filters['escape_yaml_key'] = escape_yaml_key

    return _get_jinja_environment.env
예제 #6
0
    def __init__(self, name, **vars):
        if not os.path.exists(CACHE_DIR): os.makedirs(CACHE_DIR, 0o755)
        # umask a shit
        if os.path.isdir(CACHE_DIR): os.chmod(CACHE_DIR, 0o755)

        # Environment init
        self.env = jinja2.Environment(
            loader=jinja2.FileSystemLoader(TEMPLATES_DIR),
            bytecode_cache=jinja2.FileSystemBytecodeCache(CACHE_DIR))

        for filter in _filters:
            self.env.filters[filter] = getattr(self, filter)

        for function in _functions:
            self.env.globals[function] = getattr(self, function)

        # Current template init
        self.template = self.env.get_template(name + '.html')

        self.environ = local.environ
        self.board = self.environ['waka.board']

        vars['environ'] = self.environ
        vars['board'] = self.board
        vars['stylesheets'] = list(self.get_stylesheets(self.board))
        self.env.globals['config'] = config
        self.env.globals['strings'] = strings

        self.vars = vars
예제 #7
0
 def __init__(self,
              templates,
              default='layout',
              into='contents',
              cache='.cache'):
     self.env = jinja2.Environment(
         loader=jinja2.FileSystemLoader(templates),
         bytecode_cache=jinja2.FileSystemBytecodeCache(cache),
         extensions=['jinja2.ext.with_'])
     self.default = default
     self.into = into
예제 #8
0
파일: app.py 프로젝트: kl-sinclair/isucon8
class CustomFlask(flask.Flask):
    jinja_options = flask.Flask.jinja_options.copy()
    jinja_options.update(dict(
        block_start_string='(%',
        block_end_string='%)',
        variable_start_string='((',
        variable_end_string='))',
        comment_start_string='(#',
        comment_end_string='#)',
        bytecode_cache=jinja2.FileSystemBytecodeCache(directory=TEMPLATE_CACHE)
    ))
예제 #9
0
def initialize_jinja_env(cache_dir):
    jinja_env = jinja2.Environment(
        loader=jinja2.FileSystemLoader(templates_dir),
        # Bytecode cache is not concurrency-safe unless pre-cached:
        # if pre-cached this is read-only, but writing creates a race
        # condition.
        bytecode_cache=jinja2.FileSystemBytecodeCache(cache_dir),
        keep_trailing_newline=True,  # newline-terminate generated files
        lstrip_blocks=True,  # so can indent control flow tags
        trim_blocks=True)
    return jinja_env
예제 #10
0
def initialize_jinja_env(cache_dir):
    jinja_env = jinja2.Environment(
        loader=jinja2.FileSystemLoader(templates_dir),
        # Bytecode cache is not concurrency-safe unless pre-cached:
        # if pre-cached this is read-only, but writing creates a race condition.
        bytecode_cache=jinja2.FileSystemBytecodeCache(cache_dir),
        keep_trailing_newline=True,  # newline-terminate generated files
        lstrip_blocks=True,  # so can indent control flow tags
        trim_blocks=True)
    jinja_env.filters.update({"to_title_case": to_title_case, "dash_to_camelcase": dash_to_camelcase})
    jinja_env.add_extension('jinja2.ext.loopcontrols')
    return jinja_env
예제 #11
0
def load_template_env(temp_folder, template_folder):
    """Load template environment (lazily)"""
    global _template_env
    if not _template_env:
        # Cache folder for jinja2
        tmpl_cache = os.path.join(temp_folder, 'jinja2_cache')
        if not os.path.isdir(tmpl_cache):
            os.mkdir(tmpl_cache)
        # Create jinja2 environment
        _template_env = jinja2.Environment(
            loader=jinja2.FileSystemLoader(template_folder),
            auto_reload=False,
            bytecode_cache=jinja2.FileSystemBytecodeCache(tmpl_cache))
    return _template_env
def initialize_jinja_env(cache_dir):
    jinja_env = jinja2.Environment(
        loader=jinja2.FileSystemLoader(templates_dir),
        # Bytecode cache is not concurrency-safe unless pre-cached:
        # if pre-cached this is read-only, but writing creates a race condition.
        bytecode_cache=jinja2.FileSystemBytecodeCache(cache_dir),
        keep_trailing_newline=True,  # newline-terminate generated files
        lstrip_blocks=True,  # so can indent control flow tags
        trim_blocks=True)
    jinja_env.filters.update({
        'blink_capitalize': capitalize,
        'conditional': conditional_if_endif,
        'runtime_enabled': runtime_enabled_if,
    })
    return jinja_env
예제 #13
0
파일: utils.py 프로젝트: imclab/dxr
def load_template_env(temp_folder, dxr_root):
    """Load template environment (lazily)"""
    global _template_env
    if not _template_env:
        # Cache folder for jinja2
        tmpl_cache = os.path.join(temp_folder, 'jinja2_cache')
        if not os.path.isdir(tmpl_cache):
            os.mkdir(tmpl_cache)
        # Create jinja2 environment
        _template_env = jinja2.Environment(
            loader=jinja2.FileSystemLoader(join(dxr_root, TEMPLATE_DIR)),
            auto_reload=False,
            bytecode_cache=jinja2.FileSystemBytecodeCache(tmpl_cache),
            autoescape=lambda template_name: template_name is None or
            template_name.endswith('.html'))
    return _template_env
예제 #14
0
def initialize_jinja_env(config, cache_dir):
    jinja_env = jinja2.Environment(
        loader=jinja2.FileSystemLoader(os.path.join(_MODULE_PATH, "templates")),
        # Bytecode cache is not concurrency-safe unless pre-cached:
        # if pre-cached this is read-only, but writing creates a race condition.
        bytecode_cache=jinja2.FileSystemBytecodeCache(cache_dir),
        keep_trailing_newline=True,  # newline-terminate generated files
        lstrip_blocks=True,  # so can indent control flow tags
        trim_blocks=True)
    jinja_env.filters.update({
        "to_lower_case": to_lower_case,
        "to_singular": to_singular,
        "agent_name_to_class": partial(agent_name_to_class, config),
        "agent_name_to_include": partial(agent_name_to_include, config)})
    jinja_env.add_extension('jinja2.ext.loopcontrols')
    return jinja_env
예제 #15
0
파일: jinja.py 프로젝트: Determinant/nikola
 def set_directories(self, directories, cache_folder):
     """Create a new template lookup with set directories."""
     if jinja2 is None:
         req_missing(['jinja2'], 'use this theme')
     cache_folder = os.path.join(cache_folder, 'jinja')
     makedirs(cache_folder)
     cache = jinja2.FileSystemBytecodeCache(cache_folder)
     self.lookup = jinja2.Environment(bytecode_cache=cache)
     self.lookup.trim_blocks = True
     self.lookup.lstrip_blocks = True
     self.lookup.filters['tojson'] = json.dumps
     self.lookup.globals['enumerate'] = enumerate
     self.lookup.globals['isinstance'] = isinstance
     self.lookup.globals['tuple'] = tuple
     self.directories = directories
     self.create_lookup()
예제 #16
0
def InitializeJinjaEnv(cache_dir):
    jinja_env = jinja2.Environment(
        loader=jinja2.FileSystemLoader(templates_dir),
        # Bytecode cache is not concurrency-safe unless pre-cached:
        # if pre-cached this is read-only, but writing creates a race condition.
        bytecode_cache=jinja2.FileSystemBytecodeCache(cache_dir),
        keep_trailing_newline=True,  # Newline-terminate generated files.
        lstrip_blocks=True,  # So we can indent control flow tags.
        trim_blocks=True)
    jinja_env.filters.update({
        'to_title_case': ToTitleCase,
        'dash_to_camelcase': DashToCamelCase,
        'camelcase_to_hacker_style': CamelCaseToHackerStyle,
        'sanitize_literal': SanitizeLiteral,
    })
    jinja_env.add_extension('jinja2.ext.loopcontrols')
    return jinja_env
예제 #17
0
파일: jinja.py 프로젝트: sturem/content
def _get_jinja_environment(substitutions_dict):
    if _get_jinja_environment.env is None:
        bytecode_cache = None
        if substitutions_dict.get("jinja2_cache_enabled") == "true":
            bytecode_cache = jinja2.FileSystemBytecodeCache(
                required_key(substitutions_dict, "jinja2_cache_dir"))

        # TODO: Choose better syntax?
        _get_jinja_environment.env = jinja2.Environment(
            block_start_string="{{%",
            block_end_string="%}}",
            variable_start_string="{{{",
            variable_end_string="}}}",
            comment_start_string="{{#",
            comment_end_string="#}}",
            loader=AbsolutePathFileSystemLoader(),
            bytecode_cache=bytecode_cache)

    return _get_jinja_environment.env
예제 #18
0
    def jinja_options(self) -> Dict[str, Any]:
        options = dict(Flask.jinja_options)

        jinja_exts = options.setdefault("extensions", [])
        ext = "abilian.core.extensions.jinjaext.DeferredJSExtension"
        if ext not in jinja_exts:
            jinja_exts.append(ext)

        if "bytecode_cache" not in options:
            cache_dir = Path(self.instance_path, "cache", "jinja")
            if not cache_dir.exists():
                cache_dir.mkdir(0o775, parents=True)

            options["bytecode_cache"] = jinja2.FileSystemBytecodeCache(
                str(cache_dir), "%s.cache")

        if self.debug and self.config.get("TEMPLATE_DEBUG", False):
            options["undefined"] = jinja2.StrictUndefined
        return options
예제 #19
0
def startup(app, config, locales):
    templates_path = app['config']['templates_path']
    env = aiohttp_jinja2.setup(
        app,
        context_processors=[
            computed_processor(debug=app.debug),
            config_processor(config=app['config']),
            locales_processor(locales_config=config.locales_config,
                              locales=locales),
            fonts_processor(locales_config=config.locales_config,
                            fonts_config=config.fonts_config),
        ],
        default_helpers=False,
        filters={
            'squash': _squash,
        },
        loader=jinja2.FileSystemLoader(templates_path),
        auto_reload=False,
        bytecode_cache=jinja2.FileSystemBytecodeCache())
예제 #20
0
파일: __init__.py 프로젝트: dlukes/kontext
    def __init__(self, request: werkzeug.wrappers.Request, ui_lang: str):
        """
        arguments:
        request -- Werkzeug's request object
        ui_lang -- language used by user
        """
        self._request: werkzeug.wrappers.Request = request
        self.environ: Dict[str, str] = self._request.environ  # for backward compatibility
        self.ui_lang: str = ui_lang
        self._cookies: KonTextCookie = KonTextCookie(self.environ.get('HTTP_COOKIE', ''))
        self._new_cookies: KonTextCookie = KonTextCookie()
        self._headers: Dict[str, str] = {'Content-Type': 'text/html'}
        self._status: int = 200
        self._system_messages: List[Tuple[str, str]] = []
        self._proc_time: Optional[float] = None
        # a list of functions which must pass (= return None) before any action is performed
        self._validators: List[Callable[[], Exception]] = []
        # templating engine
        self._template_dir: str = os.path.realpath(os.path.join(
            os.path.dirname(__file__), '..', '..', 'templates'))
        tpl_cache_path = settings.get('global', 'template_engine_cache_path', None)
        cache = jinja2.FileSystemBytecodeCache(tpl_cache_path) if tpl_cache_path else None
        self._template_env: jinja2.Environment = jinja2.Environment(
            loader=jinja2.FileSystemLoader(searchpath=self._template_dir),
            bytecode_cache=cache,
            trim_blocks=True,
            lstrip_blocks=True)
        self._template_env.filters.update(
            to_json=val_to_js,
            shorten=strings.shorten,
            camelize=l10n.camelize,
            _=translat_filter,
            xmle=escape,
            create_action=lambda a, p=None: self.create_url(a, p if p is not None else {})
        )
        ##
        self.args: Args = Args()
        self._uses_valid_sid: bool = True
        self._plugin_api: Optional[PluginApi] = None  # must be implemented in a descendant

        # initialize all the Parameter attributes
        for k, value in inspect.getmembers(GlobalArgs, predicate=lambda m: isinstance(m, Parameter)):
            setattr(self.args, k, value.unwrap())
예제 #21
0
def initialize_jinja_env(cache_dir):
    jinja_env = jinja2.Environment(
        loader=jinja2.FileSystemLoader(templates_dir),
        # Bytecode cache is not concurrency-safe unless pre-cached:
        # if pre-cached this is read-only, but writing creates a race condition.
        bytecode_cache=jinja2.FileSystemBytecodeCache(cache_dir),
        keep_trailing_newline=True,  # newline-terminate generated files
        lstrip_blocks=True,  # so can indent control flow tags
        trim_blocks=True)
    jinja_env.filters.update({
        'blink_capitalize': capitalize,
        'exposed': exposed_if,
        'for_origin_trial_feature': for_origin_trial_feature,
        'runtime_enabled': runtime_enabled_if,
        'unique_by': unique_by,
    })
    jinja_env.filters.update(attribute_filters())
    jinja_env.filters.update(v8_interface.constant_filters())
    jinja_env.filters.update(method_filters())
    return jinja_env
예제 #22
0
    def jinja_options(self):
        options = dict(Flask.jinja_options)

        extensions = options.setdefault('extensions', [])
        ext = 'abilian.core.jinjaext.DeferredJSExtension'
        if ext not in extensions:
            extensions.append(ext)

        if 'bytecode_cache' not in options:
            cache_dir = Path(self.instance_path, 'cache', 'jinja')
            if not cache_dir.exists():
                cache_dir.mkdir(0o775, parents=True)

            options['bytecode_cache'] = jinja2.FileSystemBytecodeCache(
                str(cache_dir), '%s.cache')

        if (self.config.get('DEBUG', False)
                and self.config.get('TEMPLATE_DEBUG', False)):
            options['undefined'] = jinja2.StrictUndefined
        return options
예제 #23
0
def initialize_jinja_env(jinja_dir, cache_dir):
    dir = os.path.abspath(jinja_dir)
    # pylint: disable=F0401
    sys.path.insert(1, os.path.join(dir, "jinja2"))
    sys.path.insert(1, os.path.join(dir, "markupsafe"))
    import jinja2

    jinja_env = jinja2.Environment(
        loader=jinja2.FileSystemLoader(module_path),
        # Bytecode cache is not concurrency-safe unless pre-cached:
        # if pre-cached this is read-only, but writing creates a race condition.
        bytecode_cache=jinja2.FileSystemBytecodeCache(cache_dir),
        keep_trailing_newline=True,  # newline-terminate generated files
        lstrip_blocks=True,  # so can indent control flow tags
        trim_blocks=True)
    jinja_env.filters.update({
        "to_title_case": to_title_case,
        "dash_to_camelcase": dash_to_camelcase
    })
    jinja_env.add_extension("jinja2.ext.loopcontrols")
    return jinja_env
예제 #24
0
    def _configure_templating(cls, app):
        cachedir = app.config["PYLOAD_API"].get_cachedir()
        cache_path = os.path.join(cachedir, "jinja")

        os.makedirs(cache_path, exist_ok=True)

        app.create_jinja_environment()

        # NOTE: enable autoescape for all file extensions (included .js)
        #       maybe this will break .txt rendering, but we don't render this kind of files actually
        #       that does not change 'default_for_string=False' (by default)
        app.jinja_env.autoescape = jinja2.select_autoescape(default=True)
        app.jinja_env.bytecode_cache = jinja2.FileSystemBytecodeCache(cache_path)

        for fn in cls.JINJA_TEMPLATE_FILTERS:
            app.add_template_filter(fn)

        for fn in cls.JINJA_TEMPLATE_GLOBALS:
            app.add_template_global(fn)

        for fn in cls.JINJA_CONTEXT_PROCESSORS:
            app.context_processor(fn)
예제 #25
0
def setup(app: aiohttp.web.Application) -> None:
    """Setup the aiohttp_jinja2 environment"""
    if sys.platform == "linux":
        try:
            cachedir = pathlib.Path(os.environ["XDG_CACHE_HOME"])
        except KeyError:
            cachedir = pathlib.Path.home() / ".cache"
    elif sys.platform == "win32":
        cachedir = pathlib.Path(os.environ["TEMP"])
    else:
        cachedir = pathlib.Path.home()
    cachedir /= doorpi.metadata.distribution.metadata["Name"]
    cachedir /= "templatecache"
    cachedir.mkdir(parents=True, exist_ok=True)

    aiohttp_jinja2.setup(
        app,
        loader=templates.DoorPiWebTemplateLoader(),
        bytecode_cache=jinja2.FileSystemBytecodeCache(cachedir),
        undefined=jinja2.StrictUndefined,
        enable_async=True,
    )
예제 #26
0
def update_config():
    ''' This code needs to be run when the config is changed to take those
    changes into account. It is called whenever a plugin is loaded as the
    plugin might have changed the config values (for instance it might
    change ckan.site_url) '''

    for plugin in p.PluginImplementations(p.IConfigurer):
        # must do update in place as this does not work:
        # config = plugin.update_config(config)
        plugin.update_config(config)

    # Set whitelisted env vars on config object
    # This is set up before globals are initialized

    ckan_db = os.environ.get('CKAN_DB', None)
    if ckan_db:
        msg = 'Setting CKAN_DB as an env var is deprecated and will be' \
            ' removed in a future release. Use CKAN_SQLALCHEMY_URL instead.'
        log.warn(msg)
        config['sqlalchemy.url'] = ckan_db

    for option in CONFIG_FROM_ENV_VARS:
        from_env = os.environ.get(CONFIG_FROM_ENV_VARS[option], None)
        if from_env:
            config[option] = from_env

    root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

    site_url = config.get('ckan.site_url', '')
    if not site_url:
        raise RuntimeError(
            'ckan.site_url is not configured and it must have a value.'
            ' Please amend your .ini file.')
    if not site_url.lower().startswith('http'):
        raise RuntimeError(
            'ckan.site_url should be a full URL, including the schema '
            '(http or https)')

    display_timezone = config.get('ckan.display_timezone', '')
    if (display_timezone and display_timezone != 'server'
            and display_timezone not in pytz.all_timezones):
        raise CkanConfigurationException(
            "ckan.display_timezone is not 'server' or a valid timezone")

    # Remove backslash from site_url if present
    config['ckan.site_url'] = config['ckan.site_url'].rstrip('/')

    ckan_host = config['ckan.host'] = urlparse(site_url).netloc
    if config.get('ckan.site_id') is None:
        if ':' in ckan_host:
            ckan_host, port = ckan_host.split(':')
        assert ckan_host, 'You need to configure ckan.site_url or ' \
                          'ckan.site_id for SOLR search-index rebuild to work.'
        config['ckan.site_id'] = ckan_host

    # ensure that a favicon has been set
    favicon = config.get('ckan.favicon', '/base/images/ckan.ico')
    config['ckan.favicon'] = favicon

    # Init SOLR settings and check if the schema is compatible
    # from ckan.lib.search import SolrSettings, check_solr_schema_version

    # lib.search is imported here as we need the config enabled and parsed
    search.SolrSettings.init(config.get('solr_url'), config.get('solr_user'),
                             config.get('solr_password'))
    search.check_solr_schema_version()

    routes_map = routing.make_map()
    config['routes.map'] = routes_map
    # The RoutesMiddleware needs its mapper updating if it exists
    if 'routes.middleware' in config:
        config['routes.middleware'].mapper = routes_map
    # routes.named_routes is a CKAN thing
    config['routes.named_routes'] = routing.named_routes
    config['pylons.app_globals'] = app_globals.app_globals
    # initialise the globals
    app_globals.app_globals._init()

    helpers.load_plugin_helpers()
    config['pylons.h'] = helpers.helper_functions

    # Templates and CSS loading from configuration
    valid_base_templates_folder_names = ['templates', 'templates-bs2']
    templates = config.get('ckan.base_templates_folder', 'templates')
    config['ckan.base_templates_folder'] = templates

    if templates not in valid_base_templates_folder_names:
        raise CkanConfigurationException(
            'You provided an invalid value for ckan.base_templates_folder. '
            'Possible values are: "templates" and "templates-bs2".')

    jinja2_templates_path = os.path.join(root, templates)
    log.info('Loading templates from %s' % jinja2_templates_path)
    template_paths = [jinja2_templates_path]

    extra_template_paths = config.get('extra_template_paths', '')
    if extra_template_paths:
        # must be first for them to override defaults
        template_paths = extra_template_paths.split(',') + template_paths
    config['computed_template_paths'] = template_paths

    # Set the default language for validation messages from formencode
    # to what is set as the default locale in the config
    default_lang = config.get('ckan.locale_default', 'en')
    formencode.api.set_stdtranslation(domain="FormEncode",
                                      languages=[default_lang])

    # Markdown ignores the logger config, so to get rid of excessive
    # markdown debug messages in the log, set it to the level of the
    # root logger.
    logging.getLogger("MARKDOWN").setLevel(logging.getLogger().level)

    # Create Jinja2 environment
    env = jinja_extensions.Environment(
        loader=jinja_extensions.CkanFileSystemLoader(template_paths),
        autoescape=True,
        extensions=[
            'jinja2.ext.do', 'jinja2.ext.with_',
            jinja_extensions.SnippetExtension, jinja_extensions.CkanExtend,
            jinja_extensions.CkanInternationalizationExtension,
            jinja_extensions.LinkForExtension,
            jinja_extensions.ResourceExtension,
            jinja_extensions.UrlForStaticExtension,
            jinja_extensions.UrlForExtension
        ],
        bytecode_cache=jinja2.FileSystemBytecodeCache())
    env.install_gettext_callables(_, ungettext, newstyle=True)
    # custom filters
    env.filters['empty_and_escape'] = jinja_extensions.empty_and_escape
    config['pylons.app_globals'].jinja_env = env

    # CONFIGURATION OPTIONS HERE (note: all config options will override
    # any Pylons config options)

    # Initialize SQLAlchemy
    engine = sqlalchemy.engine_from_config(config, client_encoding='utf8')
    model.init_model(engine)

    for plugin in p.PluginImplementations(p.IConfigurable):
        plugin.configure(config)

    # reset the template cache - we do this here so that when we load the
    # environment it is clean
    render.reset_template_info_cache()

    # clear other caches
    logic.clear_actions_cache()
    logic.clear_validators_cache()
    authz.clear_auth_functions_cache()

    # Here we create the site user if they are not already in the database
    try:
        logic.get_action('get_site_user')({'ignore_auth': True}, None)
    except (sqlalchemy.exc.ProgrammingError, sqlalchemy.exc.OperationalError):
        # (ProgrammingError for Postgres, OperationalError for SQLite)
        # The database is not initialised.  This is a bit dirty.  This occurs
        # when running tests.
        pass
    except sqlalchemy.exc.InternalError:
        # The database is not initialised.  Travis hits this
        pass

    # Close current session and open database connections to ensure a clean
    # clean environment even if an error occurs later on
    model.Session.remove()
    model.Session.bind.dispose()
예제 #27
0
    # Tuples are encoded as lists in JSON, and these are not real
    # easy to unpack or read in Javascript. So for ease of use, we
    # convert to dictionaries before returning the json results.
    # If further discrepancies are introduced please document them in templating.mkd
    arguments["results"] = [{
        "icon":
        icon,
        "path":
        path,
        "lines": [{
            "line_number": nb,
            "line": l
        } for nb, l in lines]
    } for icon, path, lines in arguments["results"]]
    json.dump(arguments, sys.stdout, indent=2)
    sys.exit(0)

# Print content-type
print 'Content-Type: text/html\n'

# Load template system
import jinja2
env = jinja2.Environment(loader=jinja2.FileSystemLoader("template"),
                         auto_reload=False,
                         bytecode_cache=jinja2.FileSystemBytecodeCache(
                             "jinja_dxr_cache", "%s.cache"))

# Get search template and dump it to stdout
env.get_template(template).stream(**arguments).dump(sys.stdout,
                                                    encoding="utf-8")
예제 #28
0
#newrelic.agent.initialize('/home/isucon/isubata/webapp/python/newrelic.ini')

static_folder = pathlib.Path(__file__).resolve().parent.parent / 'public'
icons_folder = static_folder / 'icons'

app = flask.Flask(__name__)
app.secret_key = 'tonymoris'
avatar_max_size = 1 * 1024 * 1024

SESSION_TYPE = 'redis'
SESSION_REDIS = Redis(os.environ.get('ISUBATA_DB_HOST', 'localhost'))
app.config.from_object(__name__)
Session(app)

app.jinja_options = app.jinja_options.copy()
app.jinja_options['bytecode_cache'] = jinja2.FileSystemBytecodeCache()

if not os.path.exists(str(icons_folder)):
    os.makedirs(str(icons_folder))

config = {
    'db_host': os.environ.get('ISUBATA_DB_HOST', 'localhost'),
    'db_port': int(os.environ.get('ISUBATA_DB_PORT', '3306')),
    'db_user': os.environ.get('ISUBATA_DB_USER', 'root'),
    'db_password': os.environ.get('ISUBATA_DB_PASSWORD', ''),
}

SCHEME = 'mysql://{user}:{passwd}@{host}/{db}'.format(
    user=os.environ.get('ISUBATA_DB_USER', 'root'),
    passwd=os.environ.get('ISUBATA_DB_PASSWORD', ''),
    host=os.environ.get('ISUBATA_DB_HOST', 'localhost'),
예제 #29
0
import shutil

import jinja2
import yaml

from docutils.core import publish_parts

PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_DIR = os.path.join(PROJECT_DIR, 'static')
OUTPUT_DIR = os.path.join(PROJECT_DIR, 'output')
OUTPUT_STATIC_DIR = os.path.join(OUTPUT_DIR, 'static')
POSTS_DIR = os.path.join(PROJECT_DIR, 'posts')
TEMPLATES_DIR = os.path.join(PROJECT_DIR, 'templates')
JINJA = jinja2.Environment(
    loader=jinja2.FileSystemLoader(TEMPLATES_DIR),
    bytecode_cache=jinja2.FileSystemBytecodeCache(),
    extensions=['jinja2.ext.autoescape'],
    autoescape=True,
)


def makedirs(filename):
    dirname = os.path.dirname(filename)
    try:
        os.makedirs(dirname)
    except os.error:
        pass


def write(filename, data):
    makedirs(filename)
예제 #30
0
파일: common_app.py 프로젝트: CZ-NIC/foris
def prepare_common_app(args, app_name, init_function, top_index, logger, load_plugins=True):
    """
    Prepare Foris application - i.e. apply CLI arguments, mount applications,
    install hooks and middleware etc...

    :param args: arguments received from ArgumentParser.parse_args().
    :param app_name: the name of the application
    :type app_name: str
    :param init_function: function to init the routes (app specific)
    "type init_function: callable
    :return: bottle.app() for Foris
    """

    # set app
    current_state.set_app(app_name)

    # internationalization
    i18n_defaults(bottle.SimpleTemplate, bottle.request)
    i18n_defaults(bottle.Jinja2Template, bottle.request)
    bottle.Jinja2Template.settings["extensions"] = ["foris.utils.translators.i18n"]
    bottle.Jinja2Template.settings["bytecode_cache"] = jinja2.FileSystemBytecodeCache(
        directory=current_state.assets_path
    )

    # setup default template defaults
    prepare_template_defaults()

    # init messaging template
    messages.set_template_defaults()

    app = init_default_app(top_index, args.static)

    # basic and bottle settings
    template_dir = os.path.join(BASE_DIR, "templates")
    bottle.TEMPLATE_PATH.append(template_dir)

    # mount apps
    app.mount("/main", init_function())

    if args.debug:
        if args.noauth:
            logger.warning("authentication disabled")
            app.config["no_auth"] = True

    # set custom app attributes for main app and all mounted apps
    init_common_app(app, None)
    for route in app.routes:
        if route.config.get("mountpoint"):
            mounted = route.config["mountpoint.target"]
            prefix = route.config["mountpoint.prefix"]
            init_common_app(mounted, prefix)

    if load_plugins:
        # load Foris plugins before applying Bottle plugins to app
        loader = ForisPluginLoader(app)
        loader.autoload_plugins()

    # i18n middleware
    app = I18NMiddleware(
        app,
        I18NPlugin(
            domain="messages",
            lang_code=DEFAULT_LANGUAGE,
            default=DEFAULT_LANGUAGE,
            locale_dir=os.path.join(BASE_DIR, "locale"),
        ),
    )

    # obtains required data from backend (this will happen everytime when a request arrives)
    app = BackendData(app)

    # reporting middleware for all mounted apps
    app = ReportingMiddleware(app, sensitive_params=("key", "pass", "*password*"))
    app.install_dump_route(bottle.app())

    # session handling
    app = SessionMiddleware(app, args.session_timeout)

    # print routes to console and exit
    if args.routes:
        routes = route_list_cmdline(bottle.app())
        print("\n".join(sorted(set(routes))))
        return app

    # print routes in debug mode
    if args.debug:
        routes = route_list_debug(bottle.app())
        logger.debug("Routes:\n%s", "\n".join(routes))

    # Make dynamic assets cleanup
    dynamic_assets.reset(app_name, args.assets)

    return app