annotate roundup/pygettext.py @ 8478:ed4ef394d5d6

doc: initial attempt to document setup of pgp support for email. Used an AI assistant to help write this. Basic gpg commands seem to work, but I have not tested this totally. Docs basically follow the setup used for pgp testing in the test suite. It looks like roundup accepts signed emails as well as encrypted and signed emails. But it does not generate signed emails. Also it looks like there is no PGP support for alternate email addresses. Only primary addresses can do PGP emails.
author John Rouillard <rouilj@ieee.org>
date Sat, 15 Nov 2025 16:59:24 -0500
parents 185335b2301b
children 9c3ec0a5c7fc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8080
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
1 #! /usr/bin/env python
8091
586f76eb33e8 fix: keep python2 working a little longer.
John Rouillard <rouilj@ieee.org>
parents: 8083
diff changeset
2 # -*- coding: utf-8 -*-
8080
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
3 # Originally written by Barry Warsaw <barry@python.org>
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
4 #
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
5 # Minimally patched to make it even more xgettext compatible
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
6 # by Peter Funk <pf@artcom-gmbh.de>
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
7 #
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
8 # 2002-11-22 J�rgen Hermann <jh@web.de>
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
9 # Added checks that _() only contains string literals, and
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
10 # command line args are resolved to module lists, i.e. you
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
11 # can now pass a filename, a module or package name, or a
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
12 # directory (including globbing chars, important for Win32).
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
13 # Made docstring fit in 80 chars wide displays using pydoc.
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
14 #
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
15 # 2024-07-13 John Rouillard (rouilj@users.sourceforge.net)
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
16 # Converted from python 2.
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
17
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
18 from __future__ import print_function
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
19
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
20 # for selftesting
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
21 try:
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
22 import fintl
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
23 _ = fintl.gettext
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
24 except ImportError:
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
25 _ = lambda s: s
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
26
8083
60e92a540ca7 chore: some ruff linter cleanups.
John Rouillard <rouilj@ieee.org>
parents: 8082
diff changeset
27 import getopt
60e92a540ca7 chore: some ruff linter cleanups.
John Rouillard <rouilj@ieee.org>
parents: 8082
diff changeset
28 import glob
60e92a540ca7 chore: some ruff linter cleanups.
John Rouillard <rouilj@ieee.org>
parents: 8082
diff changeset
29 import importlib
60e92a540ca7 chore: some ruff linter cleanups.
John Rouillard <rouilj@ieee.org>
parents: 8082
diff changeset
30 import operator
60e92a540ca7 chore: some ruff linter cleanups.
John Rouillard <rouilj@ieee.org>
parents: 8082
diff changeset
31 import os
60e92a540ca7 chore: some ruff linter cleanups.
John Rouillard <rouilj@ieee.org>
parents: 8082
diff changeset
32 import sys
60e92a540ca7 chore: some ruff linter cleanups.
John Rouillard <rouilj@ieee.org>
parents: 8082
diff changeset
33 import time
60e92a540ca7 chore: some ruff linter cleanups.
John Rouillard <rouilj@ieee.org>
parents: 8082
diff changeset
34 import token
60e92a540ca7 chore: some ruff linter cleanups.
John Rouillard <rouilj@ieee.org>
parents: 8082
diff changeset
35 import tokenize
60e92a540ca7 chore: some ruff linter cleanups.
John Rouillard <rouilj@ieee.org>
parents: 8082
diff changeset
36 from functools import reduce
60e92a540ca7 chore: some ruff linter cleanups.
John Rouillard <rouilj@ieee.org>
parents: 8082
diff changeset
37
60e92a540ca7 chore: some ruff linter cleanups.
John Rouillard <rouilj@ieee.org>
parents: 8082
diff changeset
38 __version__ = '1.5'
60e92a540ca7 chore: some ruff linter cleanups.
John Rouillard <rouilj@ieee.org>
parents: 8082
diff changeset
39
8108
78bca158e1e6 fix: do not translate help text or POT header.
John Rouillard <rouilj@ieee.org>
parents: 8091
diff changeset
40 __doc__ = """pygettext -- Python equivalent of xgettext(1)
8080
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
41
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
42 Many systems (Solaris, Linux, Gnu) provide extensive tools that ease the
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
43 internationalization of C programs. Most of these tools are independent of
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
44 the programming language and can be used from within Python programs.
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
45 Martin von Loewis' work[1] helps considerably in this regard.
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
46
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
47 There's one problem though; xgettext is the program that scans source code
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
48 looking for message strings, but it groks only C (or C++). Python
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
49 introduces a few wrinkles, such as dual quoting characters, triple quoted
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
50 strings, and raw strings. xgettext understands none of this.
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
51
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
52 Enter pygettext, which uses Python's standard tokenize module to scan
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
53 Python source code, generating .pot files identical to what GNU xgettext[2]
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
54 generates for C and C++ code. From there, the standard GNU tools can be
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
55 used.
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
56
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
57 A word about marking Python strings as candidates for translation. GNU
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
58 xgettext recognizes the following keywords: gettext, dgettext, dcgettext,
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
59 and gettext_noop. But those can be a lot of text to include all over your
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
60 code. C and C++ have a trick: they use the C preprocessor. Most
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
61 internationalized C source includes a #define for gettext() to _() so that
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
62 what has to be written in the source is much less. Thus these are both
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
63 translatable strings:
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
64
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
65 gettext("Translatable String")
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
66 _("Translatable String")
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
67
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
68 Python of course has no preprocessor so this doesn't work so well. Thus,
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
69 pygettext searches only for _() by default, but see the -k/--keyword flag
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
70 below for how to augment this.
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
71
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
72 [1] http://www.python.org/workshops/1997-10/proceedings/loewis.html
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
73 [2] http://www.gnu.org/software/gettext/gettext.html
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
74
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
75 NOTE: pygettext attempts to be option and feature compatible with GNU
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
76 xgettext where ever possible. However some options are still missing or are
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
77 not fully implemented. Also, xgettext's use of command line switches with
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
78 option arguments is broken, and in these cases, pygettext just defines
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
79 additional switches.
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
80
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
81 Usage: pygettext [options] inputfile ...
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
82
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
83 Options:
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
84
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
85 -a
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
86 --extract-all
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
87 Extract all strings.
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
88
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
89 -d name
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
90 --default-domain=name
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
91 Rename the default output file from messages.pot to name.pot.
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
92
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
93 -E
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
94 --escape
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
95 Replace non-ASCII characters with octal escape sequences.
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
96
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
97 -D
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
98 --docstrings
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
99 Extract module, class, method, and function docstrings. These do
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
100 not need to be wrapped in _() markers, and in fact cannot be for
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
101 Python to consider them docstrings. (See also the -X option).
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
102
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
103 -h
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
104 --help
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
105 Print this help message and exit.
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
106
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
107 -k word
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
108 --keyword=word
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
109 Keywords to look for in addition to the default set, which are:
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
110 %(DEFAULTKEYWORDS)s
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
111
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
112 You can have multiple -k flags on the command line.
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
113
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
114 -K
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
115 --no-default-keywords
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
116 Disable the default set of keywords (see above). Any keywords
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
117 explicitly added with the -k/--keyword option are still recognized.
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
118
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
119 --no-location
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
120 Do not write filename/lineno location comments.
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
121
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
122 -n
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
123 --add-location
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
124 Write filename/lineno location comments indicating where each
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
125 extracted string is found in the source. These lines appear before
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
126 each msgid. The style of comments is controlled by the -S/--style
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
127 option. This is the default.
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
128
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
129 -o filename
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
130 --output=filename
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
131 Rename the default output file from messages.pot to filename. If
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
132 filename is `-' then the output is sent to standard out.
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
133
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
134 -p dir
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
135 --output-dir=dir
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
136 Output files will be placed in directory dir.
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
137
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
138 -S stylename
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
139 --style stylename
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
140 Specify which style to use for location comments. Two styles are
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
141 supported:
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
142
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
143 Solaris # File: filename, line: line-number
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
144 GNU #: filename:line
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
145
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
146 The style name is case insensitive. GNU style is the default.
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
147
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
148 -v
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
149 --verbose
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
150 Print the names of the files being processed.
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
151
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
152 -V
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
153 --version
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
154 Print the version of pygettext and exit.
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
155
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
156 -w columns
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
157 --width=columns
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
158 Set width of output to columns.
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
159
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
160 -x filename
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
161 --exclude-file=filename
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
162 Specify a file that contains a list of strings that are not be
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
163 extracted from the input files. Each string to be excluded must
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
164 appear on a line by itself in the file.
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
165
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
166 -X filename
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
167 --no-docstrings=filename
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
168 Specify a file that contains a list of files (one per line) that
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
169 should not have their docstrings extracted. This is only useful in
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
170 conjunction with the -D option above.
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
171
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
172 If `inputfile' is -, standard input is read.
8108
78bca158e1e6 fix: do not translate help text or POT header.
John Rouillard <rouilj@ieee.org>
parents: 8091
diff changeset
173 """
8080
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
174
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
175 default_keywords = ['_']
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
176 DEFAULTKEYWORDS = ', '.join(default_keywords)
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
177
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
178 EMPTYSTRING = ''
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
179
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
180 # The normal pot-file header. msgmerge and Emacs's po-mode work better if it's
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
181 # there.
8108
78bca158e1e6 fix: do not translate help text or POT header.
John Rouillard <rouilj@ieee.org>
parents: 8091
diff changeset
182 pot_header = '''\
8080
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
183 # SOME DESCRIPTIVE TITLE.
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
184 # Copyright (C) YEAR ORGANIZATION
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
185 # FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
186 #
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
187 msgid ""
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
188 msgstr ""
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
189 "Project-Id-Version: PACKAGE VERSION\\n"
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
190 "POT-Creation-Date: %(time)s\\n"
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
191 "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\\n"
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
192 "Last-Translator: FULL NAME <EMAIL@ADDRESS>\\n"
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
193 "Language-Team: LANGUAGE <LL@li.org>\\n"
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
194 "Language: \\n"
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
195 "MIME-Version: 1.0\\n"
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
196 "Content-Type: text/plain; charset=CHARSET\\n"
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
197 "Content-Transfer-Encoding: ENCODING\\n"
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
198 "Generated-By: pygettext.py %(version)s\\n"
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
199
8108
78bca158e1e6 fix: do not translate help text or POT header.
John Rouillard <rouilj@ieee.org>
parents: 8091
diff changeset
200 '''
8080
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
201
8083
60e92a540ca7 chore: some ruff linter cleanups.
John Rouillard <rouilj@ieee.org>
parents: 8082
diff changeset
202
8080
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
203 def usage(code, msg=''):
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
204 print(__doc__ % globals(), file=sys.stderr)
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
205 if msg:
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
206 print(msg, file=sys.stderr)
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
207 sys.exit(code)
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
208
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
209
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
210 escapes = []
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
211
8083
60e92a540ca7 chore: some ruff linter cleanups.
John Rouillard <rouilj@ieee.org>
parents: 8082
diff changeset
212
8080
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
213 def make_escapes(pass_iso8859):
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
214 global escapes
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
215 escapes = [chr(i) for i in range(256)]
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
216 if pass_iso8859:
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
217 # Allow iso-8859 characters to pass through so that e.g. 'msgid
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
218 # "H�he"' would result not result in 'msgid "H\366he"'. Otherwise we
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
219 # escape any character outside the 32..126 range.
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
220 mod = 128
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
221 else:
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
222 mod = 256
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
223 for i in range(mod):
8083
60e92a540ca7 chore: some ruff linter cleanups.
John Rouillard <rouilj@ieee.org>
parents: 8082
diff changeset
224 if not (32 <= i <= 126):
8080
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
225 escapes[i] = "\\%03o" % i
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
226 escapes[ord('\\')] = '\\\\'
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
227 escapes[ord('\t')] = '\\t'
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
228 escapes[ord('\r')] = '\\r'
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
229 escapes[ord('\n')] = '\\n'
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
230 escapes[ord('\"')] = '\\"'
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
231
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
232
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
233 def escape(s):
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
234 s = list(s)
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
235 for i in range(len(s)):
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
236 s[i] = escapes[ord(s[i])]
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
237 return EMPTYSTRING.join(s)
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
238
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
239
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
240 def safe_eval(s):
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
241 # unwrap quotes, safely
8083
60e92a540ca7 chore: some ruff linter cleanups.
John Rouillard <rouilj@ieee.org>
parents: 8082
diff changeset
242 return eval(s, {'__builtins__': {}}, {})
8080
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
243
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
244
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
245 def normalize(s):
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
246 # This converts the various Python string types into a format that is
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
247 # appropriate for .po files, namely much closer to C style.
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
248 lines = s.split('\n')
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
249 if len(lines) == 1:
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
250 s = '"' + escape(s) + '"'
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
251 else:
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
252 if not lines[-1]:
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
253 del lines[-1]
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
254 lines[-1] = lines[-1] + '\n'
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
255 for i in range(len(lines)):
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
256 lines[i] = escape(lines[i])
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
257 lineterm = '\\n"\n"'
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
258 s = '""\n"' + lineterm.join(lines) + '"'
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
259 return s
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
260
8083
60e92a540ca7 chore: some ruff linter cleanups.
John Rouillard <rouilj@ieee.org>
parents: 8082
diff changeset
261
60e92a540ca7 chore: some ruff linter cleanups.
John Rouillard <rouilj@ieee.org>
parents: 8082
diff changeset
262 def containsAny(string, inset):
8080
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
263 """Check whether 'str' contains ANY of the chars in 'set'"""
8083
60e92a540ca7 chore: some ruff linter cleanups.
John Rouillard <rouilj@ieee.org>
parents: 8082
diff changeset
264 return 1 in [c in string for c in inset]
8080
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
265
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
266
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
267 def _get_modpkg_path(dotted_name, pathlist=None):
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
268 """Get the filesystem path for a module or a package.
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
269
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
270 Return the file system path to a file for a module, and to a directory for
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
271 a package. Return None if the name is not found, or is a builtin or
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
272 extension module.
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
273 """
8082
a4127d7afaa9 fix: remove references to imp in untouched code
John Rouillard <rouilj@ieee.org>
parents: 8080
diff changeset
274 pathname = None
8083
60e92a540ca7 chore: some ruff linter cleanups.
John Rouillard <rouilj@ieee.org>
parents: 8082
diff changeset
275 r = importlib.util.find_spec(dotted_name, pathlist)
8080
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
276
8082
a4127d7afaa9 fix: remove references to imp in untouched code
John Rouillard <rouilj@ieee.org>
parents: 8080
diff changeset
277 if r.loader.is_package(dotted_name):
a4127d7afaa9 fix: remove references to imp in untouched code
John Rouillard <rouilj@ieee.org>
parents: 8080
diff changeset
278 pathname = r.submodule_search_locations[0]
8083
60e92a540ca7 chore: some ruff linter cleanups.
John Rouillard <rouilj@ieee.org>
parents: 8082
diff changeset
279 elif issubclass(r.loader.__class__, (importlib.abc.SourceLoader)):
8082
a4127d7afaa9 fix: remove references to imp in untouched code
John Rouillard <rouilj@ieee.org>
parents: 8080
diff changeset
280 pathname = r.origin
8080
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
281 return pathname
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
282
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
283
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
284 def getFilesForName(name):
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
285 """Get a list of module files for a filename, a module or package name,
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
286 or a directory.
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
287 """
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
288 if not os.path.exists(name):
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
289 # check for glob chars
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
290 if containsAny(name, "*?[]"):
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
291 files = glob.glob(name)
8083
60e92a540ca7 chore: some ruff linter cleanups.
John Rouillard <rouilj@ieee.org>
parents: 8082
diff changeset
292 lst = []
8080
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
293 for file in files:
8083
60e92a540ca7 chore: some ruff linter cleanups.
John Rouillard <rouilj@ieee.org>
parents: 8082
diff changeset
294 lst.extend(getFilesForName(file))
60e92a540ca7 chore: some ruff linter cleanups.
John Rouillard <rouilj@ieee.org>
parents: 8082
diff changeset
295 return lst
8080
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
296
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
297 # try to find module or package
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
298 name = _get_modpkg_path(name)
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
299 if not name:
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
300 return []
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
301
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
302 if os.path.isdir(name):
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
303 # find all python files in directory
8083
60e92a540ca7 chore: some ruff linter cleanups.
John Rouillard <rouilj@ieee.org>
parents: 8082
diff changeset
304 lst = []
8080
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
305 # get extension for python source files
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
306 if '_py_ext' not in globals():
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
307 global _py_ext
8082
a4127d7afaa9 fix: remove references to imp in untouched code
John Rouillard <rouilj@ieee.org>
parents: 8080
diff changeset
308 _py_ext = importlib.machinery.SOURCE_SUFFIXES
8080
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
309 for root, dirs, files in os.walk(name):
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
310 # don't recurse into CVS directories
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
311 if 'CVS' in dirs:
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
312 dirs.remove('CVS')
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
313 # add all *.py files to list
8083
60e92a540ca7 chore: some ruff linter cleanups.
John Rouillard <rouilj@ieee.org>
parents: 8082
diff changeset
314 lst.extend(
8080
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
315 [os.path.join(root, file) for file in files
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
316 if os.path.splitext(file)[1] == _py_ext]
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
317 )
8083
60e92a540ca7 chore: some ruff linter cleanups.
John Rouillard <rouilj@ieee.org>
parents: 8082
diff changeset
318 return lst
8080
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
319 elif os.path.exists(name):
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
320 # a single file
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
321 return [name]
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
322
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
323 return []
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
324
8083
60e92a540ca7 chore: some ruff linter cleanups.
John Rouillard <rouilj@ieee.org>
parents: 8082
diff changeset
325
8080
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
326 class TokenEater:
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
327 def __init__(self, options):
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
328 self.__options = options
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
329 self.__messages = {}
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
330 self.__state = self.__waiting
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
331 self.__data = []
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
332 self.__lineno = -1
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
333 self.__freshmodule = 1
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
334 self.__curfile = None
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
335
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
336 def __call__(self, ttype, tstring, stup, etup, line):
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
337 # dispatch
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
338 ## import token
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
339 ## print(('ttype:', token.tok_name[ttype], \
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
340 ## 'tstring:', tstring), file=sys.stderr)
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
341 self.__state(ttype, tstring, stup[0])
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
342
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
343 def __waiting(self, ttype, tstring, lineno):
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
344 opts = self.__options
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
345 # Do docstring extractions, if enabled
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
346 if opts.docstrings and not opts.nodocstrings.get(self.__curfile):
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
347 # module docstring?
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
348 if self.__freshmodule:
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
349 if ttype == tokenize.STRING:
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
350 self.__addentry(safe_eval(tstring), lineno, isdocstring=1)
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
351 self.__freshmodule = 0
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
352 elif ttype not in (tokenize.COMMENT, tokenize.NL):
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
353 self.__freshmodule = 0
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
354 return
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
355 # class docstring?
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
356 if ttype == tokenize.NAME and tstring in ('class', 'def'):
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
357 self.__state = self.__suiteseen
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
358 return
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
359 if ttype == tokenize.NAME and tstring in opts.keywords:
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
360 self.__state = self.__keywordseen
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
361
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
362 def __suiteseen(self, ttype, tstring, lineno):
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
363 # ignore anything until we see the colon
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
364 if ttype == tokenize.OP and tstring == ':':
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
365 self.__state = self.__suitedocstring
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
366
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
367 def __suitedocstring(self, ttype, tstring, lineno):
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
368 # ignore any intervening noise
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
369 if ttype == tokenize.STRING:
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
370 self.__addentry(safe_eval(tstring), lineno, isdocstring=1)
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
371 self.__state = self.__waiting
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
372 elif ttype not in (tokenize.NEWLINE, tokenize.INDENT,
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
373 tokenize.COMMENT):
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
374 # there was no class docstring
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
375 self.__state = self.__waiting
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
376
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
377 def __keywordseen(self, ttype, tstring, lineno):
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
378 if ttype == tokenize.OP and tstring == '(':
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
379 self.__data = []
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
380 self.__lineno = lineno
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
381 self.__state = self.__openseen
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
382 else:
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
383 self.__state = self.__waiting
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
384
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
385 def __openseen(self, ttype, tstring, lineno):
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
386 if ttype == tokenize.OP and tstring == ')':
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
387 # We've seen the last of the translatable strings. Record the
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
388 # line number of the first line of the strings and update the list
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
389 # of messages seen. Reset state for the next batch. If there
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
390 # were no strings inside _(), then just ignore this entry.
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
391 if self.__data:
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
392 self.__addentry(EMPTYSTRING.join(self.__data))
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
393 self.__state = self.__waiting
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
394 elif ttype == tokenize.STRING:
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
395 self.__data.append(safe_eval(tstring))
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
396 elif ttype not in [tokenize.COMMENT, token.INDENT, token.DEDENT,
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
397 token.NEWLINE, tokenize.NL]:
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
398 # warn if we see anything else than STRING or whitespace
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
399 print(_(
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
400 '*** %(file)s:%(lineno)s: Seen unexpected token "%(token)s"'
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
401 ) % {
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
402 'token': tstring,
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
403 'file': self.__curfile,
8083
60e92a540ca7 chore: some ruff linter cleanups.
John Rouillard <rouilj@ieee.org>
parents: 8082
diff changeset
404 'lineno': self.__lineno,
8080
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
405 }, file=sys.stderr)
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
406 self.__state = self.__waiting
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
407
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
408 def __addentry(self, msg, lineno=None, isdocstring=0):
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
409 if lineno is None:
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
410 lineno = self.__lineno
8083
60e92a540ca7 chore: some ruff linter cleanups.
John Rouillard <rouilj@ieee.org>
parents: 8082
diff changeset
411 if msg not in self.__options.toexclude:
8080
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
412 entry = (self.__curfile, lineno)
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
413 self.__messages.setdefault(msg, {})[entry] = isdocstring
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
414
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
415 def set_filename(self, filename):
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
416 self.__curfile = filename
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
417 self.__freshmodule = 1
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
418
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
419 def write(self, fp):
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
420 options = self.__options
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
421 timestamp = time.strftime('%Y-%m-%d %H:%M+%Z')
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
422 # The time stamp in the header doesn't have the same format as that
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
423 # generated by xgettext...
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
424 print(pot_header % {'time': timestamp, 'version':
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
425 __version__}, file=fp)
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
426 # Sort the entries. First sort each particular entry's keys, then
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
427 # sort all the entries by their first item.
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
428 reverse = {}
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
429 for k, v in self.__messages.items():
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
430 keys = v.keys()
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
431 keys = sorted(keys)
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
432 reverse.setdefault(tuple(keys), []).append((k, v))
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
433 rkeys = reverse.keys()
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
434 for rkey in sorted(rkeys):
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
435 rentries = reverse[rkey]
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
436 rentries.sort()
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
437 for k, v in rentries:
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
438 isdocstring = 0
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
439 # If the entry was gleaned out of a docstring, then add a
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
440 # comment stating so. This is to aid translators who may wish
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
441 # to skip translating some unimportant docstrings.
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
442 if reduce(operator.__add__, v.values()):
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
443 isdocstring = 1
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
444 # k is the message string, v is a dictionary-set of (filename,
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
445 # lineno) tuples. We want to sort the entries in v first by
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
446 # file name and then by line number.
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
447 v = v.keys()
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
448 v = sorted(v)
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
449 if not options.writelocations:
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
450 pass
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
451 # location comments are different b/w Solaris and GNU:
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
452 elif options.locationstyle == options.SOLARIS:
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
453 for filename, lineno in v:
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
454 d = {'filename': filename, 'lineno': lineno}
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
455 print(_(
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
456 '# File: %(filename)s, line: %(lineno)d') % d, file=fp)
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
457 elif options.locationstyle == options.GNU:
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
458 # fit as many locations on one line, as long as the
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
459 # resulting line length doesn't exceed 'options.width'
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
460 locline = '#:'
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
461 for filename, lineno in v:
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
462 d = {'filename': filename, 'lineno': lineno}
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
463 s = _(' %(filename)s:%(lineno)d') % d
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
464 if len(locline) + len(s) <= options.width:
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
465 locline = locline + s
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
466 else:
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
467 print(locline, file=fp)
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
468 locline = "#:" + s
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
469 if len(locline) > 2:
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
470 print(locline, file=fp)
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
471 if isdocstring:
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
472 print('#, docstring', file=fp)
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
473 print('msgid', normalize(k), file=fp)
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
474 print('msgstr ""\n', file=fp)
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
475
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
476
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
477 def main():
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
478 global default_keywords
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
479 try:
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
480 opts, args = getopt.getopt(
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
481 sys.argv[1:],
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
482 'ad:DEhk:Kno:p:S:Vvw:x:X:',
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
483 ['extract-all', 'default-domain=', 'escape', 'help',
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
484 'keyword=', 'no-default-keywords',
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
485 'add-location', 'no-location', 'output=', 'output-dir=',
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
486 'style=', 'verbose', 'version', 'width=', 'exclude-file=',
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
487 'docstrings', 'no-docstrings',
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
488 ])
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
489 except getopt.error as msg:
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
490 usage(1, msg)
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
491
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
492 # for holding option values
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
493 class Options:
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
494 # constants
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
495 GNU = 1
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
496 SOLARIS = 2
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
497 # defaults
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
498 extractall = 0 # FIXME: currently this option has no effect at all.
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
499 escape = 0
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
500 keywords = []
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
501 outpath = ''
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
502 outfile = 'messages.pot'
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
503 writelocations = 1
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
504 locationstyle = GNU
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
505 verbose = 0
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
506 width = 78
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
507 excludefilename = ''
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
508 docstrings = 0
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
509 nodocstrings = {}
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
510
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
511 options = Options()
8083
60e92a540ca7 chore: some ruff linter cleanups.
John Rouillard <rouilj@ieee.org>
parents: 8082
diff changeset
512 locations = {'gnu': options.GNU,
60e92a540ca7 chore: some ruff linter cleanups.
John Rouillard <rouilj@ieee.org>
parents: 8082
diff changeset
513 'solaris': options.SOLARIS,
8080
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
514 }
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
515
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
516 # parse options
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
517 for opt, arg in opts:
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
518 if opt in ('-h', '--help'):
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
519 usage(0)
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
520 elif opt in ('-a', '--extract-all'):
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
521 options.extractall = 1
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
522 elif opt in ('-d', '--default-domain'):
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
523 options.outfile = arg + '.pot'
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
524 elif opt in ('-E', '--escape'):
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
525 options.escape = 1
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
526 elif opt in ('-D', '--docstrings'):
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
527 options.docstrings = 1
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
528 elif opt in ('-k', '--keyword'):
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
529 options.keywords.append(arg)
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
530 elif opt in ('-K', '--no-default-keywords'):
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
531 default_keywords = []
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
532 elif opt in ('-n', '--add-location'):
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
533 options.writelocations = 1
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
534 elif opt in ('--no-location',):
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
535 options.writelocations = 0
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
536 elif opt in ('-S', '--style'):
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
537 options.locationstyle = locations.get(arg.lower())
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
538 if options.locationstyle is None:
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
539 usage(1, _('Invalid value for --style: %s') % arg)
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
540 elif opt in ('-o', '--output'):
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
541 options.outfile = arg
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
542 elif opt in ('-p', '--output-dir'):
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
543 options.outpath = arg
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
544 elif opt in ('-v', '--verbose'):
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
545 options.verbose = 1
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
546 elif opt in ('-V', '--version'):
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
547 print(_('pygettext.py (xgettext for Python) %s') % __version__)
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
548 sys.exit(0)
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
549 elif opt in ('-w', '--width'):
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
550 try:
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
551 options.width = int(arg)
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
552 except ValueError:
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
553 usage(1, _('--width argument must be an integer: %s') % arg)
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
554 elif opt in ('-x', '--exclude-file'):
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
555 options.excludefilename = arg
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
556 elif opt in ('-X', '--no-docstrings'):
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
557 fp = open(arg)
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
558 try:
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
559 while 1:
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
560 line = fp.readline()
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
561 if not line:
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
562 break
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
563 options.nodocstrings[line[:-1]] = 1
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
564 finally:
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
565 fp.close()
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
566
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
567 # calculate escapes
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
568 make_escapes(not options.escape)
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
569
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
570 # calculate all keywords
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
571 options.keywords.extend(default_keywords)
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
572
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
573 # initialize list of strings to exclude
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
574 if options.excludefilename:
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
575 try:
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
576 fp = open(options.excludefilename)
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
577 options.toexclude = fp.readlines()
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
578 fp.close()
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
579 except IOError:
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
580 print(_(
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
581 "Can't read --exclude-file: %s") % options.excludefilename, file=sys.stderr)
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
582 sys.exit(1)
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
583 else:
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
584 options.toexclude = []
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
585
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
586 # resolve args to module lists
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
587 expanded = []
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
588 for arg in args:
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
589 if arg == '-':
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
590 expanded.append(arg)
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
591 else:
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
592 expanded.extend(getFilesForName(arg))
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
593 args = expanded
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
594
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
595 # slurp through all the files
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
596 eater = TokenEater(options)
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
597 for filename in args:
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
598 if filename == '-':
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
599 if options.verbose:
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
600 print(_('Reading standard input'))
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
601 fp = sys.stdin
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
602 closep = 0
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
603 else:
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
604 if options.verbose:
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
605 print(_('Working on %s') % filename)
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
606 fp = open(filename)
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
607 closep = 1
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
608 try:
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
609 eater.set_filename(filename)
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
610 try:
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
611 for token in tokenize.generate_tokens(fp.readline):
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
612 eater(*token)
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
613 except tokenize.TokenError as e:
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
614 print('%s: %s, line %d, column %d' % (
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
615 e[0], filename, e[1][0], e[1][1]), file=sys.stderr)
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
616 finally:
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
617 if closep:
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
618 fp.close()
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
619
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
620 # write the output
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
621 if options.outfile == '-':
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
622 fp = sys.stdout
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
623 closep = 0
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
624 else:
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
625 if options.outpath:
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
626 options.outfile = os.path.join(options.outpath, options.outfile)
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
627 fp = open(options.outfile, 'w')
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
628 closep = 1
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
629 try:
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
630 eater.write(fp)
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
631 finally:
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
632 if closep:
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
633 fp.close()
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
634
8083
60e92a540ca7 chore: some ruff linter cleanups.
John Rouillard <rouilj@ieee.org>
parents: 8082
diff changeset
635
8080
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
636 if __name__ == '__main__':
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
637 main()
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
638 # some more test strings
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
639 _(u'a unicode string')
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
640 # this one creates a warning
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
641 _('*** Seen unexpected token "%(token)s"') % {'token': 'test'}
d1c29284ccd9 feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
642 _('more' 'than' 'one' 'string')

Roundup Issue Tracker: http://roundup-tracker.org/