Skip to content

Commit eb6bab1

Browse files
authored
Bug 1420225 - Read legacy files when scanning for Sources in transforms (#30)
1 parent 3fffe29 commit eb6bab1

File tree

6 files changed

+33
-49
lines changed

6 files changed

+33
-49
lines changed

fluent/migrate/context.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,12 @@ def maybe_add_localization(self, path):
139139
else:
140140
self.localization_resources[path] = collection
141141

142-
def add_transforms(self, path, reference, transforms):
143-
"""Define transforms for path using reference as template.
142+
def add_transforms(self, target, reference, transforms):
143+
"""Define transforms for target using reference as template.
144+
145+
`target` is a path of the destination FTL file relative to the
146+
localization directory. `reference` is a path to the template FTL
147+
file relative to the reference directory.
144148
145149
Each transform is an extended FTL node with `Transform` nodes as some
146150
values. Transforms are stored in their lazy AST form until
@@ -169,34 +173,38 @@ def get_sources(acc, cur):
169173
else:
170174
# The reference file will be used by the merge function as
171175
# a template for serializing the merge results.
172-
self.reference_resources[path] = ast
176+
self.reference_resources[target] = ast
173177

174178
for node in transforms:
175179
# Scan `node` for `Source` nodes and collect the information they
176180
# store into a set of dependencies.
177181
dependencies = fold(get_sources, node, set())
178182
# Set these sources as dependencies for the current transform.
179-
self.dependencies[(path, node.id.name)] = dependencies
183+
self.dependencies[(target, node.id.name)] = dependencies
184+
185+
# Read all legacy translation files defined in Source transforms.
186+
for path in set(path for path, _ in dependencies):
187+
self.maybe_add_localization(path)
180188

181-
path_transforms = self.transforms.setdefault(path, [])
189+
path_transforms = self.transforms.setdefault(target, [])
182190
path_transforms += transforms
183191

184-
if path not in self.localization_resources:
185-
fullpath = os.path.join(self.localization_dir, path)
192+
if target not in self.localization_resources:
193+
fullpath = os.path.join(self.localization_dir, target)
186194
try:
187195
ast = self.read_ftl_resource(fullpath)
188196
except IOError:
189197
logger = logging.getLogger('migrate')
190198
logger.info(
191199
'Localization file {} does not exist and '
192-
'it will be created'.format(path))
200+
'it will be created'.format(target))
193201
except UnicodeDecodeError:
194202
logger = logging.getLogger('migrate')
195203
logger.warn(
196204
'Localization file {} will be re-created and some '
197-
'translations might be lost'.format(path))
205+
'translations might be lost'.format(target))
198206
else:
199-
self.localization_resources[path] = ast
207+
self.localization_resources[target] = ast
200208

201209
def get_source(self, path, key):
202210
"""Get an entity value from a localized legacy source.

tests/migrate/test_context.py

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
import logging
66
import unittest
77

8+
try:
9+
import compare_locales
10+
except ImportError:
11+
compare_locales = None
12+
813
import fluent.syntax.ast as FTL
914

1015
from fluent.migrate.errors import NotSupportedError, UnreadableReferenceError
@@ -18,6 +23,7 @@ def here(*parts):
1823
return os.path.join(dirname, *parts)
1924

2025

26+
@unittest.skipUnless(compare_locales, 'compare-locales requried')
2127
class TestMergeContext(unittest.TestCase):
2228
def setUp(self):
2329
self.ctx = MergeContext(
@@ -26,12 +32,6 @@ def setUp(self):
2632
localization_dir=here('fixtures/pl')
2733
)
2834

29-
try:
30-
self.ctx.maybe_add_localization('aboutDownloads.dtd')
31-
self.ctx.maybe_add_localization('aboutDownloads.properties')
32-
except RuntimeError:
33-
self.skipTest('compare-locales required')
34-
3535
def test_hardcoded_node(self):
3636
self.ctx.add_transforms('aboutDownloads.ftl', 'aboutDownloads.ftl', [
3737
FTL.Message(
@@ -249,6 +249,7 @@ def test_missing_reference_file(self):
249249
self.ctx.add_transforms('some.ftl', 'missing.ftl', [])
250250

251251

252+
@unittest.skipUnless(compare_locales, 'compare-locales requried')
252253
class TestIncompleteLocalization(unittest.TestCase):
253254
def setUp(self):
254255
# Silence all logging.
@@ -260,11 +261,6 @@ def setUp(self):
260261
localization_dir=here('fixtures/pl')
261262
)
262263

263-
try:
264-
self.ctx.maybe_add_localization('browser.dtd')
265-
except RuntimeError:
266-
self.skipTest('compare-locales required')
267-
268264
self.ctx.add_transforms('toolbar.ftl', 'toolbar.ftl', [
269265
FTL.Message(
270266
id=FTL.Identifier('urlbar-textbox'),
@@ -299,6 +295,7 @@ def test_missing_localization_file(self):
299295
)
300296

301297

298+
@unittest.skipUnless(compare_locales, 'compare-locales requried')
302299
class TestExistingTarget(unittest.TestCase):
303300
maxDiff = None
304301

@@ -312,11 +309,6 @@ def setUp(self):
312309
localization_dir=here('fixtures/pl')
313310
)
314311

315-
try:
316-
self.ctx.maybe_add_localization('privacy.dtd')
317-
except RuntimeError:
318-
self.skipTest('compare-locales required')
319-
320312
def tearDown(self):
321313
# Resume logging.
322314
logging.disable(logging.NOTSET)

tests/migrate/test_context_real_examples.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
import os
55
import unittest
66

7+
try:
8+
import compare_locales
9+
except ImportError:
10+
compare_locales = None
11+
712
import fluent.syntax.ast as FTL
813

914
from fluent.migrate.util import ftl_resource_to_json, to_json
@@ -19,6 +24,7 @@ def here(*parts):
1924
return os.path.join(dirname, *parts)
2025

2126

27+
@unittest.skipUnless(compare_locales, 'compare-locales requried')
2228
class TestMergeAboutDownloads(unittest.TestCase):
2329
def setUp(self):
2430
self.ctx = MergeContext(
@@ -27,12 +33,6 @@ def setUp(self):
2733
localization_dir=here('fixtures/pl')
2834
)
2935

30-
try:
31-
self.ctx.maybe_add_localization('aboutDownloads.dtd')
32-
self.ctx.maybe_add_localization('aboutDownloads.properties')
33-
except RuntimeError:
34-
self.skipTest('compare-locales required')
35-
3636
self.ctx.add_transforms('aboutDownloads.ftl', 'aboutDownloads.ftl', [
3737
FTL.Message(
3838
id=FTL.Identifier('title'),
@@ -279,6 +279,7 @@ def test_merge_context_some_messages(self):
279279
)
280280

281281

282+
@unittest.skipUnless(compare_locales, 'compare-locales requried')
282283
class TestMergeAboutDialog(unittest.TestCase):
283284
def setUp(self):
284285
self.ctx = MergeContext(
@@ -287,11 +288,6 @@ def setUp(self):
287288
localization_dir=here('fixtures/pl')
288289
)
289290

290-
try:
291-
self.ctx.maybe_add_localization('aboutDialog.dtd')
292-
except RuntimeError:
293-
self.skipTest('compare-locales required')
294-
295291
self.ctx.add_transforms('aboutDialog.ftl', 'aboutDialog.ftl', [
296292
FTL.Message(
297293
id=FTL.Identifier('update-failed'),

tools/migrate/examples/about_dialog.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
def migrate(ctx):
1010
"""Migrate about:dialog, part {index}"""
1111

12-
ctx.maybe_add_localization('browser/chrome/browser/aboutDialog.dtd')
13-
1412
ctx.add_transforms('browser/about_dialog.ftl', 'about_dialog.ftl', [
1513
FTL.Message(
1614
id=FTL.Identifier('update-failed'),

tools/migrate/examples/about_downloads.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,6 @@
77
def migrate(ctx):
88
"""Migrate about:download in Firefox for Android, part {index}"""
99

10-
ctx.maybe_add_localization(
11-
'mobile/android/chrome/aboutDownloads.dtd')
12-
ctx.maybe_add_localization(
13-
'mobile/android/chrome/aboutDownloads.properties')
14-
1510
ctx.add_transforms('mobile/about_downloads.ftl', 'about_downloads.ftl', [
1611
FTL.Message(
1712
id=FTL.Identifier('title'),

tools/migrate/examples/bug_1291693.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,6 @@
77
def migrate(ctx):
88
"""Bug 1291693 - Migrate the menubar to FTL, part {index}"""
99

10-
ctx.maybe_add_localization('browser/chrome/browser/browser.dtd')
11-
ctx.maybe_add_localization('browser/chrome/browser/browser.properties')
12-
ctx.maybe_add_localization('browser/branding/official/brand.dtd')
13-
ctx.maybe_add_localization('browser/branding/official/brand.properties')
14-
1510
ctx.add_transforms('browser/menubar.ftl', 'menubar.ftl', [
1611
FTL.Message(
1712
id=FTL.Identifier('file-menu'),

0 commit comments

Comments
 (0)