-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathupdate-gz
More file actions
executable file
·95 lines (76 loc) · 3.24 KB
/
update-gz
File metadata and controls
executable file
·95 lines (76 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/python3
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)
# Begin by checking if gzip is supported. If not - return schema.json
htaccess_rules = f'''RewriteCond %{{REQUEST_URI}} !^/schema
RewriteRule ".?" "-" [S=LINES_TO_SKIP,L]
RewriteCond %{{HTTP:Accept-Encoding}} !gzip
RewriteRule ^schema(/.*)?$ /zotero-schema/schema.json [QSD,L]
RewriteCond %{{QUERY_STRING}} (?:^|&)locale=({'|'.join(schema['locales'].keys())})(?:&|$)
RewriteRule ^schema(/.*)?$ /zotero-schema/locales/%1.json.gz [QSD]
'''
#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)
# Each rule is only applid is gzip encoding is accepted
htaccess_rules += f'''RewriteCond %{{QUERY_STRING}} (?:^|&)locale=({country_code}(-.*)?)(?:&|$)
RewriteRule ^schema(/.*)?$ /zotero-schema/locales/{htaccess_mapings[country_code][0]}.json.gz [QSD,L]
'''
# Catch all for default schema with all locales
htaccess_rules += '''RewriteRule ^schema$ /zotero-schema/schema.json.gz [QSD,L]'''
htaccess_rules += f'''
<FilesMatch \"(schema|{'|'.join(schema['locales'].keys())})\.json\.gz$\">
# Serve correct encoding type
Header append Content-Encoding gzip
# Set correct content type
Header set Content-Type application/json
# Prevent double-gzip
SetEnv no-gzip 1
# Force proxies to cache gzipped & non-gzipped schema file separately
Header append Vary Accept-Encoding
# CORS
Header set Access-Control-Allow-Origin "*"
</FilesMatch>'''
htaccess_rules = htaccess_rules.replace("LINES_TO_SKIP", str(len(htaccess_rules.split('RewriteRule'))-2))
print(htaccess_rules)
# 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)