1# coding=utf8
2from __future__ import unicode_literals
3from __future__ import absolute_import
4
5import fluent.syntax.ast as FTL
6
7from .errors import SkipTransform
8from .util import get_message, get_transform
9
10
11def merge_resource(ctx, reference, current, transforms, in_changeset):
12    """Transform legacy translations into FTL.
13
14    Use the `reference` FTL AST as a template.  For each en-US string in the
15    reference, first check for an existing translation in the current FTL
16    `localization` and use it if it's present; then if the string has
17    a transform defined in the migration specification and if it's in the
18    currently processed changeset, evaluate the transform.
19    """
20
21    def merge_body(body):
22        return [
23            entry
24            for entry in map(merge_entry, body)
25            if entry is not None
26        ]
27
28    def merge_entry(entry):
29        # All standalone comments will be merged.
30        if isinstance(entry, FTL.BaseComment):
31            return entry
32
33        # Ignore Junk
34        if isinstance(entry, FTL.Junk):
35            return None
36
37        ident = entry.id.name
38
39        # If the message is present in the existing localization, we add it to
40        # the resulting resource.  This ensures consecutive merges don't remove
41        # translations but rather create supersets of them.
42        existing = get_message(current.body, ident)
43        if existing is not None:
44            return existing
45
46        transform = get_transform(transforms, ident)
47
48        # Make sure this message is supposed to be migrated as part of the
49        # current changeset.
50        if transform is not None and in_changeset(ident):
51            if transform.comment is None:
52                transform.comment = entry.comment
53            try:
54                return ctx.evaluate(transform)
55            except SkipTransform:
56                return None
57
58    body = merge_body(reference.body)
59    return FTL.Resource(body)
60