Mercurial > p > roundup > code
comparison roundup/scripts/roundup_gettext.py @ 8084:2943140f5286
chore: some ruff linter cleanups.
| author | John Rouillard <rouilj@ieee.org> |
|---|---|
| date | Sun, 14 Jul 2024 12:52:10 -0400 |
| parents | d1c29284ccd9 |
| children | 1045425c23b2 |
comparison
equal
deleted
inserted
replaced
| 8083:60e92a540ca7 | 8084:2943140f5286 |
|---|---|
| 3 # Copyright 2004 Richard Jones (richard@mechanicalcat.net) | 3 # Copyright 2004 Richard Jones (richard@mechanicalcat.net) |
| 4 | 4 |
| 5 """Extract translatable strings from tracker templates and detectors/extensions""" | 5 """Extract translatable strings from tracker templates and detectors/extensions""" |
| 6 | 6 |
| 7 from __future__ import print_function | 7 from __future__ import print_function |
| 8 | |
| 8 import os | 9 import os |
| 9 import sys | |
| 10 import tempfile | |
| 11 | 10 |
| 12 # --- patch sys.path to make sure 'import roundup' finds correct version | 11 # --- patch sys.path to make sure 'import roundup' finds correct version |
| 13 import os.path as osp | 12 import os.path as osp |
| 13 import sys | |
| 14 import tempfile | |
| 14 | 15 |
| 15 thisdir = osp.dirname(osp.abspath(__file__)) | 16 thisdir = osp.dirname(osp.abspath(__file__)) |
| 16 rootdir = osp.dirname(osp.dirname(thisdir)) | 17 rootdir = osp.dirname(osp.dirname(thisdir)) |
| 17 if (osp.exists(thisdir + '/__init__.py') and | 18 if (osp.exists(thisdir + '/__init__.py') and |
| 18 osp.exists(rootdir + '/roundup/__init__.py')): | 19 osp.exists(rootdir + '/roundup/__init__.py')): |
| 19 # the script is located inside roundup source code | 20 # the script is located inside roundup source code |
| 20 sys.path.insert(0, rootdir) | 21 sys.path.insert(0, rootdir) |
| 21 # --/ | 22 # --/ |
| 22 | 23 |
| 23 | 24 |
| 25 from roundup.cgi.TAL import talgettext | |
| 24 from roundup.i18n import _ | 26 from roundup.i18n import _ |
| 25 from roundup.cgi.TAL import talgettext | 27 from roundup.pygettext import TokenEater, make_escapes, tokenize |
| 26 from roundup.pygettext import make_escapes, TokenEater, tokenize | |
| 27 | 28 |
| 28 try: | 29 try: |
| 29 import polib | 30 import polib |
| 30 except ImportError: | 31 except ImportError: |
| 31 print(_("\nExtracting translatable strings from html templates.\n" | 32 print(_("\nExtracting translatable strings from html templates.\n" |
| 32 "Because the 'polib' module is missing, unable to extract\n" | 33 "Because the 'polib' module is missing, unable to extract\n" |
| 33 "translations from detectors or extensions.\n" | 34 "translations from detectors or extensions.\n" |
| 34 "The 'polib' module can be installed with pip.\n")) | 35 "The 'polib' module can be installed with pip.\n")) |
| 35 polib = None | 36 polib = None |
| 37 | |
| 36 | 38 |
| 37 # from pygettext's main(): | 39 # from pygettext's main(): |
| 38 class Options: | 40 class Options: |
| 39 # constants | 41 # constants |
| 40 GNU = 1 | 42 GNU = 1 |
| 51 width = 10 | 53 width = 10 |
| 52 excludefilename = '' | 54 excludefilename = '' |
| 53 docstrings = 0 | 55 docstrings = 0 |
| 54 nodocstrings = {} | 56 nodocstrings = {} |
| 55 toexclude = [] # TODO we should exclude all strings already found in some template | 57 toexclude = [] # TODO we should exclude all strings already found in some template |
| 58 | |
| 56 | 59 |
| 57 tokeneater_options = Options() | 60 tokeneater_options = Options() |
| 58 | 61 |
| 59 # name of message template file. | 62 # name of message template file. |
| 60 # i don't think this will ever need to be changed, but still... | 63 # i don't think this will ever need to be changed, but still... |
| 102 # tokeneater_options.keywords | 105 # tokeneater_options.keywords |
| 103 # this is partly assembled from pygettext's main() | 106 # this is partly assembled from pygettext's main() |
| 104 make_escapes(not tokeneater_options.escape) | 107 make_escapes(not tokeneater_options.escape) |
| 105 | 108 |
| 106 pyfiles = [] | 109 pyfiles = [] |
| 107 for source in ["detectors", "extensions"] : | 110 for source in ["detectors", "extensions"]: |
| 108 for root, dirs, files in os.walk (os.path.join ("..", source)) : | 111 for root, _dirs, files in os.walk(os.path.join("..", source)): |
| 109 pyfiles.extend ([os.path.join (root, f) for f in files if f.endswith (".py")]) | 112 pyfiles.extend([os.path.join(root, f) for f in files if f.endswith(".py")]) |
| 110 | 113 |
| 111 eater = TokenEater (tokeneater_options) | 114 eater = TokenEater(tokeneater_options) |
| 112 | 115 |
| 113 for filename in pyfiles : | 116 for filename in pyfiles: |
| 114 eater.set_filename (filename) | 117 eater.set_filename(filename) |
| 115 with open (filename, "r") as f: | 118 with open(filename, "r") as f: |
| 116 try: | 119 try: |
| 117 for token in tokenize.generate_tokens(f.readline): | 120 for token in tokenize.generate_tokens(f.readline): |
| 118 eater(*token) | 121 eater(*token) |
| 119 except tokenize.TokenError as e: | 122 except tokenize.TokenError as e: |
| 120 print('%s: %s, line %d, column %d' % ( | 123 print('%s: %s, line %d, column %d' % ( |
| 121 e[0], filename, e[1][0], e[1][1]), file=sys.stderr) | 124 e[0], filename, e[1][0], e[1][1]), file=sys.stderr) |
| 122 | 125 |
| 123 with tempfile.NamedTemporaryFile("w") as tf : | 126 with tempfile.NamedTemporaryFile("w") as tf: |
| 124 eater.write(tf) | 127 eater.write(tf) |
| 125 tf.seek (0) | 128 tf.seek(0) |
| 126 p1 = polib.pofile(TEMPLATE_FILE) | 129 p1 = polib.pofile(TEMPLATE_FILE) |
| 127 p2 = polib.pofile(tf.name) | 130 p2 = polib.pofile(tf.name) |
| 128 | 131 |
| 129 p2_msg_ids = set([e.msgid for e in p2]) | 132 p2_msg_ids = {e.msgid for e in p2} |
| 130 for e in p1 : | 133 for e in p1: |
| 131 if e.msgid in p2_msg_ids : | 134 if e.msgid in p2_msg_ids: |
| 132 p2_e = p2.find (e.msgid) | 135 p2_e = p2.find(e.msgid) |
| 133 e.occurrences.extend (p2_e.occurrences) | 136 e.occurrences.extend(p2_e.occurrences) |
| 134 p2_msg_ids.remove (e.msgid) | 137 p2_msg_ids.remove(e.msgid) |
| 135 | 138 |
| 136 for msgid in p2_msg_ids : | 139 for msgid in p2_msg_ids: |
| 137 p1.append (p2.find (msgid)) | 140 p1.append(p2.find(msgid)) |
| 138 p1.save () | 141 p1.save() |
| 142 | |
| 139 | 143 |
| 140 if __name__ == "__main__": | 144 if __name__ == "__main__": |
| 141 run() | 145 run() |
| 142 | 146 |
| 143 # vim: set et sts=4 sw=4 : | 147 # vim: set et sts=4 sw=4 : |
