Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions fluent/migrate/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# coding=utf8

from .context import MergeContext # noqa: F401
from .transforms import ( # noqa: F401
CONCAT, EXTERNAL, LITERAL, LITERAL_FROM, PLURALS, PLURALS_FROM, REPLACE,
REPLACE_FROM, SOURCE
)
from .changesets import convert_blame_to_changesets # noqa: F401
58 changes: 58 additions & 0 deletions fluent/migrate/changesets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# coding=utf8

import time


def by_first_commit(item):
"""Order two changesets by their first commit date."""
return item['first_commit']


def convert_blame_to_changesets(blame_json):
"""Convert a blame dict into a list of changesets.

The blame information in `blame_json` should be a dict of the following
structure:

{
'authors': [
'A.N. Author <author@example.com>',
],
'blame': {
'path/one': {
'key1': [0, 1346095921.0],
},
}
}

It will be transformed into a list of changesets which can be fed into
`MergeContext.serialize_changeset`:

[
{
'author': 'A.N. Author <author@example.com>',
'first_commit': 1346095921.0,
'changes': {
('path/one', 'key1'),
}
},
]

"""
now = time.time()
changesets = [
{
'author': author,
'first_commit': now,
'changes': set()
} for author in blame_json['authors']
]

for path, keys_info in blame_json['blame'].items():
for key, (author_index, timestamp) in keys_info.items():
changeset = changesets[author_index]
changeset['changes'].add((path, key))
if timestamp < changeset['first_commit']:
changeset['first_commit'] = timestamp

return sorted(changesets, key=by_first_commit)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless we want to call this in more places, I'd just inline this as a lambda function.

If you'd like to keep the method, please adjust that the doc string to denote it as a thing used by sort, not sorting itself?

55 changes: 55 additions & 0 deletions fluent/migrate/cldr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# coding=utf8

import pkgutil
import json


def in_canonical_order(item):
return canonical_order.index(item)


cldr_plurals = json.loads(
pkgutil.get_data('fluent.migrate', 'cldr_data/plurals.json').decode('utf-8')
)

rules = cldr_plurals['supplemental']['plurals-type-cardinal']
canonical_order = ('zero', 'one', 'two', 'few', 'many', 'other')

categories = {}
for lang, rules in rules.items():
categories[lang] = tuple(sorted(map(
lambda key: key.replace('pluralRule-count-', ''),
rules.keys()
), key=in_canonical_order))


def get_plural_categories(lang):
"""Return a tuple of CLDR plural categories for `lang`.

If an exact match for `lang` is not available, recursively fall back to
a language code with the last subtag stripped. That is, if `ja-JP-mac` is
not defined in CLDR, the code will try `ja-JP` and then `ja`.

If no matches are found, a `RuntimeError` is raised.

>>> get_plural_categories('sl')
('one', 'two', 'few', 'other')
>>> get_plural_categories('ga-IE')
('one', 'few', 'two', 'few', 'other')
>>> get_plural_categories('ja-JP-mac')
('other')

"""

langs_categories = categories.get(lang, None)

if langs_categories is None:
# Remove the trailing subtag.
fallback_lang, _, _ = lang.rpartition('-')

if fallback_lang == '':
raise RuntimeError('Unknown language: {}'.format(lang))

return get_plural_categories(fallback_lang)

return langs_categories
857 changes: 857 additions & 0 deletions fluent/migrate/cldr_data/plurals.json

Large diffs are not rendered by default.

53 changes: 53 additions & 0 deletions fluent/migrate/cldr_data/unicode-license.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
UNICODE, INC. LICENSE AGREEMENT - DATA FILES AND SOFTWARE

Unicode Data Files include all data files under the directories
http://www.unicode.org/Public/, http://www.unicode.org/reports/,
http://www.unicode.org/cldr/data/, http://source.icu-project.org/repos/icu/,
and http://www.unicode.org/utility/trac/browser/.

Unicode Data Files do not include PDF online code charts under the
directory http://www.unicode.org/Public/.

Software includes any source code published in the Unicode Standard
or under the directories
http://www.unicode.org/Public/, http://www.unicode.org/reports/,
http://www.unicode.org/cldr/data/, http://source.icu-project.org/repos/icu/,
and http://www.unicode.org/utility/trac/browser/.

NOTICE TO USER: Carefully read the following legal agreement.
BY DOWNLOADING, INSTALLING, COPYING OR OTHERWISE USING UNICODE INC.'S DATA
FILES ("DATA FILES"), AND/OR SOFTWARE ("SOFTWARE"), YOU UNEQUIVOCALLY ACCEPT,
AND AGREE TO BE BOUND BY, ALL OF THE TERMS AND CONDITIONS OF THIS AGREEMENT.
IF YOU DO NOT AGREE, DO NOT DOWNLOAD, INSTALL, COPY, DISTRIBUTE OR USE THE
DATA FILES OR SOFTWARE.

COPYRIGHT AND PERMISSION NOTICE

Copyright © 1991-2016 Unicode, Inc. All rights reserved. Distributed under
the Terms of Use in http://www.unicode.org/copyright.html.

Permission is hereby granted, free of charge, to any person obtaining a copy
of the Unicode data files and any associated documentation (the "Data Files")
or Unicode software and any associated documentation (the "Software") to deal
in the Data Files or Software without restriction, including without
limitation the rights to use, copy, modify, merge, publish, distribute,
and/or sell copies of the Data Files or Software, and to permit persons to
whom the Data Files or Software are furnished to do so, provided that either
(a) this copyright and permission notice appear with all copies of the Data
Files or Software, or
(b) this copyright and permission notice appear in associated Documentation.

THE DATA FILES AND SOFTWARE ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR
CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
OF THE DATA FILES OR SOFTWARE.

Except as contained in this notice, the name of a copyright holder shall not
be used in advertising or otherwise to promote the sale, use or other
dealings in these Data Files or Software without prior written authorization
of the copyright holder.
Loading