Mercurial > p > roundup > code
annotate roundup/pygettext.py @ 8082:a4127d7afaa9
fix: remove references to imp in untouched code
CI runs flake8 to check for syntax errors and it threw errors on old
imp.XYZ code that was not actually called.
It looks like the code was only called when resolving modules like
ssl, _ssl, importlib, importlib.resources .. rather than
files/directory paths. In Roundup's use case it only runs over
files/directories.
| author | John Rouillard <rouilj@ieee.org> |
|---|---|
| date | Sat, 13 Jul 2024 21:15:50 -0400 |
| parents | d1c29284ccd9 |
| children | 60e92a540ca7 |
| 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 """ |
|
8082
a4127d7afaa9
fix: remove references to imp in untouched code
John Rouillard <rouilj@ieee.org>
parents:
8080
diff
changeset
|
272 pathname = None |
|
a4127d7afaa9
fix: remove references to imp in untouched code
John Rouillard <rouilj@ieee.org>
parents:
8080
diff
changeset
|
273 r = importlib.util.find_spec(dotted_name, pathlist) |
|
8080
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
274 |
|
8082
a4127d7afaa9
fix: remove references to imp in untouched code
John Rouillard <rouilj@ieee.org>
parents:
8080
diff
changeset
|
275 if r.loader.is_package(dotted_name): |
|
a4127d7afaa9
fix: remove references to imp in untouched code
John Rouillard <rouilj@ieee.org>
parents:
8080
diff
changeset
|
276 pathname = r.submodule_search_locations[0] |
|
a4127d7afaa9
fix: remove references to imp in untouched code
John Rouillard <rouilj@ieee.org>
parents:
8080
diff
changeset
|
277 elif issubclass(r.loader.__class__,(importlib.abc.SourceLoader)): |
|
a4127d7afaa9
fix: remove references to imp in untouched code
John Rouillard <rouilj@ieee.org>
parents:
8080
diff
changeset
|
278 pathname = r.origin |
|
8080
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
279 return pathname |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
280 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
281 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
282 def getFilesForName(name): |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
283 """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
|
284 or a directory. |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
285 """ |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
286 if not os.path.exists(name): |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
287 # check for glob chars |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
288 if containsAny(name, "*?[]"): |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
289 files = glob.glob(name) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
290 list = [] |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
291 for file in files: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
292 list.extend(getFilesForName(file)) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
293 return list |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
294 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
295 # try to find module or package |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
296 name = _get_modpkg_path(name) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
297 if not name: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
298 return [] |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
299 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
300 if os.path.isdir(name): |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
301 # find all python files in directory |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
302 list = [] |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
303 # get extension for python source files |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
304 if '_py_ext' not in globals(): |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
305 global _py_ext |
|
8082
a4127d7afaa9
fix: remove references to imp in untouched code
John Rouillard <rouilj@ieee.org>
parents:
8080
diff
changeset
|
306 _py_ext = importlib.machinery.SOURCE_SUFFIXES |
|
8080
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
307 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
|
308 # don't recurse into CVS directories |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
309 if 'CVS' in dirs: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
310 dirs.remove('CVS') |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
311 # add all *.py files to list |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
312 list.extend( |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
313 [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
|
314 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
|
315 ) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
316 return list |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
317 elif os.path.exists(name): |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
318 # a single file |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
319 return [name] |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
320 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
321 return [] |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
322 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
323 class TokenEater: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
324 def __init__(self, options): |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
325 self.__options = options |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
326 self.__messages = {} |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
327 self.__state = self.__waiting |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
328 self.__data = [] |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
329 self.__lineno = -1 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
330 self.__freshmodule = 1 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
331 self.__curfile = None |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
332 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
333 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
|
334 # dispatch |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
335 ## import token |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
336 ## print(('ttype:', token.tok_name[ttype], \ |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
337 ## 'tstring:', tstring), file=sys.stderr) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
338 self.__state(ttype, tstring, stup[0]) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
339 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
340 def __waiting(self, ttype, tstring, lineno): |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
341 opts = self.__options |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
342 # Do docstring extractions, if enabled |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
343 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
|
344 # module docstring? |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
345 if self.__freshmodule: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
346 if ttype == tokenize.STRING: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
347 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
|
348 self.__freshmodule = 0 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
349 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
|
350 self.__freshmodule = 0 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
351 return |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
352 # class docstring? |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
353 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
|
354 self.__state = self.__suiteseen |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
355 return |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
356 if ttype == tokenize.NAME and tstring in opts.keywords: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
357 self.__state = self.__keywordseen |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
358 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
359 def __suiteseen(self, ttype, tstring, lineno): |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
360 # 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
|
361 if ttype == tokenize.OP and tstring == ':': |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
362 self.__state = self.__suitedocstring |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
363 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
364 def __suitedocstring(self, ttype, tstring, lineno): |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
365 # ignore any intervening noise |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
366 if ttype == tokenize.STRING: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
367 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
|
368 self.__state = self.__waiting |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
369 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
|
370 tokenize.COMMENT): |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
371 # there was no class docstring |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
372 self.__state = self.__waiting |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
373 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
374 def __keywordseen(self, ttype, tstring, lineno): |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
375 if ttype == tokenize.OP and tstring == '(': |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
376 self.__data = [] |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
377 self.__lineno = lineno |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
378 self.__state = self.__openseen |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
379 else: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
380 self.__state = self.__waiting |
|
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 __openseen(self, ttype, tstring, lineno): |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
383 if ttype == tokenize.OP and tstring == ')': |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
384 # 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
|
385 # 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
|
386 # 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
|
387 # 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
|
388 if self.__data: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
389 self.__addentry(EMPTYSTRING.join(self.__data)) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
390 self.__state = self.__waiting |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
391 elif ttype == tokenize.STRING: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
392 self.__data.append(safe_eval(tstring)) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
393 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
|
394 token.NEWLINE, tokenize.NL]: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
395 # 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
|
396 print(_( |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
397 '*** %(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
|
398 ) % { |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
399 'token': tstring, |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
400 'file': self.__curfile, |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
401 'lineno': self.__lineno |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
402 }, file=sys.stderr) |
|
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 __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
|
406 if lineno is None: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
407 lineno = self.__lineno |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
408 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
|
409 entry = (self.__curfile, lineno) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
410 self.__messages.setdefault(msg, {})[entry] = isdocstring |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
411 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
412 def set_filename(self, filename): |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
413 self.__curfile = filename |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
414 self.__freshmodule = 1 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
415 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
416 def write(self, fp): |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
417 options = self.__options |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
418 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
|
419 # 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
|
420 # generated by xgettext... |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
421 print(pot_header % {'time': timestamp, 'version': |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
422 __version__}, file=fp) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
423 # 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
|
424 # 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
|
425 reverse = {} |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
426 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
|
427 keys = v.keys() |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
428 keys = sorted(keys) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
429 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
|
430 rkeys = reverse.keys() |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
431 for rkey in sorted(rkeys): |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
432 rentries = reverse[rkey] |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
433 rentries.sort() |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
434 for k, v in rentries: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
435 isdocstring = 0 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
436 # 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
|
437 # 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
|
438 # to skip translating some unimportant docstrings. |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
439 if reduce(operator.__add__, v.values()): |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
440 isdocstring = 1 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
441 # 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
|
442 # 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
|
443 # 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
|
444 v = v.keys() |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
445 v = sorted(v) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
446 if not options.writelocations: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
447 pass |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
448 # 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
|
449 elif options.locationstyle == options.SOLARIS: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
450 for filename, lineno in v: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
451 d = {'filename': filename, 'lineno': lineno} |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
452 print(_( |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
453 '# 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
|
454 elif options.locationstyle == options.GNU: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
455 # 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
|
456 # 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
|
457 locline = '#:' |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
458 for filename, lineno in v: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
459 d = {'filename': filename, 'lineno': lineno} |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
460 s = _(' %(filename)s:%(lineno)d') % d |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
461 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
|
462 locline = locline + s |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
463 else: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
464 print(locline, file=fp) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
465 locline = "#:" + s |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
466 if len(locline) > 2: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
467 print(locline, file=fp) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
468 if isdocstring: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
469 print('#, docstring', file=fp) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
470 print('msgid', normalize(k), file=fp) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
471 print('msgstr ""\n', file=fp) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
472 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
473 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
474 def main(): |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
475 global default_keywords |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
476 try: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
477 opts, args = getopt.getopt( |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
478 sys.argv[1:], |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
479 '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
|
480 ['extract-all', 'default-domain=', 'escape', 'help', |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
481 'keyword=', 'no-default-keywords', |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
482 '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
|
483 'style=', 'verbose', 'version', 'width=', 'exclude-file=', |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
484 'docstrings', 'no-docstrings', |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
485 ]) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
486 except getopt.error as msg: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
487 usage(1, msg) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
488 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
489 # for holding option values |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
490 class Options: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
491 # constants |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
492 GNU = 1 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
493 SOLARIS = 2 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
494 # defaults |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
495 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
|
496 escape = 0 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
497 keywords = [] |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
498 outpath = '' |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
499 outfile = 'messages.pot' |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
500 writelocations = 1 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
501 locationstyle = GNU |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
502 verbose = 0 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
503 width = 78 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
504 excludefilename = '' |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
505 docstrings = 0 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
506 nodocstrings = {} |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
507 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
508 options = Options() |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
509 locations = {'gnu' : options.GNU, |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
510 'solaris' : options.SOLARIS, |
|
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 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
513 # parse options |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
514 for opt, arg in opts: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
515 if opt in ('-h', '--help'): |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
516 usage(0) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
517 elif opt in ('-a', '--extract-all'): |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
518 options.extractall = 1 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
519 elif opt in ('-d', '--default-domain'): |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
520 options.outfile = arg + '.pot' |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
521 elif opt in ('-E', '--escape'): |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
522 options.escape = 1 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
523 elif opt in ('-D', '--docstrings'): |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
524 options.docstrings = 1 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
525 elif opt in ('-k', '--keyword'): |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
526 options.keywords.append(arg) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
527 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
|
528 default_keywords = [] |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
529 elif opt in ('-n', '--add-location'): |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
530 options.writelocations = 1 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
531 elif opt in ('--no-location',): |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
532 options.writelocations = 0 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
533 elif opt in ('-S', '--style'): |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
534 options.locationstyle = locations.get(arg.lower()) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
535 if options.locationstyle is None: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
536 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
|
537 elif opt in ('-o', '--output'): |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
538 options.outfile = arg |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
539 elif opt in ('-p', '--output-dir'): |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
540 options.outpath = arg |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
541 elif opt in ('-v', '--verbose'): |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
542 options.verbose = 1 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
543 elif opt in ('-V', '--version'): |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
544 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
|
545 sys.exit(0) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
546 elif opt in ('-w', '--width'): |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
547 try: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
548 options.width = int(arg) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
549 except ValueError: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
550 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
|
551 elif opt in ('-x', '--exclude-file'): |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
552 options.excludefilename = arg |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
553 elif opt in ('-X', '--no-docstrings'): |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
554 fp = open(arg) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
555 try: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
556 while 1: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
557 line = fp.readline() |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
558 if not line: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
559 break |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
560 options.nodocstrings[line[:-1]] = 1 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
561 finally: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
562 fp.close() |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
563 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
564 # calculate escapes |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
565 make_escapes(not options.escape) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
566 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
567 # calculate all keywords |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
568 options.keywords.extend(default_keywords) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
569 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
570 # initialize list of strings to exclude |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
571 if options.excludefilename: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
572 try: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
573 fp = open(options.excludefilename) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
574 options.toexclude = fp.readlines() |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
575 fp.close() |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
576 except IOError: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
577 print(_( |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
578 "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
|
579 sys.exit(1) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
580 else: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
581 options.toexclude = [] |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
582 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
583 # resolve args to module lists |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
584 expanded = [] |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
585 for arg in args: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
586 if arg == '-': |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
587 expanded.append(arg) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
588 else: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
589 expanded.extend(getFilesForName(arg)) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
590 args = expanded |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
591 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
592 # slurp through all the files |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
593 eater = TokenEater(options) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
594 for filename in args: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
595 if filename == '-': |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
596 if options.verbose: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
597 print(_('Reading standard input')) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
598 fp = sys.stdin |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
599 closep = 0 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
600 else: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
601 if options.verbose: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
602 print(_('Working on %s') % filename) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
603 fp = open(filename) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
604 closep = 1 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
605 try: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
606 eater.set_filename(filename) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
607 try: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
608 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
|
609 eater(*token) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
610 except tokenize.TokenError as e: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
611 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
|
612 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
|
613 finally: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
614 if closep: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
615 fp.close() |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
616 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
617 # write the output |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
618 if options.outfile == '-': |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
619 fp = sys.stdout |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
620 closep = 0 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
621 else: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
622 if options.outpath: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
623 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
|
624 fp = open(options.outfile, 'w') |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
625 closep = 1 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
626 try: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
627 eater.write(fp) |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
628 finally: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
629 if closep: |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
630 fp.close() |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
631 |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
632 if __name__ == '__main__': |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
633 main() |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
634 # some more test strings |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
635 _(u'a unicode string') |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
636 # this one creates a warning |
|
d1c29284ccd9
feat: issue2551287 - roundup-gettext extracts strings from detectors/extensions
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
637 _('*** 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
|
638 _('more' 'than' 'one' 'string') |
