-
Notifications
You must be signed in to change notification settings - Fork 30
Rebase the migration tools from python-l20n #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?