File: merge.py

package info (click to toggle)
mozjs128 128.14.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,132,528 kB
  • sloc: javascript: 2,181,430; cpp: 1,371,420; python: 776,651; ansic: 641,398; xml: 117,736; sh: 17,537; asm: 13,468; makefile: 11,191; yacc: 4,504; perl: 2,221; lex: 1,414; ruby: 1,032; exp: 756; java: 185; sql: 66; sed: 18
file content (51 lines) | stat: -rw-r--r-- 1,808 bytes parent folder | download | duplicates (17)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import fluent.syntax.ast as FTL

from .errors import SkipTransform
from .util import get_message, get_transform


def merge_resource(ctx, reference, current, transforms, in_changeset):
    """Transform legacy translations into FTL.

    Use the `reference` FTL AST as a template.  For each en-US string in the
    reference, first check for an existing translation in the current FTL
    `localization` and use it if it's present; then if the string has
    a transform defined in the migration specification and if it's in the
    currently processed changeset, evaluate the transform.
    """

    def merge_body(body):
        return [entry for entry in map(merge_entry, body) if entry is not None]

    def merge_entry(entry):
        # All standalone comments will be merged.
        if isinstance(entry, FTL.BaseComment):
            return entry

        # Ignore Junk
        if isinstance(entry, FTL.Junk):
            return None

        ident = entry.id.name

        # If the message is present in the existing localization, we add it to
        # the resulting resource.  This ensures consecutive merges don't remove
        # translations but rather create supersets of them.
        existing = get_message(current.body, ident)
        if existing is not None:
            return existing

        transform = get_transform(transforms, ident)

        # Make sure this message is supposed to be migrated as part of the
        # current changeset.
        if transform is not None and in_changeset(ident):
            if transform.comment is None:
                transform.comment = entry.comment
            try:
                return ctx.evaluate(transform)
            except SkipTransform:
                return None

    body = merge_body(reference.body)
    return FTL.Resource(body)