Beispiel #1
0
def test_number_simplifying(benchmark):
    resources = [FtlResource("""
my-message = { NUMBER($count) ->
      [0]     None
      [1]     One
     *[other] NUMBER(NUMBER($count))
}
""")]
    benchmark(lambda: compile_messages('en', resources))
Beispiel #2
0
def test_term_inlining(benchmark):
    resources = [FtlResource("""
-my-brand = { $style ->
    *[lower]    foobar
     [upper]    FOOBAR
 }
my-message = This is a message from { -my-brand(style: "upper") }

""")]
    benchmark(lambda: compile_messages('en', resources))
def compile_messages_to_python(source,
                               locale,
                               use_isolating=False,
                               functions=None,
                               escapers=None,
                               filename=None):
    # We use FluentBundle partially here, but then switch to
    # messages_to_module instead of compile_messages so that we can get the AST
    # back instead of a compiled function.
    resource = FtlResource(dedent_ftl(source), filename=filename)
    output = compile_messages(
        locale,
        [resource],
        use_isolating=use_isolating,
        functions=functions,
        escapers=escapers,
    )
    return decompile_ast_list([output.module_ast]), output.errors
Beispiel #4
0
    def get_compiled_unit_for_locale(self, locale):
        try:
            return self._compiled_unit_for_locale[locale]
        except KeyError:
            pass

        # Fill out _compiled_unit_for_locale
        # necessary, but do this synchronized for all threads.
        with self._lock:
            # Double checked locking pattern.
            try:
                return self._compiled_unit_for_locale[locale]
            except KeyError:
                pass

            # Do the compilation:
            resources = []
            for path in self._paths:
                try:
                    resource = self._finder.load(locale, path, reloader=self._reloader)
                except FileNotFoundError:
                    pass
                    if locale == self._get_default_locale():
                        # Can't find any FTL with the specified filename, we
                        # want to bail early and alert developer.
                        raise
                    # Allow missing files otherwise
                else:
                    resources.append(resource)

            unit = compile_messages(
                locale,
                resources,
                use_isolating=self._use_isolating,
                escapers=[html_escaper]
            )
            errors = unit.errors
            for msg_id, error in errors:
                self._log_error(locale, msg_id, {}, error)
            self._compiled_unit_for_locale[locale] = unit
            return unit
Beispiel #5
0
def test_simple_message(benchmark):
    resources = [FtlResource('single-string-literal = Hello I am a single string literal')]
    benchmark(lambda: compile_messages('en', resources))