File: merge.py

package info (click to toggle)
thunderbird 1%3A127.0~b5-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 4,421,808 kB
  • sloc: cpp: 7,379,480; javascript: 5,568,142; ansic: 3,852,317; python: 1,221,238; xml: 620,984; asm: 456,308; java: 179,035; sh: 118,742; makefile: 21,918; perl: 14,823; objc: 12,399; yacc: 4,583; pascal: 2,973; lex: 1,720; ruby: 1,190; exp: 762; sql: 666; awk: 580; php: 436; lisp: 430; sed: 70; csh: 10
file content (51 lines) | stat: -rw-r--r-- 1,808 bytes parent folder | download | duplicates (25)
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)