Mercurial > p > roundup > code
annotate roundup/pygettext.py @ 8080:d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
Enhance roundup_gettext.py to extract strings from
detectors/extensions. If the polib module is available,
roundup-gettext will extract translatable strings from the tracker's
Python code. If polib is missing, it will print a warning.
Marcus did most of the work, I had to do a python 2-> conversion of
pygettext.py.
| author | John Rouillard <rouilj@ieee.org> |
|---|---|
| date | Sat, 13 Jul 2024 18:27:11 -0400 |
| parents | |
| children | a4127d7afaa9 |
| 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 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
2 # 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
|
3 # |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
4 # 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
|
5 # 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
|
6 # |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
7 # 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
|
8 # 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
|
9 # 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
|
10 # 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
|
11 # 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
|
12 # 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
|
13 # |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
14 # 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
|
15 # Converted from python 2. |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
16 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
17 from __future__ import print_function |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
18 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
19 # for selftesting |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
20 try: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
21 import fintl |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
22 _ = fintl.gettext |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
23 except ImportError: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
24 _ = lambda s: s |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
25 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
26 __doc__ = _("""pygettext -- Python equivalent of xgettext(1) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
27 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
28 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
|
29 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
|
30 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
|
31 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
|
32 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
33 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
|
34 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
|
35 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
|
36 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
|
37 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
38 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
|
39 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
|
40 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
|
41 used. |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
42 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
43 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
|
44 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
|
45 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
|
46 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
|
47 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
|
48 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
|
49 translatable strings: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
50 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
51 gettext("Translatable String") |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
52 _("Translatable String") |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
53 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
54 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
|
55 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
|
56 below for how to augment this. |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
57 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
58 [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
|
59 [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
|
60 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
61 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
|
62 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
|
63 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
|
64 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
|
65 additional switches. |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
66 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
67 Usage: pygettext [options] inputfile ... |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
68 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
69 Options: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
70 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
71 -a |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
72 --extract-all |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
73 Extract all strings. |
|
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 -d name |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
76 --default-domain=name |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
77 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
|
78 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
79 -E |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
80 --escape |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
81 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
|
82 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
83 -D |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
84 --docstrings |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
85 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
|
86 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
|
87 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
|
88 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
89 -h |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
90 --help |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
91 Print this help message and exit. |
|
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 -k word |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
94 --keyword=word |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
95 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
|
96 %(DEFAULTKEYWORDS)s |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
97 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
98 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
|
99 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
100 -K |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
101 --no-default-keywords |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
102 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
|
103 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
|
104 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
105 --no-location |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
106 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
|
107 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
108 -n |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
109 --add-location |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
110 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
|
111 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
|
112 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
|
113 option. This is the default. |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
114 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
115 -o filename |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
116 --output=filename |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
117 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
|
118 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
|
119 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
120 -p dir |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
121 --output-dir=dir |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
122 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
|
123 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
124 -S stylename |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
125 --style stylename |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
126 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
|
127 supported: |
|
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 Solaris # File: filename, line: line-number |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
130 GNU #: filename:line |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
131 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
132 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
|
133 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
134 -v |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
135 --verbose |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
136 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
|
137 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
138 -V |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
139 --version |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
140 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
|
141 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
142 -w columns |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
143 --width=columns |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
144 Set width of output to columns. |
|
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 -x filename |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
147 --exclude-file=filename |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
148 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
|
149 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
|
150 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
|
151 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
152 -X filename |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
153 --no-docstrings=filename |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
154 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
|
155 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
|
156 conjunction with the -D option above. |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
157 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
158 If `inputfile' is -, standard input is read. |
|
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 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
161 import os |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
162 import importlib |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
163 import sys |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
164 import glob |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
165 import time |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
166 import getopt |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
167 import token |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
168 import tokenize |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
169 import operator |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
170 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
171 from functools import reduce |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
172 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
173 __version__ = '1.5' |
|
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. |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
182 pot_header = _('''\ |
|
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 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
200 ''') |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
201 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
202 def usage(code, msg=''): |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
203 print(__doc__ % globals(), file=sys.stderr) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
204 if msg: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
205 print(msg, file=sys.stderr) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
206 sys.exit(code) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
207 |
|
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 escapes = [] |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
210 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
211 def make_escapes(pass_iso8859): |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
212 global escapes |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
213 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
|
214 if pass_iso8859: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
215 # 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
|
216 # "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
|
217 # 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
|
218 mod = 128 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
219 else: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
220 mod = 256 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
221 for i in range(mod): |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
222 if not(32 <= i <= 126): |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
223 escapes[i] = "\\%03o" % i |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
224 escapes[ord('\\')] = '\\\\' |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
225 escapes[ord('\t')] = '\\t' |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
226 escapes[ord('\r')] = '\\r' |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
227 escapes[ord('\n')] = '\\n' |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
228 escapes[ord('\"')] = '\\"' |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
229 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
230 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
231 def escape(s): |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
232 global escapes |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
233 s = list(s) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
234 for i in range(len(s)): |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
235 s[i] = escapes[ord(s[i])] |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
236 return EMPTYSTRING.join(s) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
237 |
|
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 def safe_eval(s): |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
240 # unwrap quotes, safely |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
241 return eval(s, {'__builtins__':{}}, {}) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
242 |
|
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 def normalize(s): |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
245 # 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
|
246 # 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
|
247 lines = s.split('\n') |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
248 if len(lines) == 1: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
249 s = '"' + escape(s) + '"' |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
250 else: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
251 if not lines[-1]: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
252 del lines[-1] |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
253 lines[-1] = lines[-1] + '\n' |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
254 for i in range(len(lines)): |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
255 lines[i] = escape(lines[i]) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
256 lineterm = '\\n"\n"' |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
257 s = '""\n"' + lineterm.join(lines) + '"' |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
258 return s |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
259 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
260 def containsAny(str, set): |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
261 """Check whether 'str' contains ANY of the chars in 'set'""" |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
262 return 1 in [c in str for c in set] |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
263 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
264 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
265 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
|
266 """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
|
267 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
268 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
|
269 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
|
270 extension module. |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
271 """ |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
272 # split off top-most name |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
273 parts = dotted_name.split('.', 1) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
274 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
275 if len(parts) > 1: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
276 # we have a dotted path, import top-level package |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
277 try: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
278 file, pathname, description = importlib.find_module(parts[0], pathlist) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
279 if file: file.close() |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
280 except ImportError: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
281 return None |
|
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 # check if it's indeed a package |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
284 if description[2] == imp.PKG_DIRECTORY: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
285 # recursively handle the remaining name parts |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
286 pathname = _get_modpkg_path(parts[1], [pathname]) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
287 else: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
288 pathname = None |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
289 else: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
290 # plain name |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
291 try: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
292 file, pathname, description = imp.find_module( |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
293 dotted_name, pathlist) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
294 if file: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
295 file.close() |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
296 if description[2] not in [imp.PY_SOURCE, imp.PKG_DIRECTORY]: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
297 pathname = None |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
298 except ImportError: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
299 pathname = None |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
300 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
301 return pathname |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
302 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
303 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
304 def getFilesForName(name): |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
305 """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
|
306 or a directory. |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
307 """ |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
308 if not os.path.exists(name): |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
309 # check for glob chars |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
310 if containsAny(name, "*?[]"): |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
311 files = glob.glob(name) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
312 list = [] |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
313 for file in files: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
314 list.extend(getFilesForName(file)) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
315 return list |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
316 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
317 # try to find module or package |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
318 name = _get_modpkg_path(name) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
319 if not name: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
320 return [] |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
321 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
322 if os.path.isdir(name): |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
323 # find all python files in directory |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
324 list = [] |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
325 # get extension for python source files |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
326 if '_py_ext' not in globals(): |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
327 global _py_ext |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
328 _py_ext = [triple[0] for triple in imp.get_suffixes() |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
329 if triple[2] == imp.PY_SOURCE][0] |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
330 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
|
331 # don't recurse into CVS directories |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
332 if 'CVS' in dirs: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
333 dirs.remove('CVS') |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
334 # add all *.py files to list |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
335 list.extend( |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
336 [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
|
337 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
|
338 ) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
339 return list |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
340 elif os.path.exists(name): |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
341 # a single file |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
342 return [name] |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
343 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
344 return [] |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
345 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
346 class TokenEater: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
347 def __init__(self, options): |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
348 self.__options = options |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
349 self.__messages = {} |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
350 self.__state = self.__waiting |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
351 self.__data = [] |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
352 self.__lineno = -1 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
353 self.__freshmodule = 1 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
354 self.__curfile = None |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
355 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
356 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
|
357 # dispatch |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
358 ## import token |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
359 ## print(('ttype:', token.tok_name[ttype], \ |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
360 ## 'tstring:', tstring), file=sys.stderr) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
361 self.__state(ttype, tstring, stup[0]) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
362 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
363 def __waiting(self, ttype, tstring, lineno): |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
364 opts = self.__options |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
365 # Do docstring extractions, if enabled |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
366 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
|
367 # module docstring? |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
368 if self.__freshmodule: |
|
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.__freshmodule = 0 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
372 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
|
373 self.__freshmodule = 0 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
374 return |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
375 # class docstring? |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
376 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
|
377 self.__state = self.__suiteseen |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
378 return |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
379 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
|
380 self.__state = self.__keywordseen |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
381 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
382 def __suiteseen(self, ttype, tstring, lineno): |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
383 # 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
|
384 if ttype == tokenize.OP and tstring == ':': |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
385 self.__state = self.__suitedocstring |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
386 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
387 def __suitedocstring(self, ttype, tstring, lineno): |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
388 # ignore any intervening noise |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
389 if ttype == tokenize.STRING: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
390 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
|
391 self.__state = self.__waiting |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
392 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
|
393 tokenize.COMMENT): |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
394 # there was no class docstring |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
395 self.__state = self.__waiting |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
396 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
397 def __keywordseen(self, ttype, tstring, lineno): |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
398 if ttype == tokenize.OP and tstring == '(': |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
399 self.__data = [] |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
400 self.__lineno = lineno |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
401 self.__state = self.__openseen |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
402 else: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
403 self.__state = self.__waiting |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
404 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
405 def __openseen(self, ttype, tstring, lineno): |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
406 if ttype == tokenize.OP and tstring == ')': |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
407 # 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
|
408 # 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
|
409 # 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
|
410 # 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
|
411 if self.__data: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
412 self.__addentry(EMPTYSTRING.join(self.__data)) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
413 self.__state = self.__waiting |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
414 elif ttype == tokenize.STRING: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
415 self.__data.append(safe_eval(tstring)) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
416 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
|
417 token.NEWLINE, tokenize.NL]: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
418 # 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
|
419 print(_( |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
420 '*** %(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
|
421 ) % { |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
422 'token': tstring, |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
423 'file': self.__curfile, |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
424 'lineno': self.__lineno |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
425 }, file=sys.stderr) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
426 self.__state = self.__waiting |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
427 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
428 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
|
429 if lineno is None: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
430 lineno = self.__lineno |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
431 if not msg in self.__options.toexclude: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
432 entry = (self.__curfile, lineno) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
433 self.__messages.setdefault(msg, {})[entry] = isdocstring |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
434 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
435 def set_filename(self, filename): |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
436 self.__curfile = filename |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
437 self.__freshmodule = 1 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
438 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
439 def write(self, fp): |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
440 options = self.__options |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
441 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
|
442 # 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
|
443 # generated by xgettext... |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
444 print(pot_header % {'time': timestamp, 'version': |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
445 __version__}, file=fp) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
446 # 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
|
447 # 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
|
448 reverse = {} |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
449 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
|
450 keys = v.keys() |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
451 keys = sorted(keys) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
452 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
|
453 rkeys = reverse.keys() |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
454 for rkey in sorted(rkeys): |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
455 rentries = reverse[rkey] |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
456 rentries.sort() |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
457 for k, v in rentries: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
458 isdocstring = 0 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
459 # 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
|
460 # 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
|
461 # to skip translating some unimportant docstrings. |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
462 if reduce(operator.__add__, v.values()): |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
463 isdocstring = 1 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
464 # 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
|
465 # 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
|
466 # 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
|
467 v = v.keys() |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
468 v = sorted(v) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
469 if not options.writelocations: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
470 pass |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
471 # 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
|
472 elif options.locationstyle == options.SOLARIS: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
473 for filename, lineno in v: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
474 d = {'filename': filename, 'lineno': lineno} |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
475 print(_( |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
476 '# 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
|
477 elif options.locationstyle == options.GNU: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
478 # 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
|
479 # 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
|
480 locline = '#:' |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
481 for filename, lineno in v: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
482 d = {'filename': filename, 'lineno': lineno} |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
483 s = _(' %(filename)s:%(lineno)d') % d |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
484 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
|
485 locline = locline + s |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
486 else: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
487 print(locline, file=fp) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
488 locline = "#:" + s |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
489 if len(locline) > 2: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
490 print(locline, file=fp) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
491 if isdocstring: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
492 print('#, docstring', file=fp) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
493 print('msgid', normalize(k), file=fp) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
494 print('msgstr ""\n', file=fp) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
495 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
496 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
497 def main(): |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
498 global default_keywords |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
499 try: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
500 opts, args = getopt.getopt( |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
501 sys.argv[1:], |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
502 '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
|
503 ['extract-all', 'default-domain=', 'escape', 'help', |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
504 'keyword=', 'no-default-keywords', |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
505 '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
|
506 'style=', 'verbose', 'version', 'width=', 'exclude-file=', |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
507 'docstrings', 'no-docstrings', |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
508 ]) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
509 except getopt.error as msg: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
510 usage(1, msg) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
511 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
512 # for holding option values |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
513 class Options: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
514 # constants |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
515 GNU = 1 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
516 SOLARIS = 2 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
517 # defaults |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
518 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
|
519 escape = 0 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
520 keywords = [] |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
521 outpath = '' |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
522 outfile = 'messages.pot' |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
523 writelocations = 1 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
524 locationstyle = GNU |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
525 verbose = 0 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
526 width = 78 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
527 excludefilename = '' |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
528 docstrings = 0 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
529 nodocstrings = {} |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
530 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
531 options = Options() |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
532 locations = {'gnu' : options.GNU, |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
533 'solaris' : options.SOLARIS, |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
534 } |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
535 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
536 # parse options |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
537 for opt, arg in opts: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
538 if opt in ('-h', '--help'): |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
539 usage(0) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
540 elif opt in ('-a', '--extract-all'): |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
541 options.extractall = 1 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
542 elif opt in ('-d', '--default-domain'): |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
543 options.outfile = arg + '.pot' |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
544 elif opt in ('-E', '--escape'): |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
545 options.escape = 1 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
546 elif opt in ('-D', '--docstrings'): |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
547 options.docstrings = 1 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
548 elif opt in ('-k', '--keyword'): |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
549 options.keywords.append(arg) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
550 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
|
551 default_keywords = [] |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
552 elif opt in ('-n', '--add-location'): |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
553 options.writelocations = 1 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
554 elif opt in ('--no-location',): |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
555 options.writelocations = 0 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
556 elif opt in ('-S', '--style'): |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
557 options.locationstyle = locations.get(arg.lower()) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
558 if options.locationstyle is None: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
559 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
|
560 elif opt in ('-o', '--output'): |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
561 options.outfile = arg |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
562 elif opt in ('-p', '--output-dir'): |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
563 options.outpath = arg |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
564 elif opt in ('-v', '--verbose'): |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
565 options.verbose = 1 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
566 elif opt in ('-V', '--version'): |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
567 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
|
568 sys.exit(0) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
569 elif opt in ('-w', '--width'): |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
570 try: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
571 options.width = int(arg) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
572 except ValueError: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
573 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
|
574 elif opt in ('-x', '--exclude-file'): |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
575 options.excludefilename = arg |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
576 elif opt in ('-X', '--no-docstrings'): |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
577 fp = open(arg) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
578 try: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
579 while 1: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
580 line = fp.readline() |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
581 if not line: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
582 break |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
583 options.nodocstrings[line[:-1]] = 1 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
584 finally: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
585 fp.close() |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
586 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
587 # calculate escapes |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
588 make_escapes(not options.escape) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
589 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
590 # calculate all keywords |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
591 options.keywords.extend(default_keywords) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
592 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
593 # initialize list of strings to exclude |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
594 if options.excludefilename: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
595 try: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
596 fp = open(options.excludefilename) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
597 options.toexclude = fp.readlines() |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
598 fp.close() |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
599 except IOError: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
600 print(_( |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
601 "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
|
602 sys.exit(1) |
|
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 options.toexclude = [] |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
605 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
606 # resolve args to module lists |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
607 expanded = [] |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
608 for arg in args: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
609 if arg == '-': |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
610 expanded.append(arg) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
611 else: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
612 expanded.extend(getFilesForName(arg)) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
613 args = expanded |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
614 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
615 # slurp through all the files |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
616 eater = TokenEater(options) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
617 for filename in args: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
618 if filename == '-': |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
619 if options.verbose: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
620 print(_('Reading standard input')) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
621 fp = sys.stdin |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
622 closep = 0 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
623 else: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
624 if options.verbose: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
625 print(_('Working on %s') % filename) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
626 fp = open(filename) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
627 closep = 1 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
628 try: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
629 eater.set_filename(filename) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
630 try: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
631 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
|
632 eater(*token) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
633 except tokenize.TokenError as e: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
634 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
|
635 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
|
636 finally: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
637 if closep: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
638 fp.close() |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
639 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
640 # write the output |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
641 if options.outfile == '-': |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
642 fp = sys.stdout |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
643 closep = 0 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
644 else: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
645 if options.outpath: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
646 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
|
647 fp = open(options.outfile, 'w') |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
648 closep = 1 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
649 try: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
650 eater.write(fp) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
651 finally: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
652 if closep: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
653 fp.close() |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
654 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
655 if __name__ == '__main__': |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
656 main() |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
657 # some more test strings |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
658 _(u'a unicode string') |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
659 # this one creates a warning |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
660 _('*** 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
|
661 _('more' 'than' 'one' 'string') |
