summaryrefslogtreecommitdiffstats
path: root/third_party/python/fluent.migrate/fluent/migrate/transforms.py
blob: f45ad1531cecce227ef1d0e3913147c8e1b713bc (plain)
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
"""Migration Transforms.

Transforms are AST nodes which describe how legacy translations should be
migrated.  They are created inert and only return the migrated AST nodes when
they are evaluated by a MigrationContext.

All Transforms evaluate to Fluent Patterns. This makes them suitable for
defining migrations of values of message, attributes and variants.  The special
CONCAT Transform is capable of joining multiple Patterns returned by evaluating
other Transforms into a single Pattern.  It can also concatenate Pattern
elements: TextElements and Placeables.

The COPY, REPLACE and PLURALS Transforms inherit from Source which is a special
AST Node defining the location (the file path and the id) of the legacy
translation.  During the migration, the current MigrationContext scans the
migration spec for Source nodes and extracts the information about all legacy
translations being migrated. For instance,

    COPY('file.dtd', 'hello')

is equivalent to:

    FTL.Pattern([
        Source('file.dtd', 'hello')
    ])

Sometimes it's useful to work with text rather than (path, key) source
definitions. This is the case when the migrated translation requires some
hardcoded text, e.g. <a> and </a> when multiple translations become a single
one with a DOM overlay. In such cases it's best to use FTL.TextElements:

    FTL.Message(
        id=FTL.Identifier('update-failed'),
        value=CONCAT(
            COPY('aboutDialog.dtd', 'update.failed.start'),
            FTL.TextElement('<a>'),
            COPY('aboutDialog.dtd', 'update.failed.linkText'),
            FTL.TextElement('</a>'),
            COPY('aboutDialog.dtd', 'update.failed.end'),
        )
    )

The REPLACE_IN_TEXT Transform also takes TextElements as input, making it
possible to pass it as the foreach function of the PLURALS Transform. In the
example below, each slice of the plural string is converted into a
TextElement by PLURALS and then run through the REPLACE_IN_TEXT transform.

    FTL.Message(
        FTL.Identifier('delete-all'),
        value=PLURALS(
            'aboutDownloads.dtd',
            'deleteAll',
            VARIABLE_REFERENCE('num'),
            lambda text: REPLACE_IN_TEXT(
                text,
                {
                    '#1': VARIABLE_REFERENCE('num')
                }
            )
        )
    )
"""

import re

from fluent.syntax import ast as FTL
from fluent.syntax.visitor import Transformer
from .errors import NotSupportedError


def chain_elements(elements):
    """Flatten a list of FTL nodes into an iterator over PatternElements."""
    for element in elements:
        if isinstance(element, FTL.Pattern):
            # PY3 yield from element.elements
            yield from element.elements
        elif isinstance(element, FTL.PatternElement):
            yield element
        elif isinstance(element, FTL.Expression):
            yield FTL.Placeable(element)
        else:
            raise RuntimeError("Expected Pattern, PatternElement or Expression")


re_leading_ws = re.compile(
    r"\A(?:(?P<whitespace> +)(?P<text>.*?)|(?P<block_text>\n.*?))\Z",
    re.S,
)
re_trailing_ws = re.compile(
    r"\A(?:(?P<text>.*?)(?P<whitespace> +)|(?P<block_text>.*\n))\Z", re.S
)


def extract_whitespace(regex, element):
    """Extract leading or trailing whitespace from a TextElement.

    Return a tuple of (Placeable, TextElement) in which the Placeable
    encodes the extracted whitespace as a StringLiteral and the
    TextElement has the same amount of whitespace removed. The
    Placeable with the extracted whitespace is always returned first.
    If the element starts or ends with a newline, add an empty
    StringLiteral.
    """
    match = re.search(regex, element.value)
    if match:
        # If white-space is None, we're a newline. Add an
        # empty { "" }
        whitespace = match.group("whitespace") or ""
        placeable = FTL.Placeable(FTL.StringLiteral(whitespace))
        if whitespace == element.value:
            return placeable, None
        else:
            # Either text or block_text matched the rest.
            text = match.group("text") or match.group("block_text")
            return placeable, FTL.TextElement(text)
    else:
        return None, element


class Transform(FTL.BaseNode):
    def __call__(self, ctx):
        raise NotImplementedError

    @staticmethod
    def pattern_of(*elements):
        normalized = []

        # Normalize text content: convert text content to TextElements, join
        # adjacent text and prune empty. Text content is either existing
        # TextElements or whitespace-only StringLiterals. This may result in
        # leading and trailing whitespace being put back into TextElements if
        # the new Pattern is built from existing Patterns (CONCAT(COPY...)).
        # The leading and trailing whitespace of the new Pattern will be
        # extracted later into new StringLiterals.
        for element in chain_elements(elements):
            if isinstance(element, FTL.TextElement):
                text_content = element.value
            elif (
                isinstance(element, FTL.Placeable)
                and isinstance(element.expression, FTL.StringLiteral)
                and re.match(r"^ *$", element.expression.value)
            ):
                text_content = element.expression.value
            else:
                # The element does not contain text content which should be
                # normalized. It may be a number, a reference, or
                # a StringLiteral which should be preserved in the Pattern.
                normalized.append(element)
                continue

            previous = normalized[-1] if len(normalized) else None
            if isinstance(previous, FTL.TextElement):
                # Join adjacent TextElements.
                previous.value += text_content
            elif len(text_content) > 0:
                # Normalize non-empty text to a TextElement.
                normalized.append(FTL.TextElement(text_content))
            else:
                # Prune empty text.
                pass

        # Store empty values explicitly as {""}.
        if len(normalized) == 0:
            empty = FTL.Placeable(FTL.StringLiteral(""))
            return FTL.Pattern([empty])

        # Extract explicit leading whitespace into a StringLiteral.
        if isinstance(normalized[0], FTL.TextElement):
            ws, text = extract_whitespace(re_leading_ws, normalized[0])
            normalized[:1] = [ws, text]

        # Extract explicit trailing whitespace into a StringLiteral.
        if isinstance(normalized[-1], FTL.TextElement):
            ws, text = extract_whitespace(re_trailing_ws, normalized[-1])
            normalized[-1:] = [text, ws]

        return FTL.Pattern([element for element in normalized if element is not None])


class Source(Transform):
    """Base class for Transforms that get translations from source files.

    The contract is that the first argument is the source path, and the
    second is a key representing legacy string IDs, or Fluent id.attr.
    """

    def __init__(self, path, key):
        self.path = path
        self.key = key


class FluentSource(Source):
    """Declare a Fluent source translation to be copied over.

    When evaluated, it clones the Pattern of the parsed source.
    """

    def __init__(self, path, key):
        if not path.endswith(".ftl"):
            raise NotSupportedError(
                "Please use COPY to migrate from legacy files " "({})".format(path)
            )
        if key[0] == "-" and "." in key:
            raise NotSupportedError(
                "Cannot migrate from Term Attributes, as they are"
                "locale-dependent ({})".format(path)
            )
        super().__init__(path, key)

    def __call__(self, ctx):
        pattern = ctx.get_fluent_source_pattern(self.path, self.key)
        return pattern.clone()


class COPY_PATTERN(FluentSource):
    """Create a Pattern with the translation value from the given source.

    The given key can be a Message ID, Message ID.attribute_name, or
    Term ID. Accessing Term attributes is not supported, as they're internal
    to the localization.
    """

    pass


class TransformPattern(FluentSource, Transformer):
    """Base class for modifying a Fluent pattern as part of a migration.

    Implement visit_* methods of the Transformer pattern to do the
    actual modifications.
    """

    def __call__(self, ctx):
        pattern = super().__call__(ctx)
        return self.visit(pattern)

    def visit_Pattern(self, node):
        # Make sure we're creating valid Patterns after restructuring
        # transforms.
        node = self.generic_visit(node)
        pattern = Transform.pattern_of(*node.elements)
        return pattern

    def visit_Placeable(self, node):
        # Ensure we have a Placeable with an expression still.
        # Transforms could have replaced the expression with
        # a Pattern or PatternElement, in which case we
        # just pass that through.
        # Patterns then get flattened by visit_Pattern.
        node = self.generic_visit(node)
        if isinstance(node.expression, (FTL.Pattern, FTL.PatternElement)):
            return node.expression
        return node


class LegacySource(Source):
    """Declare the source translation to be migrated with other transforms.

    When evaluated, `Source` returns a TextElement with the content from the
    source translation. Escaped characters are unescaped by the
    compare-locales parser according to the file format:

      - in properties files: \\uXXXX,
      - in DTD files: known named, decimal, and hexadecimal HTML entities.

    Consult the following files for the list of known named HTML entities:

    https://github.com/python/cpython/blob/2.7/Lib/htmlentitydefs.py
    https://github.com/python/cpython/blob/3.6/Lib/html/entities.py

    By default, leading and trailing whitespace on each line as well as
    leading and trailing empty lines will be stripped from the source
    translation's content. Set `trim=False` to disable this behavior.
    """

    def __init__(self, path, key, trim=None):
        if path.endswith(".ftl"):
            raise NotSupportedError(
                "Please use COPY_PATTERN to migrate from Fluent files "
                "({})".format(path)
            )

        super().__init__(path, key)
        self.trim = trim

    def get_text(self, ctx):
        return ctx.get_legacy_source(self.path, self.key)

    @staticmethod
    def trim_text(text):
        # strip leading white-space from each line
        text = re.sub("^[ \t]+", "", text, flags=re.M)
        # strip trailing white-space from each line
        text = re.sub("[ \t]+$", "", text, flags=re.M)
        # strip leading and trailing empty lines
        text = text.strip("\r\n")
        return text

    def __call__(self, ctx):
        text = self.get_text(ctx)
        if self.trim is not False:
            text = self.trim_text(text)
        return FTL.TextElement(text)


class COPY(LegacySource):
    """Create a Pattern with the translation value from the given source."""

    def __call__(self, ctx):
        element = super().__call__(ctx)
        return Transform.pattern_of(element)


PRINTF = re.compile(
    r"%(?P<good>%|"
    r"(?:(?P<number>[1-9][0-9]*)\$)?"
    r"(?P<width>\*|[0-9]+)?"
    r"(?P<prec>\.(?:\*|[0-9]+)?)?"
    r"(?P<spec>[duxXosScpfg]))"
)


def number():
    i = 1
    while True:
        yield i
        i += 1


def normalize_printf(text):
    """Normalize printf arguments so that they're all numbered.
    Gecko forbids mixing unnumbered and numbered ones, so
    we just need to convert unnumbered to numbered ones.
    Also remove ones that have zero width, as they're intended
    to be removed from the output by the localizer.
    """
    next_number = number()

    def normalized(match):
        if match.group("good") == "%":
            return "%"
        hidden = match.group("width") == "0"
        if match.group("number"):
            return "" if hidden else match.group()
        num = next(next_number)
        return "" if hidden else "%{}${}".format(num, match.group("spec"))

    return PRINTF.sub(normalized, text)


class REPLACE_IN_TEXT(Transform):
    """Create a Pattern from a TextElement and replace legacy placeables.

    The original placeables are defined as keys on the `replacements` dict.
    For each key the value must be defined as a FTL Pattern, Placeable,
    TextElement or Expression to be interpolated.
    """

    def __init__(self, element, replacements, normalize_printf=False):
        self.element = element
        self.replacements = replacements
        self.normalize_printf = normalize_printf

    def __call__(self, ctx):
        # For each specified replacement, find all indices of the original
        # placeable in the source translation. If missing, the list of indices
        # will be empty.
        value = self.element.value
        if self.normalize_printf:
            value = normalize_printf(value)
        key_indices = {
            key: [m.start() for m in re.finditer(re.escape(key), value)]
            for key in self.replacements.keys()
        }

        # Build a dict of indices to replacement keys.
        keys_indexed = {}
        for key, indices in key_indices.items():
            for index in indices:
                keys_indexed[index] = key

        # Order the replacements by the position of the original placeable in
        # the translation.
        replacements = (
            (key, ctx.evaluate(self.replacements[key]))
            for index, key in sorted(keys_indexed.items(), key=lambda x: x[0])
        )

        # A list of PatternElements built from the legacy translation and the
        # FTL replacements. It may contain empty or adjacent TextElements.
        elements = []
        tail = value

        # Convert original placeables and text into FTL Nodes. For each
        # original placeable the translation will be partitioned around it and
        # the text before it will be converted into an `FTL.TextElement` and
        # the placeable will be replaced with its replacement.
        for key, node in replacements:
            before, key, tail = tail.partition(key)
            elements.append(FTL.TextElement(before))
            elements.append(node)

        # Don't forget about the tail after the loop ends.
        elements.append(FTL.TextElement(tail))
        return Transform.pattern_of(*elements)


class REPLACE(LegacySource):
    """Create a Pattern with interpolations from given source.

    Interpolations in the translation value from the given source will be
    replaced with FTL placeables using the `REPLACE_IN_TEXT` transform.
    """

    def __init__(self, path, key, replacements, **kwargs):
        # We default normalize_printf to False except for .properties files.
        # We still allow the caller to override the default value.
        normalize_printf = False
        if "normalize_printf" in kwargs:
            normalize_printf = kwargs["normalize_printf"]
            del kwargs["normalize_printf"]
        elif path.endswith(".properties"):
            normalize_printf = True

        super().__init__(path, key, **kwargs)
        self.replacements = replacements
        self.normalize_printf = normalize_printf

    def __call__(self, ctx):
        element = super().__call__(ctx)
        return REPLACE_IN_TEXT(
            element, self.replacements, normalize_printf=self.normalize_printf
        )(ctx)


class PLURALS(LegacySource):
    """Create a Pattern with plurals from given source.

    Build an `FTL.SelectExpression` with the supplied `selector` and variants
    extracted from the source. The original translation should be a
    semicolon-separated list of plural forms. Each form will be converted
    into a TextElement and run through the `foreach` function, which should
    return an `FTL.Node` or a `Transform`. By default, the `foreach` function
    creates a valid Pattern from the TextElement passed into it.
    """

    DEFAULT_ORDER = ("zero", "one", "two", "few", "many", "other")

    def __init__(self, path, key, selector, foreach=Transform.pattern_of, **kwargs):
        super().__init__(path, key, **kwargs)
        self.selector = selector
        self.foreach = foreach

    def __call__(self, ctx):
        element = super().__call__(ctx)
        selector = ctx.evaluate(self.selector)
        keys = ctx.plural_categories
        forms = [FTL.TextElement(part.strip()) for part in element.value.split(";")]

        # The default CLDR form should be the last we have in DEFAULT_ORDER,
        # usually `other`, but in some cases `many`. If we don't have a variant
        # for that, we'll append one, using the, in CLDR order, last existing
        # variant in the legacy translation. That may or may not be the last
        # variant.
        default_key = [key for key in reversed(self.DEFAULT_ORDER) if key in keys][0]

        # Match keys to legacy forms in the order they are defined in Gecko's
        # PluralForm.jsm. Filter out empty forms.
        pairs = [(key, var) for key, var in zip(keys, forms) if var.value]

        # A special case for legacy translations which don't define any
        # plural forms.
        if len(pairs) == 0:
            return Transform.pattern_of()

        # A special case for languages with one plural category or one legacy
        # variant. We don't need to insert a SelectExpression for them.
        if len(pairs) == 1:
            _, only_form = pairs[0]
            only_variant = ctx.evaluate(self.foreach(only_form))
            return Transform.pattern_of(only_variant)

        # Make sure the default key is defined. If it's missing, use the last
        # form (in CLDR order) found in the legacy translation.
        pairs.sort(key=lambda pair: self.DEFAULT_ORDER.index(pair[0]))
        last_key, last_form = pairs[-1]
        if last_key != default_key:
            pairs.append((default_key, last_form))

        def createVariant(key, form):
            # Run the legacy plural form through `foreach` which returns an
            # `FTL.Node` describing the transformation required for each
            # variant. Then evaluate it to a migrated FTL node.
            value = ctx.evaluate(self.foreach(form))
            return FTL.Variant(
                key=FTL.Identifier(key), value=value, default=key == default_key
            )

        select = FTL.SelectExpression(
            selector=selector,
            variants=[createVariant(key, form) for key, form in pairs],
        )

        return Transform.pattern_of(select)


class CONCAT(Transform):
    """Create a new Pattern from Patterns, PatternElements and Expressions.

    When called with at least two elements, `CONCAT` disables the trimming
    behavior of the elements which are subclasses of `LegacySource` by
    setting `trim=False`, unless `trim` has already been set explicitly. The
    following two `CONCAT` calls are equivalent:

       CONCAT(
           FTL.TextElement("Hello"),
           COPY("file.properties", "hello")
       )

       CONCAT(
           FTL.TextElement("Hello"),
           COPY("file.properties", "hello", trim=False)
       )

    Set `trim=True` explicitly to force trimming:

       CONCAT(
           FTL.TextElement("Hello "),
           COPY("file.properties", "hello", trim=True)
       )

    When called with a single element and when the element is a subclass of
    `LegacySource`, the trimming behavior is not changed. The following two
    transforms are equivalent:

       CONCAT(COPY("file.properties", "hello"))

       COPY("file.properties", "hello")
    """

    def __init__(self, *elements, **kwargs):
        # We want to support both passing elements as *elements in the
        # migration specs and as elements=[]. The latter is used by
        # FTL.BaseNode.traverse when it recreates the traversed node using its
        # attributes as kwargs.
        self.elements = list(kwargs.get("elements", elements))

        # We want to make CONCAT(COPY()) equivalent to COPY() so that it's
        # always safe (no-op) to wrap transforms in a CONCAT. This is used by
        # the implementation of transforms_from.
        if len(self.elements) > 1:
            for elem in self.elements:
                # Only change trim if it hasn't been set explicitly.
                if isinstance(elem, LegacySource) and elem.trim is None:
                    elem.trim = False

    def __call__(self, ctx):
        return Transform.pattern_of(*self.elements)