-
Notifications
You must be signed in to change notification settings - Fork 21
Python script to generate a .json.gz file per each locale #5
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
Open
abaevbog
wants to merge
11
commits into
zotero:master
Choose a base branch
from
abaevbog:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
c7e4f2d
python script to generate a .json.gz file per each locale
def1906
typo fix
d15f816
generating .htaccess file + cleanup
962f3c3
slightly refined htaccess rules
31e232e
htaccess rules with locale as query parameter instead of path
b90db18
defaulting to schema.json if gzip is not accepted, rules to serve cor…
abaevbog 498b190
skipping rules if url does not start with /schema
abaevbog 4ccd056
updated filter to not skip rules on zotero-schema
abaevbog e3e1ed5
simplifications for the rules
abaevbog 7aef704
No L in skip rule
abaevbog 393ffe5
typo fix and last L
abaevbog 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 |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| schema.json.gz | ||
| locales/ |
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,68 @@ | ||
| import os | ||
| import shutil | ||
| import gzip | ||
| import json | ||
| from pathlib import Path | ||
| import copy | ||
|
|
||
| parent_folder = Path(__file__).resolve().parent.parent | ||
| gzipped_file = f'{parent_folder}/schema.json.gz' | ||
| schema_file = f'{parent_folder}/schema.json' | ||
| locales_folder = f'{parent_folder}/locales' | ||
|
|
||
| with open(schema_file, 'r') as f: | ||
| schema_text = f.read() | ||
| schema = json.loads(schema_text) | ||
|
|
||
| if os.path.exists(locales_folder): | ||
| shutil.rmtree(locales_folder) | ||
|
|
||
| os.mkdir(locales_folder) | ||
|
|
||
| # String that accumulates the rules to paste into htaccess | ||
| htaccess_rules = f"RewriteRule ^schema/({'|'.join(schema['locales'].keys())})$ /zotero-schema/locales/$1.gz [L]" | ||
| #Dict to collect country codes with locales to handle multiple locales per country code | ||
| htaccess_mapings = {} | ||
|
|
||
| # For each item in the 'locales' list of the schema | ||
| for locale_name in schema['locales'].keys(): | ||
| schema_with_one_locale = copy.deepcopy(schema) | ||
| current_locale = schema_with_one_locale['locales'][locale_name] | ||
|
|
||
| schema_with_one_locale['locales'] = {locale_name : current_locale} | ||
|
|
||
| # Rule to match by country | ||
| if locale_name[:2] in htaccess_mapings: | ||
| htaccess_mapings[locale_name[:2]].append(locale_name) | ||
| else: | ||
| htaccess_mapings[locale_name[:2]] = [locale_name] | ||
|
|
||
| # generate a gzip file and save it to the locales subdirectory | ||
| with gzip.open(f'{locales_folder}/{locale_name}.json.gz', 'wb') as f_out: | ||
| json_bytes = json.dumps(schema_with_one_locale, ensure_ascii=False).encode('utf8') | ||
| f_out.write(json_bytes) | ||
|
|
||
|
|
||
| def locale_sort_key(a): | ||
| # Prefer en-US | ||
| if a == 'en-US': | ||
| return 'A' | ||
| # Prefer canonical locales | ||
| if a[:2] == a[3:5].lower(): | ||
| return 'A' | ||
| return a[3:5] | ||
|
|
||
| # For every country code, sort locale candidates and add rule to htacecss | ||
| for country_code in htaccess_mapings.keys(): | ||
| htaccess_mapings[country_code].sort(key=locale_sort_key) | ||
| htaccess_rules += f'\nRewriteRule ^schema/{country_code}(-.*)?$ /zotero-schema/locales/{htaccess_mapings[country_code][0]}.gz [L]' | ||
|
|
||
| # Catch all for default schema with all locales | ||
| htaccess_rules += f'\nRewriteRule ^schema/* /zotero-schema/schema.json.gz [L]' | ||
|
|
||
| print("--- .htacess rules --- \n" + htaccess_rules + "\n--- ---") | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can skip the header and footer |
||
|
|
||
|
|
||
| # Create gzip for the main schema | ||
| with open(schema_file, 'rb') as f_in, gzip.open(gzipped_file, 'wb') as f_out: | ||
| shutil.copyfileobj(f_in, f_out) | ||
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.
It should be a
localequery string parameter, not a path component.