Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
makemessages: Write with orjson.
Anders Kaseorg2de648df0211
diff --git a/tools/i18n/process-mobile-i18n b/tools/i18n/process-mobile-i18nindex f09bc8ba109..2b19f57754a 100755--- a/tools/i18n/process-mobile-i18n+++ b/tools/i18n/process-mobile-i18n@@ -1,9 +1,10 @@#!/usr/bin/env python3-import jsonimport osimport refrom subprocess import check_output+import orjson+def get_json_filename(locale: str) -> str:return f"locale/{locale}/mobile.json"@@ -23,8 +24,8 @@ def get_locales() -> list[str]:def get_translation_stats(resource_path: str) -> dict[str, int]:- with open(resource_path) as raw_resource_file:- raw_info = json.load(raw_resource_file)+ with open(resource_path, "rb") as raw_resource_file:+ raw_info = orjson.loads(raw_resource_file.read())total = len(raw_info)not_translated = len([i for i in raw_info.items() if i[1] == ""])@@ -41,8 +42,12 @@ for locale in get_locales():locale_paths.append(path)stats_path = os.path.join("locale", "mobile_info.json")-with open(stats_path, "w") as f:- json.dump(translation_stats, f, indent=2, sort_keys=True)- f.write("\n")+with open(stats_path, "wb") as f:+ f.write(+ orjson.dumps(+ translation_stats,+ option=orjson.OPT_APPEND_NEWLINE | orjson.OPT_INDENT_2 | orjson.OPT_SORT_KEYS,+ )+ )print("Mobile stats file created at: " + stats_path)diff --git a/tools/i18n/unescape-contents b/tools/i18n/unescape-contentsindex 1fd34362755..d080086c90f 100755--- a/tools/i18n/unescape-contents+++ b/tools/i18n/unescape-contents@@ -2,10 +2,11 @@import argparseimport html-import jsonimport sysfrom typing import NamedTuple+import orjson+class CLIArgs(NamedTuple):unescape_html: bool@@ -38,8 +39,8 @@ if __name__ == "__main__":json_data: dict[str, str] = {}- with open(args.filename) as source:- json_data = json.load(source)+ with open(args.filename, "rb") as source:+ json_data = orjson.loads(source.read())if args.unescape_html:for key, value in json_data.items():@@ -51,11 +52,13 @@ if __name__ == "__main__":file=sys.stderr,)- with open(args.filename, mode="w") as dest:- # At least on Linux systems with LANG=en_US.UTF-8, ensure_ascii=False- # ensures our output uses real UTF-8 codepoints for human readability,- # rather than \u0000 style escape sequences, providing us a- # somewhat-implicit JSON unescape. This may behave in unexpected ways- # on other OSes or system encodings.- json.dump(json_data, dest, ensure_ascii=False, indent=args.indent_level)- dest.write("\n")+ with open(args.filename, mode="wb") as dest:+ # orjson ensures our output uses real UTF-8 codepoints for+ # human readability, rather than \u0000 style escape+ # sequences, providing us a somewhat-implicit JSON unescape.+ dest.write(+ orjson.dumps(+ json_data,+ option=orjson.OPT_APPEND_NEWLINE | orjson.OPT_INDENT_2 | orjson.OPT_SORT_KEYS,+ )+ )diff --git a/tools/i18n/update-for-legacy-translations b/tools/i18n/update-for-legacy-translationsindex cb9406bca72..652fe28d73a 100755--- a/tools/i18n/update-for-legacy-translations+++ b/tools/i18n/update-for-legacy-translations@@ -1,9 +1,10 @@#!/usr/bin/env python3-import jsonimport osimport refrom subprocess import check_output+import orjson+LEGACY_STRINGS_MAP = {"<p>You are searching for messages that belong to more than one channel, which is not possible.</p>": "<p>You are searching for messages that belong to more than one stream, which is not possible.</p>","<strong>{name}</strong> <i>(guest)</i> is not subscribed to this channel. They will not be notified if you mention them.": "<strong>{name}</strong> <i>(guest)</i> is not subscribed to this stream. They will not be notified if you mention them.",@@ -200,8 +201,8 @@ def get_locales() -> list[str]:def get_translations(path: str) -> dict[str, str]:- with open(path) as raw_resource_file:- translations = json.load(raw_resource_file)+ with open(path, "rb") as raw_resource_file:+ translations = orjson.loads(raw_resource_file.read())return translations@@ -226,9 +227,13 @@ def update_for_legacy_stream_translations(# Only replace file content if we've made any updates for legacy# translated strings.if number_of_updates > 0:- with open(path, "w") as f:- json.dump(updated_translations, f, ensure_ascii=False, indent=2, sort_keys=True)- f.write("\n")+ with open(path, "wb") as f:+ f.write(+ orjson.dumps(+ updated_translations,+ option=orjson.OPT_APPEND_NEWLINE | orjson.OPT_INDENT_2 | orjson.OPT_SORT_KEYS,+ )+ )print(f"Updated {number_of_updates} strings in: {path}")diff --git a/zerver/management/commands/compilemessages.py b/zerver/management/commands/compilemessages.pyindex 84262074e06..9314c238a9b 100644--- a/zerver/management/commands/compilemessages.py+++ b/zerver/management/commands/compilemessages.py@@ -1,4 +1,3 @@-import jsonimport osimport reimport unicodedata@@ -137,9 +136,13 @@ class Command(compilemessages.Command):info["percent_translated"] = percentagedata["languages"].append(info)- with open(output_path, "w") as writer:- json.dump(data, writer, indent=2, sort_keys=True)- writer.write("\n")+ with open(output_path, "wb") as writer:+ writer.write(+ orjson.dumps(+ data,+ option=orjson.OPT_APPEND_NEWLINE | orjson.OPT_INDENT_2 | orjson.OPT_SORT_KEYS,+ )+ )def get_translation_percentage(self, locale_path: str, locale: str) -> int:# backend statsdiff --git a/zerver/management/commands/makemessages.py b/zerver/management/commands/makemessages.pyindex 5e6c941fdd5..f5b2b0c4238 100644--- a/zerver/management/commands/makemessages.py+++ b/zerver/management/commands/makemessages.py@@ -34,13 +34,13 @@ https://stackoverflow.com/questions/2090717import globimport itertools-import jsonimport osimport reimport subprocessfrom collections.abc import Collection, Iterator, Mappingfrom typing import Any+import orjsonfrom django.core.management.base import CommandParserfrom django.core.management.commands import makemessagesfrom django.template.base import BLOCK_TAG_END, BLOCK_TAG_START@@ -215,7 +215,7 @@ class Command(makemessages.Command):"web/src/**/*.ts",])- translation_strings.extend(json.loads(extracted).values())+ translation_strings.extend(orjson.loads(extracted).values())return list(set(translation_strings))@@ -276,11 +276,18 @@ class Command(makemessages.Command):for locale, output_path in zip(self.get_locales(), self.get_output_paths(), strict=False):self.stdout.write(f"[frontend] processing locale {locale}")try:- with open(output_path) as reader:- old_strings = json.load(reader)+ with open(output_path, "rb") as reader:+ old_strings = orjson.loads(reader.read())except (OSError, ValueError):old_strings = {}new_strings = self.get_new_strings(old_strings, translation_strings, locale)- with open(output_path, "w") as writer:- json.dump(new_strings, writer, indent=2, sort_keys=True)+ with open(output_path, "wb") as writer:+ writer.write(+ orjson.dumps(+ new_strings,+ option=orjson.OPT_APPEND_NEWLINE+ | orjson.OPT_INDENT_2+ | orjson.OPT_SORT_KEYS,+ )+ )