Skip to content

Commit fbe04c9

Browse files
committed
bug 1436014, part 1 - refactor in_changeset
Move in_changeset to a method, and use functools.partial to get a per-path and changeset version of it. Adjust the doc string to be more explicit about dependencies vs transforms.
1 parent ea6409c commit fbe04c9

File tree

1 file changed

+43
-41
lines changed

1 file changed

+43
-41
lines changed

fluent/migrate/context.py

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import os
55
import codecs
6+
from functools import partial
67
import logging
78

89
try:
@@ -290,11 +291,8 @@ def merge_changeset(self, changeset=None, known_translations=None):
290291
all legacy translations will be allowed to be migrated in a single
291292
changeset.
292293
293-
The inner `in_changeset` function is used to determine if a message
294-
should be migrated for the given changeset. It compares the legacy
295-
dependencies of the transform defined for the message with legacy
296-
translations available in the changeset. If all dependencies are
297-
present, the message will be migrated.
294+
We use the `in_changeset` method to determine if a message should be
295+
migrated for the given changeset.
298296
299297
Given `changeset`, return a dict whose keys are resource paths and
300298
values are `FTL.Resource` instances. The values will also be used to
@@ -316,42 +314,7 @@ def merge_changeset(self, changeset=None, known_translations=None):
316314
for path, reference in self.reference_resources.iteritems():
317315
current = self.localization_resources[path]
318316
transforms = self.transforms.get(path, [])
319-
320-
def in_changeset(ident):
321-
"""Check if a message should be migrated.
322-
323-
A message will be migrated only if all of its dependencies
324-
are present in the currently processed changeset.
325-
326-
If a transform defined for this message points to a missing
327-
legacy translation, this message will not be merged. The
328-
missing legacy dependency won't be present in the changeset.
329-
330-
This also means that partially translated messages (e.g.
331-
constructed from two legacy strings out of which only one is
332-
avaiable) will never be migrated.
333-
"""
334-
message_deps = self.dependencies.get((path, ident), None)
335-
336-
# Don't merge if we don't have a transform for this message.
337-
if message_deps is None:
338-
return False
339-
340-
# As a special case, if a transform exists but has no
341-
# dependecies, it's a hardcoded `FTL.Node` which doesn't
342-
# migrate any existing translation but rather creates a new
343-
# one. Merge it.
344-
if len(message_deps) == 0:
345-
return True
346-
347-
# Make sure all the dependencies are present in the current
348-
# changeset. Partial migrations are not currently supported.
349-
# See https://bugzilla.mozilla.org/show_bug.cgi?id=1321271
350-
# We only return True if our current changeset touches
351-
# the transform, and we have all of the dependencies.
352-
active_deps = message_deps & changeset
353-
available_deps = message_deps & known_translations
354-
return active_deps and message_deps == available_deps
317+
in_changeset = partial(self.in_changeset, changeset, known_translations, path)
355318

356319
# Merge legacy translations with the existing ones using the
357320
# reference as a template.
@@ -376,6 +339,45 @@ def in_changeset(ident):
376339
# The result for this path is a complete `FTL.Resource`.
377340
yield path, snapshot
378341

342+
def in_changeset(self, changeset, known_translations, path, ident):
343+
"""Check if a message should be migrated in this changeset.
344+
345+
The message is identified by path and ident.
346+
347+
348+
A message will be migrated only if all of its dependencies
349+
are present in the currently processed changeset.
350+
351+
If a transform defined for this message points to a missing
352+
legacy translation, this message will not be merged. The
353+
missing legacy dependency won't be present in the changeset.
354+
355+
This also means that partially translated messages (e.g.
356+
constructed from two legacy strings out of which only one is
357+
avaiable) will never be migrated.
358+
"""
359+
message_deps = self.dependencies.get((path, ident), None)
360+
361+
# Don't merge if we don't have a transform for this message.
362+
if message_deps is None:
363+
return False
364+
365+
# As a special case, if a transform exists but has no
366+
# dependecies, it's a hardcoded `FTL.Node` which doesn't
367+
# migrate any existing translation but rather creates a new
368+
# one. Merge it.
369+
if len(message_deps) == 0:
370+
return True
371+
372+
# Make sure all the dependencies are present in the current
373+
# changeset. Partial migrations are not currently supported.
374+
# See https://bugzilla.mozilla.org/show_bug.cgi?id=1321271
375+
# We only return True if our current changeset touches
376+
# the transform, and we have all of the dependencies.
377+
active_deps = message_deps & changeset
378+
available_deps = message_deps & known_translations
379+
return active_deps and message_deps == available_deps
380+
379381
def serialize_changeset(self, changeset, known_translations=None):
380382
"""Return a dict of serialized FTLs for the changeset.
381383

0 commit comments

Comments
 (0)