-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
redgettext.py
493 lines (435 loc) · 16.1 KB
/
redgettext.py
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
from __future__ import annotations
import argparse
import ast
import re
import sys
import time
from pathlib import Path
from typing import Dict, List, Optional, Union
import polib
__version__ = "4.0.0"
CLASS_DECORATOR_NAMES = ("cog_i18n",)
FUNCTION_DECORATOR_NAMES = ("command", "group")
DEFAULT_KEYWORDS = ["_"]
class Options:
def __init__(
self,
*,
omit_empty: bool = False,
keywords: List[str] = DEFAULT_KEYWORDS,
comment_tag: str = "Translators: ",
docstrings: bool = False,
cmd_docstrings: bool = False,
relative_to_cwd: bool = False,
output_dir: str = "locales",
output_filename: str = "messages.pot",
) -> None:
self.omit_empty = omit_empty
self.keywords = keywords
self.comment_tag = comment_tag.lstrip()
self.docstrings = docstrings
self.cmd_docstrings = cmd_docstrings
self.relative_to_cwd = relative_to_cwd
self.output_dir = output_dir
self.output_filename = output_filename
@classmethod
def from_args(cls, namespace: argparse.Namespace) -> Options:
return cls(
omit_empty=namespace.omit_empty,
keywords=namespace.keywords,
docstrings=namespace.docstrings,
cmd_docstrings=namespace.cmd_docstrings,
)
class POTFileManager:
def __init__(self, options: Options) -> None:
self.options = options
self._potfiles: Dict[Path, polib.POFile] = {}
self.current_infile: Optional[Path] = None
self._current_outfile: Optional[Path] = None
self.current_potfile: Optional[polib.POFile] = None
def set_current_file(self, path: Path) -> None:
opts = self.options
self.current_infile = path
if opts.relative_to_cwd:
current_dir = Path()
else:
current_dir = path.parent
self._current_outfile = current_dir / opts.output_dir / opts.output_filename
if self._current_outfile not in self._potfiles:
self.current_potfile = polib.POFile()
self._potfiles[self._current_outfile] = self.current_potfile
self.current_potfile.metadata = self.get_potfile_metadata()
@staticmethod
def get_potfile_metadata() -> Dict[str, str]:
return {
"Project-Id-Version": "PACKAGE VERSION",
"POT-Creation-Date": time.strftime("%Y-%m-%d %H:%M%z"),
"PO-Revision-Date": "YEAR-MO-DA HO:MI+ZONE",
"Last-Translator": "FULL NAME <EMAIL@ADDRESS>",
"Language-Team": "LANGUAGE <LL@li.org>",
"MIME-Version": "1.0",
"Content-Type": "text/plain; charset=UTF-8",
"Content-Transfer-Encoding": "8bit",
"Generated-By": f"redgettext {__version__}",
}
def write(self) -> None:
for outfile_path, potfile in self._potfiles.items():
if not potfile and self.options.omit_empty:
continue
outfile_path.parent.mkdir(parents=True, exist_ok=True)
potfile.sort(key=lambda e: e.occurrences[0])
potfile.save(str(outfile_path))
def add_entry(
self,
msgid: str,
*,
comment: Optional[str] = None,
lineno: int,
is_docstring: bool = False,
) -> None:
if self.current_potfile is None:
raise RuntimeError("There's no pot file set.")
occurrence = (str(self.current_infile), lineno)
if not msgid:
print(
f"{occurrence[0]}:{occurrence[1]}: warning: Empty msgid. Empty msgid is reserved"
' - gettext("") call returns po file metadata, not the empty string.',
file=sys.stderr,
)
return
comment = comment or ""
entry = self.current_potfile.find(msgid)
if is_docstring:
flags = ["docstring"]
else:
flags = []
if entry is None:
self.current_potfile.append(
polib.POEntry(
msgid=msgid,
comment=comment,
occurrences=[occurrence],
flags=flags,
)
)
else:
if not entry.comment:
entry.comment = comment
elif comment:
entry.comment = f"{entry.comment}\n{comment}"
if not entry.flags:
entry.flags = flags
entry.occurrences.append(occurrence)
entry.occurrences.sort()
class MessageExtractor(ast.NodeVisitor):
COMMENT_RE = re.compile(r"[\t ]*(#(?P<comment>.*))?")
def __init__(self, source: str, potfile_manager: POTFileManager) -> None:
self.source = source
self.potfile_manager = potfile_manager
self.options = potfile_manager.options
# {line_number: comment contents}
self.translator_comments: Dict[int, str] = {}
def collect_comments(self) -> None:
comment_tag = self.options.comment_tag
current_comment = []
pattern = self.COMMENT_RE
for lineno, line in enumerate(self.source.splitlines(), 1):
if match := pattern.fullmatch(line):
comment = match["comment"]
if comment is None:
# regex matched a whitespace-only line which we want to ignore
continue
comment = comment.strip()
# Collect a (potentially first) comment when we're either
# already collecting comments or we encounter a comment that starts with the tag.
if current_comment or comment.startswith(comment_tag):
current_comment.append(comment)
# regex doesn't match - this line is neither whitespace nor comment
elif current_comment:
# We're currently collecting comments so this is the time to save.
self.translator_comments[lineno] = "\n".join(current_comment)
current_comment.clear()
@classmethod
def extract_messages(cls, source: str, potfile_manager: POTFileManager) -> MessageExtractor:
module = ast.parse(source)
self = cls(source, potfile_manager)
self.collect_comments()
self.visit(module)
file = self.potfile_manager.current_infile
for lineno in self.translator_comments:
print(f"{file}:{lineno}: unused translator comment", file=sys.stderr)
return self
def get_literal_string(self, node: ast.AST) -> Optional[ast.Constant]:
if type(node) is ast.Constant and isinstance(node.value, str):
return node
else:
return None
def get_docstring_node(
self, node: Union[ast.Module, ast.ClassDef, ast.AsyncFunctionDef, ast.FunctionDef]
) -> Optional[ast.Constant]:
body = node.body
if not body:
return None
expr = body[0]
if type(expr) is not ast.Expr:
return None
return self.get_literal_string(expr.value)
def print_error(self, starting_node: ast.AST, message: str) -> None:
file = self.potfile_manager.current_infile
code = ast.get_source_segment(self.source, starting_node, padded=True)
print(f"*** {file}:{starting_node.lineno}: {message}:\n{code}", file=sys.stderr)
def visit_Call(self, node: ast.Call) -> None:
if type(node.func) is ast.Name:
if node.func.id not in self.options.keywords:
return self.generic_visit(node)
elif type(node.func) is ast.Attribute:
if node.func.attr not in self.options.keywords:
return self.generic_visit(node)
else:
return self.generic_visit(node)
if len(node.args) != 1:
self.print_error(
node, "Seen unexpected amount of positional arguments in gettext call"
)
return self.generic_visit(node)
if node.keywords:
self.print_error(node, "Seen unexpected keyword arguments in gettext call")
return self.generic_visit(node)
arg = node.args[0]
string_node = self.get_literal_string(arg)
if string_node is not None:
comments = []
for commentable_node in (node, string_node):
if commentable_node is not None:
comment = self.translator_comments.pop(commentable_node.lineno, None)
if comment is not None:
comments.append(comment)
self.add_entry(string_node, comment="\n".join(comments), starting_node=node)
else:
self.print_error(node, "Seen unexpected argument type in gettext call")
return self.generic_visit(node)
def visit_Module(self, node: ast.Module) -> None:
if not self.options.docstrings:
return self.generic_visit(node)
docstring_node = self.get_docstring_node(node)
if docstring_node is not None:
self.add_entry(docstring_node, is_docstring=True)
return self.generic_visit(node)
def visit_ClassDef(self, node: ast.ClassDef) -> None:
self.handle_class_or_function(node)
return self.generic_visit(node)
def visit_AsyncFunctionDef(self, node: ast.AsyncFunctionDef) -> None:
self.handle_class_or_function(node)
return self.generic_visit(node)
def visit_FunctionDef(self, node: ast.FunctionDef) -> None:
self.handle_class_or_function(node)
return self.generic_visit(node)
def handle_class_or_function(
self, node: Union[ast.ClassDef, ast.AsyncFunctionDef, ast.FunctionDef]
) -> None:
if self.options.docstrings:
pass
elif self.options.cmd_docstrings:
if isinstance(node, ast.ClassDef):
decorator_names = CLASS_DECORATOR_NAMES
else:
decorator_names = FUNCTION_DECORATOR_NAMES
for deco in node.decorator_list:
# @deco_name is not valid, it needs to be a call: @deco_name(...)
if type(deco) is not ast.Call:
continue
if type(deco.func) is ast.Name:
if deco.func.id in decorator_names:
break
elif type(deco.func) is ast.Attribute:
# in `a.b.c()`, only `c` is checked
if deco.func.attr in decorator_names:
break
else:
return
else:
return
docstring_node = self.get_docstring_node(node)
if docstring_node is not None:
self.add_entry(docstring_node, is_docstring=True)
def add_entry(
self,
node: ast.Constant,
*,
comment: str = "",
starting_node: Optional[ast.AST] = None,
is_docstring: bool = False,
) -> None:
if starting_node is None:
starting_node = node
self.potfile_manager.add_entry(
node.value,
comment=comment,
lineno=starting_node.lineno,
is_docstring=is_docstring,
)
def _parse_args(args: List[str]) -> argparse.Namespace:
parser = argparse.ArgumentParser(
prog="redgettext",
description="pygettext for Red-DiscordBot.",
usage="%(prog)s [OPTIONS] INFILE [INFILE ...]",
)
parser.add_argument(
"infiles",
nargs="*",
metavar="INFILE",
type=Path,
help=(
"An input file or directory. When a directory is specified, strings "
"will be extracted from all `.py` submodules. Can be multiple."
),
)
parser.add_argument(
"--command-docstrings",
"-c",
action="store_true",
dest="cmd_docstrings",
help=(
"Extract all cog and command docstrings. Has no effect when used with the "
"-D option."
),
)
parser.add_argument(
"--docstrings",
"-D",
action="store_true",
help="Extract all module, class, function and method docstrings.",
)
parser.add_argument(
"--exclude-files",
"-X",
metavar="PATTERN",
dest="excluded_files",
action="append",
help=(
"Exclude a glob of files from the list of `infiles`. These excluded files "
"will not be worked on. This pattern is treated as relative to the current "
"working directory. You can use this flag multiple times."
),
)
parser.add_argument(
"--include-context",
"-n",
action="store_true",
default=True,
help=(
"Include contextual comments for msgid entries. This is the default. "
"Opposite of --no-context."
),
)
parser.add_argument(
"--omit-empty",
action="store_true",
help="Empty .pot files will not be outputted.",
)
parser.add_argument(
"--output-dir",
"-O",
type=Path,
metavar="DIR",
default="locales",
help=(
"Output files will be placed in DIR. Default is `locales`. Specify `.` to "
"output in the same directory."
),
)
parser.add_argument(
"--output-filename",
"-o",
metavar="FILENAME",
default="messages.pot",
help="Rename the default output file from messages.pot to FILENAME.",
)
parser.add_argument(
"--no-context",
"-N",
action="store_false",
dest="include_context",
help="Don't include contextual comments for msgid entries.",
)
parser.add_argument(
"--recursive",
"-r",
action="store_true",
help=("For directories passed as input, recurse through subdirectories as well."),
)
parser.add_argument(
"--relative-to-cwd",
"-R",
action="store_true",
help=(
"Output directory will be relative to the current working directory "
"instead of the directory being translated."
),
)
parser.add_argument("--verbose", "-v", action="count", help="Be more verbose.")
parser.add_argument(
"--version",
"-V",
action="store_true",
help="Print the version of %(prog)s and exit.",
)
parser.add_argument(
"--width",
"-w",
type=int,
metavar="COLUMNS",
default=79,
help="Set the width of output to COLUMNS.",
)
return parser.parse_args(args)
def main(args: Optional[List[str]] = None) -> int:
if args is None:
args = sys.argv[1:]
args = _parse_args(args)
# TODO: Make these an option
args.keywords = DEFAULT_KEYWORDS
if args.version:
print(f"redgettext {__version__}")
return 0
if not args.infiles:
print("You must include at least one input file or directory.")
return 1
all_infiles: List[Path] = []
path: Path
for path in args.infiles:
if path.is_dir():
if args.recursive:
all_infiles.extend(path.glob("**/*.py"))
else:
all_infiles.extend(path.glob("*.py"))
else:
all_infiles.append(path)
# filter excluded files
if args.excluded_files:
for glob in args.excluded_files:
excluded_files = set(Path().glob(glob))
all_infiles = [f for f in all_infiles if f not in excluded_files]
# slurp through all the files
options = Options.from_args(args)
potfile_manager = POTFileManager(options)
for path in all_infiles:
if args.verbose:
print(f"Working on {path}")
with path.open("r") as fp:
potfile_manager.set_current_file(path)
try:
MessageExtractor.extract_messages(fp.read(), potfile_manager)
except SyntaxError as exc:
if exc.text is None:
msg = f"{exc.__class__.__name__}: {exc}"
else:
msg = "{0.text}\n{1:>{0.offset}}\n{2}: {0}".format(
exc, "^", type(exc).__name__
)
print(f"{path}:", msg, file=sys.stderr)
# write the output
potfile_manager.write()
return 0
if __name__ == "__main__":
sys.exit(main())