Skip to content

Commit

Permalink
Improve speed of extract_json_strings.py by caching output file pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
Qrox committed Dec 20, 2020
1 parent 74d1e43 commit 6c04172
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions lang/extract_json_strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -1038,7 +1038,7 @@ def gettextify(string, context=None, plural=None):


def writestr(filename, string, context=None, format_strings=False,
comment=None, pl_fmt=False):
comment=None, pl_fmt=False, _local_fp_cache=dict()):
"Wrap the string and write to the file."
if type(string) is list:
for entry in string:
Expand Down Expand Up @@ -1091,17 +1091,21 @@ def writestr(filename, string, context=None, format_strings=False,
raise WrongJSONItem(
"ERROR: value is not a string, dict, list, or None", string)

with open(filename, 'a', encoding="utf-8", newline='\n') as fs:
# Append developers comment
if comment:
tlcomment(fs, comment)
# most of the strings from json don't use string formatting.
# we must tell xgettext this explicitly
contains_percent = ("%" in str_singular or
(str_pl is not None and "%" in str_pl))
if not format_strings and contains_percent:
fs.write("# xgettext:no-python-format\n")
fs.write(gettextify(str_singular, context=context, plural=str_pl))
if filename in _local_fp_cache:
fs = _local_fp_cache[filename]
else:
fs = open(filename, 'a', encoding="utf-8", newline='\n')
_local_fp_cache[filename] = fs
# Append developers comment
if comment:
tlcomment(fs, comment)
# most of the strings from json don't use string formatting.
# we must tell xgettext this explicitly
contains_percent = ("%" in str_singular or
(str_pl is not None and "%" in str_pl))
if not format_strings and contains_percent:
fs.write("# xgettext:no-python-format\n")
fs.write(gettextify(str_singular, context=context, plural=str_pl))


def get_outfile(json_object_type):
Expand Down

0 comments on commit 6c04172

Please sign in to comment.