Barry Warsaw | af57251 | 1999-08-11 21:40:38 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
Barry Warsaw | 63ce5af | 2001-07-27 16:47:18 +0000 | [diff] [blame] | 2 | # Originally written by Barry Warsaw <barry@zope.com> |
Barry Warsaw | c8f0892 | 2000-02-26 20:56:47 +0000 | [diff] [blame] | 3 | # |
Barry Warsaw | 6e97241 | 2001-05-21 19:35:20 +0000 | [diff] [blame] | 4 | # Minimally patched to make it even more xgettext compatible |
Barry Warsaw | c8f0892 | 2000-02-26 20:56:47 +0000 | [diff] [blame] | 5 | # by Peter Funk <pf@artcom-gmbh.de> |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 6 | |
Barry Warsaw | 08a8a35 | 2000-10-27 04:56:28 +0000 | [diff] [blame] | 7 | """pygettext -- Python equivalent of xgettext(1) |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 8 | |
| 9 | Many systems (Solaris, Linux, Gnu) provide extensive tools that ease the |
| 10 | internationalization of C programs. Most of these tools are independent of |
| 11 | the programming language and can be used from within Python programs. Martin |
| 12 | von Loewis' work[1] helps considerably in this regard. |
| 13 | |
Barry Warsaw | 5dbf526 | 1999-11-03 18:47:52 +0000 | [diff] [blame] | 14 | There's one problem though; xgettext is the program that scans source code |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 15 | looking for message strings, but it groks only C (or C++). Python introduces |
| 16 | a few wrinkles, such as dual quoting characters, triple quoted strings, and |
| 17 | raw strings. xgettext understands none of this. |
| 18 | |
| 19 | Enter pygettext, which uses Python's standard tokenize module to scan Python |
| 20 | source code, generating .pot files identical to what GNU xgettext[2] generates |
Barry Warsaw | 5dbf526 | 1999-11-03 18:47:52 +0000 | [diff] [blame] | 21 | for C and C++ code. From there, the standard GNU tools can be used. |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 22 | |
| 23 | A word about marking Python strings as candidates for translation. GNU |
| 24 | xgettext recognizes the following keywords: gettext, dgettext, dcgettext, and |
| 25 | gettext_noop. But those can be a lot of text to include all over your code. |
Barry Warsaw | 5dbf526 | 1999-11-03 18:47:52 +0000 | [diff] [blame] | 26 | C and C++ have a trick: they use the C preprocessor. Most internationalized C |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 27 | source includes a #define for gettext() to _() so that what has to be written |
| 28 | in the source is much less. Thus these are both translatable strings: |
| 29 | |
| 30 | gettext("Translatable String") |
| 31 | _("Translatable String") |
| 32 | |
| 33 | Python of course has no preprocessor so this doesn't work so well. Thus, |
| 34 | pygettext searches only for _() by default, but see the -k/--keyword flag |
| 35 | below for how to augment this. |
| 36 | |
| 37 | [1] http://www.python.org/workshops/1997-10/proceedings/loewis.html |
| 38 | [2] http://www.gnu.org/software/gettext/gettext.html |
| 39 | |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 40 | NOTE: pygettext attempts to be option and feature compatible with GNU xgettext |
Barry Warsaw | c8f0892 | 2000-02-26 20:56:47 +0000 | [diff] [blame] | 41 | where ever possible. However some options are still missing or are not fully |
Barry Warsaw | a17e0f1 | 2000-03-08 15:18:35 +0000 | [diff] [blame] | 42 | implemented. Also, xgettext's use of command line switches with option |
| 43 | arguments is broken, and in these cases, pygettext just defines additional |
| 44 | switches. |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 45 | |
Barry Warsaw | a17e0f1 | 2000-03-08 15:18:35 +0000 | [diff] [blame] | 46 | Usage: pygettext [options] inputfile ... |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 47 | |
| 48 | Options: |
| 49 | |
| 50 | -a |
| 51 | --extract-all |
Barry Warsaw | 63ce5af | 2001-07-27 16:47:18 +0000 | [diff] [blame] | 52 | Extract all strings. |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 53 | |
Barry Warsaw | c8f0892 | 2000-02-26 20:56:47 +0000 | [diff] [blame] | 54 | -d name |
| 55 | --default-domain=name |
Barry Warsaw | 63ce5af | 2001-07-27 16:47:18 +0000 | [diff] [blame] | 56 | Rename the default output file from messages.pot to name.pot. |
Barry Warsaw | c8f0892 | 2000-02-26 20:56:47 +0000 | [diff] [blame] | 57 | |
| 58 | -E |
| 59 | --escape |
Barry Warsaw | 08a8a35 | 2000-10-27 04:56:28 +0000 | [diff] [blame] | 60 | Replace non-ASCII characters with octal escape sequences. |
| 61 | |
| 62 | -D |
| 63 | --docstrings |
| 64 | Extract module, class, method, and function docstrings. These do not |
| 65 | need to be wrapped in _() markers, and in fact cannot be for Python to |
Barry Warsaw | 63ce5af | 2001-07-27 16:47:18 +0000 | [diff] [blame] | 66 | consider them docstrings. (See also the -X option). |
Barry Warsaw | c8f0892 | 2000-02-26 20:56:47 +0000 | [diff] [blame] | 67 | |
| 68 | -h |
| 69 | --help |
Barry Warsaw | 63ce5af | 2001-07-27 16:47:18 +0000 | [diff] [blame] | 70 | Print this help message and exit. |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 71 | |
Barry Warsaw | a17e0f1 | 2000-03-08 15:18:35 +0000 | [diff] [blame] | 72 | -k word |
| 73 | --keyword=word |
| 74 | Keywords to look for in addition to the default set, which are: |
| 75 | %(DEFAULTKEYWORDS)s |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 76 | |
Barry Warsaw | a17e0f1 | 2000-03-08 15:18:35 +0000 | [diff] [blame] | 77 | You can have multiple -k flags on the command line. |
| 78 | |
| 79 | -K |
| 80 | --no-default-keywords |
| 81 | Disable the default set of keywords (see above). Any keywords |
| 82 | explicitly added with the -k/--keyword option are still recognized. |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 83 | |
| 84 | --no-location |
Barry Warsaw | a17e0f1 | 2000-03-08 15:18:35 +0000 | [diff] [blame] | 85 | Do not write filename/lineno location comments. |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 86 | |
Barry Warsaw | a17e0f1 | 2000-03-08 15:18:35 +0000 | [diff] [blame] | 87 | -n |
| 88 | --add-location |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 89 | Write filename/lineno location comments indicating where each |
| 90 | extracted string is found in the source. These lines appear before |
Barry Warsaw | a17e0f1 | 2000-03-08 15:18:35 +0000 | [diff] [blame] | 91 | each msgid. The style of comments is controlled by the -S/--style |
| 92 | option. This is the default. |
| 93 | |
Barry Warsaw | 08a8a35 | 2000-10-27 04:56:28 +0000 | [diff] [blame] | 94 | -o filename |
| 95 | --output=filename |
| 96 | Rename the default output file from messages.pot to filename. If |
| 97 | filename is `-' then the output is sent to standard out. |
| 98 | |
| 99 | -p dir |
| 100 | --output-dir=dir |
| 101 | Output files will be placed in directory dir. |
| 102 | |
Barry Warsaw | a17e0f1 | 2000-03-08 15:18:35 +0000 | [diff] [blame] | 103 | -S stylename |
| 104 | --style stylename |
| 105 | Specify which style to use for location comments. Two styles are |
| 106 | supported: |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 107 | |
| 108 | Solaris # File: filename, line: line-number |
Barry Warsaw | a17e0f1 | 2000-03-08 15:18:35 +0000 | [diff] [blame] | 109 | GNU #: filename:line |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 110 | |
Barry Warsaw | a17e0f1 | 2000-03-08 15:18:35 +0000 | [diff] [blame] | 111 | The style name is case insensitive. GNU style is the default. |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 112 | |
Barry Warsaw | 5dbf526 | 1999-11-03 18:47:52 +0000 | [diff] [blame] | 113 | -v |
| 114 | --verbose |
| 115 | Print the names of the files being processed. |
| 116 | |
Barry Warsaw | c8f0892 | 2000-02-26 20:56:47 +0000 | [diff] [blame] | 117 | -V |
| 118 | --version |
| 119 | Print the version of pygettext and exit. |
| 120 | |
| 121 | -w columns |
| 122 | --width=columns |
| 123 | Set width of output to columns. |
| 124 | |
| 125 | -x filename |
| 126 | --exclude-file=filename |
| 127 | Specify a file that contains a list of strings that are not be |
| 128 | extracted from the input files. Each string to be excluded must |
| 129 | appear on a line by itself in the file. |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 130 | |
Barry Warsaw | 63ce5af | 2001-07-27 16:47:18 +0000 | [diff] [blame] | 131 | -X filename |
| 132 | --no-docstrings=filename |
| 133 | Specify a file that contains a list of files (one per line) that |
| 134 | should not have their docstrings extracted. This is only useful in |
| 135 | conjunction with the -D option above. |
Barry Warsaw | a17e0f1 | 2000-03-08 15:18:35 +0000 | [diff] [blame] | 136 | |
Barry Warsaw | 63ce5af | 2001-07-27 16:47:18 +0000 | [diff] [blame] | 137 | If `inputfile' is -, standard input is read. |
Barry Warsaw | 08a8a35 | 2000-10-27 04:56:28 +0000 | [diff] [blame] | 138 | """ |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 139 | |
| 140 | import os |
| 141 | import sys |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 142 | import time |
| 143 | import getopt |
| 144 | import tokenize |
Barry Warsaw | 16b62c1 | 2001-05-21 19:51:26 +0000 | [diff] [blame] | 145 | import operator |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 146 | |
Barry Warsaw | 08a8a35 | 2000-10-27 04:56:28 +0000 | [diff] [blame] | 147 | # for selftesting |
| 148 | try: |
| 149 | import fintl |
| 150 | _ = fintl.gettext |
| 151 | except ImportError: |
| 152 | def _(s): return s |
| 153 | |
Barry Warsaw | 63ce5af | 2001-07-27 16:47:18 +0000 | [diff] [blame] | 154 | __version__ = '1.4' |
Barry Warsaw | a17e0f1 | 2000-03-08 15:18:35 +0000 | [diff] [blame] | 155 | |
| 156 | default_keywords = ['_'] |
| 157 | DEFAULTKEYWORDS = ', '.join(default_keywords) |
| 158 | |
| 159 | EMPTYSTRING = '' |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 160 | |
| 161 | |
| 162 | |
Barry Warsaw | 63ce5af | 2001-07-27 16:47:18 +0000 | [diff] [blame] | 163 | # The normal pot-file header. msgmerge and Emacs's po-mode work better if it's |
| 164 | # there. |
Barry Warsaw | 5dbf526 | 1999-11-03 18:47:52 +0000 | [diff] [blame] | 165 | pot_header = _('''\ |
| 166 | # SOME DESCRIPTIVE TITLE. |
| 167 | # Copyright (C) YEAR ORGANIZATION |
| 168 | # FIRST AUTHOR <EMAIL@ADDRESS>, YEAR. |
| 169 | # |
| 170 | msgid "" |
| 171 | msgstr "" |
| 172 | "Project-Id-Version: PACKAGE VERSION\\n" |
Martin v. Löwis | 0f6b383 | 2001-03-01 22:56:17 +0000 | [diff] [blame] | 173 | "POT-Creation-Date: %(time)s\\n" |
| 174 | "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\\n" |
Barry Warsaw | 5dbf526 | 1999-11-03 18:47:52 +0000 | [diff] [blame] | 175 | "Last-Translator: FULL NAME <EMAIL@ADDRESS>\\n" |
| 176 | "Language-Team: LANGUAGE <LL@li.org>\\n" |
| 177 | "MIME-Version: 1.0\\n" |
| 178 | "Content-Type: text/plain; charset=CHARSET\\n" |
| 179 | "Content-Transfer-Encoding: ENCODING\\n" |
| 180 | "Generated-By: pygettext.py %(version)s\\n" |
| 181 | |
| 182 | ''') |
| 183 | |
| 184 | |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 185 | def usage(code, msg=''): |
Barry Warsaw | 08a8a35 | 2000-10-27 04:56:28 +0000 | [diff] [blame] | 186 | print >> sys.stderr, _(__doc__) % globals() |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 187 | if msg: |
Barry Warsaw | 08a8a35 | 2000-10-27 04:56:28 +0000 | [diff] [blame] | 188 | print >> sys.stderr, msg |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 189 | sys.exit(code) |
| 190 | |
Barry Warsaw | c8f0892 | 2000-02-26 20:56:47 +0000 | [diff] [blame] | 191 | |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 192 | |
Barry Warsaw | 5dbf526 | 1999-11-03 18:47:52 +0000 | [diff] [blame] | 193 | escapes = [] |
Barry Warsaw | 5dbf526 | 1999-11-03 18:47:52 +0000 | [diff] [blame] | 194 | |
Barry Warsaw | c8f0892 | 2000-02-26 20:56:47 +0000 | [diff] [blame] | 195 | def make_escapes(pass_iso8859): |
| 196 | global escapes |
Barry Warsaw | 7733e12 | 2000-02-27 14:30:48 +0000 | [diff] [blame] | 197 | if pass_iso8859: |
| 198 | # Allow iso-8859 characters to pass through so that e.g. 'msgid |
| 199 | # "Höhe"' would result not result in 'msgid "H\366he"'. Otherwise we |
| 200 | # escape any character outside the 32..126 range. |
| 201 | mod = 128 |
| 202 | else: |
| 203 | mod = 256 |
Barry Warsaw | c8f0892 | 2000-02-26 20:56:47 +0000 | [diff] [blame] | 204 | for i in range(256): |
Barry Warsaw | 7733e12 | 2000-02-27 14:30:48 +0000 | [diff] [blame] | 205 | if 32 <= (i % mod) <= 126: |
Barry Warsaw | c8f0892 | 2000-02-26 20:56:47 +0000 | [diff] [blame] | 206 | escapes.append(chr(i)) |
| 207 | else: |
| 208 | escapes.append("\\%03o" % i) |
| 209 | escapes[ord('\\')] = '\\\\' |
| 210 | escapes[ord('\t')] = '\\t' |
| 211 | escapes[ord('\r')] = '\\r' |
| 212 | escapes[ord('\n')] = '\\n' |
| 213 | escapes[ord('\"')] = '\\"' |
| 214 | |
Barry Warsaw | 5dbf526 | 1999-11-03 18:47:52 +0000 | [diff] [blame] | 215 | |
| 216 | def escape(s): |
Barry Warsaw | c8f0892 | 2000-02-26 20:56:47 +0000 | [diff] [blame] | 217 | global escapes |
Barry Warsaw | 5dbf526 | 1999-11-03 18:47:52 +0000 | [diff] [blame] | 218 | s = list(s) |
| 219 | for i in range(len(s)): |
| 220 | s[i] = escapes[ord(s[i])] |
Barry Warsaw | a17e0f1 | 2000-03-08 15:18:35 +0000 | [diff] [blame] | 221 | return EMPTYSTRING.join(s) |
Barry Warsaw | 5dbf526 | 1999-11-03 18:47:52 +0000 | [diff] [blame] | 222 | |
| 223 | |
| 224 | def safe_eval(s): |
| 225 | # unwrap quotes, safely |
| 226 | return eval(s, {'__builtins__':{}}, {}) |
| 227 | |
| 228 | |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 229 | def normalize(s): |
| 230 | # This converts the various Python string types into a format that is |
| 231 | # appropriate for .po files, namely much closer to C style. |
Barry Warsaw | a17e0f1 | 2000-03-08 15:18:35 +0000 | [diff] [blame] | 232 | lines = s.split('\n') |
Barry Warsaw | 5dbf526 | 1999-11-03 18:47:52 +0000 | [diff] [blame] | 233 | if len(lines) == 1: |
| 234 | s = '"' + escape(s) + '"' |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 235 | else: |
Barry Warsaw | 5dbf526 | 1999-11-03 18:47:52 +0000 | [diff] [blame] | 236 | if not lines[-1]: |
| 237 | del lines[-1] |
| 238 | lines[-1] = lines[-1] + '\n' |
| 239 | for i in range(len(lines)): |
| 240 | lines[i] = escape(lines[i]) |
Barry Warsaw | a17e0f1 | 2000-03-08 15:18:35 +0000 | [diff] [blame] | 241 | lineterm = '\\n"\n"' |
| 242 | s = '""\n"' + lineterm.join(lines) + '"' |
Barry Warsaw | 5dbf526 | 1999-11-03 18:47:52 +0000 | [diff] [blame] | 243 | return s |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 244 | |
| 245 | |
| 246 | |
| 247 | class TokenEater: |
| 248 | def __init__(self, options): |
| 249 | self.__options = options |
| 250 | self.__messages = {} |
| 251 | self.__state = self.__waiting |
| 252 | self.__data = [] |
| 253 | self.__lineno = -1 |
Barry Warsaw | 08a8a35 | 2000-10-27 04:56:28 +0000 | [diff] [blame] | 254 | self.__freshmodule = 1 |
Barry Warsaw | 63ce5af | 2001-07-27 16:47:18 +0000 | [diff] [blame] | 255 | self.__curfile = None |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 256 | |
| 257 | def __call__(self, ttype, tstring, stup, etup, line): |
| 258 | # dispatch |
Barry Warsaw | 08a8a35 | 2000-10-27 04:56:28 +0000 | [diff] [blame] | 259 | ## import token |
| 260 | ## print >> sys.stderr, 'ttype:', token.tok_name[ttype], \ |
| 261 | ## 'tstring:', tstring |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 262 | self.__state(ttype, tstring, stup[0]) |
| 263 | |
| 264 | def __waiting(self, ttype, tstring, lineno): |
Barry Warsaw | 63ce5af | 2001-07-27 16:47:18 +0000 | [diff] [blame] | 265 | opts = self.__options |
Barry Warsaw | 08a8a35 | 2000-10-27 04:56:28 +0000 | [diff] [blame] | 266 | # Do docstring extractions, if enabled |
Barry Warsaw | 63ce5af | 2001-07-27 16:47:18 +0000 | [diff] [blame] | 267 | if opts.docstrings and not opts.nodocstrings.get(self.__curfile): |
Barry Warsaw | 08a8a35 | 2000-10-27 04:56:28 +0000 | [diff] [blame] | 268 | # module docstring? |
| 269 | if self.__freshmodule: |
| 270 | if ttype == tokenize.STRING: |
Barry Warsaw | 16b62c1 | 2001-05-21 19:51:26 +0000 | [diff] [blame] | 271 | self.__addentry(safe_eval(tstring), lineno, isdocstring=1) |
Barry Warsaw | 08a8a35 | 2000-10-27 04:56:28 +0000 | [diff] [blame] | 272 | self.__freshmodule = 0 |
| 273 | elif ttype not in (tokenize.COMMENT, tokenize.NL): |
| 274 | self.__freshmodule = 0 |
| 275 | return |
| 276 | # class docstring? |
| 277 | if ttype == tokenize.NAME and tstring in ('class', 'def'): |
| 278 | self.__state = self.__suiteseen |
| 279 | return |
Barry Warsaw | 63ce5af | 2001-07-27 16:47:18 +0000 | [diff] [blame] | 280 | if ttype == tokenize.NAME and tstring in opts.keywords: |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 281 | self.__state = self.__keywordseen |
| 282 | |
Barry Warsaw | 08a8a35 | 2000-10-27 04:56:28 +0000 | [diff] [blame] | 283 | def __suiteseen(self, ttype, tstring, lineno): |
| 284 | # ignore anything until we see the colon |
| 285 | if ttype == tokenize.OP and tstring == ':': |
| 286 | self.__state = self.__suitedocstring |
| 287 | |
| 288 | def __suitedocstring(self, ttype, tstring, lineno): |
| 289 | # ignore any intervening noise |
| 290 | if ttype == tokenize.STRING: |
Barry Warsaw | 16b62c1 | 2001-05-21 19:51:26 +0000 | [diff] [blame] | 291 | self.__addentry(safe_eval(tstring), lineno, isdocstring=1) |
Barry Warsaw | 08a8a35 | 2000-10-27 04:56:28 +0000 | [diff] [blame] | 292 | self.__state = self.__waiting |
| 293 | elif ttype not in (tokenize.NEWLINE, tokenize.INDENT, |
| 294 | tokenize.COMMENT): |
| 295 | # there was no class docstring |
| 296 | self.__state = self.__waiting |
| 297 | |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 298 | def __keywordseen(self, ttype, tstring, lineno): |
| 299 | if ttype == tokenize.OP and tstring == '(': |
| 300 | self.__data = [] |
| 301 | self.__lineno = lineno |
| 302 | self.__state = self.__openseen |
| 303 | else: |
| 304 | self.__state = self.__waiting |
| 305 | |
| 306 | def __openseen(self, ttype, tstring, lineno): |
| 307 | if ttype == tokenize.OP and tstring == ')': |
| 308 | # We've seen the last of the translatable strings. Record the |
| 309 | # line number of the first line of the strings and update the list |
| 310 | # of messages seen. Reset state for the next batch. If there |
| 311 | # were no strings inside _(), then just ignore this entry. |
| 312 | if self.__data: |
Barry Warsaw | 08a8a35 | 2000-10-27 04:56:28 +0000 | [diff] [blame] | 313 | self.__addentry(EMPTYSTRING.join(self.__data)) |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 314 | self.__state = self.__waiting |
| 315 | elif ttype == tokenize.STRING: |
Barry Warsaw | 5dbf526 | 1999-11-03 18:47:52 +0000 | [diff] [blame] | 316 | self.__data.append(safe_eval(tstring)) |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 317 | # TBD: should we warn if we seen anything else? |
| 318 | |
Barry Warsaw | 16b62c1 | 2001-05-21 19:51:26 +0000 | [diff] [blame] | 319 | def __addentry(self, msg, lineno=None, isdocstring=0): |
Barry Warsaw | 08a8a35 | 2000-10-27 04:56:28 +0000 | [diff] [blame] | 320 | if lineno is None: |
| 321 | lineno = self.__lineno |
| 322 | if not msg in self.__options.toexclude: |
| 323 | entry = (self.__curfile, lineno) |
Barry Warsaw | 16b62c1 | 2001-05-21 19:51:26 +0000 | [diff] [blame] | 324 | self.__messages.setdefault(msg, {})[entry] = isdocstring |
Barry Warsaw | 08a8a35 | 2000-10-27 04:56:28 +0000 | [diff] [blame] | 325 | |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 326 | def set_filename(self, filename): |
| 327 | self.__curfile = filename |
Barry Warsaw | 63ce5af | 2001-07-27 16:47:18 +0000 | [diff] [blame] | 328 | self.__freshmodule = 1 |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 329 | |
| 330 | def write(self, fp): |
| 331 | options = self.__options |
| 332 | timestamp = time.ctime(time.time()) |
Barry Warsaw | 08a8a35 | 2000-10-27 04:56:28 +0000 | [diff] [blame] | 333 | # The time stamp in the header doesn't have the same format as that |
| 334 | # generated by xgettext... |
| 335 | print >> fp, pot_header % {'time': timestamp, 'version': __version__} |
Barry Warsaw | 128c77d | 2001-05-23 16:59:45 +0000 | [diff] [blame] | 336 | # Sort the entries. First sort each particular entry's keys, then |
| 337 | # sort all the entries by their first item. |
| 338 | reverse = {} |
Fred Drake | 33e2c3e | 2000-10-26 03:49:15 +0000 | [diff] [blame] | 339 | for k, v in self.__messages.items(): |
Barry Warsaw | 128c77d | 2001-05-23 16:59:45 +0000 | [diff] [blame] | 340 | keys = v.keys() |
| 341 | keys.sort() |
Barry Warsaw | 50cf706 | 2001-05-24 23:06:13 +0000 | [diff] [blame] | 342 | reverse.setdefault(tuple(keys), []).append((k, v)) |
Barry Warsaw | 128c77d | 2001-05-23 16:59:45 +0000 | [diff] [blame] | 343 | rkeys = reverse.keys() |
| 344 | rkeys.sort() |
| 345 | for rkey in rkeys: |
Barry Warsaw | 50cf706 | 2001-05-24 23:06:13 +0000 | [diff] [blame] | 346 | rentries = reverse[rkey] |
| 347 | rentries.sort() |
| 348 | for k, v in rentries: |
Barry Warsaw | 5c94ce5 | 2001-06-20 19:41:40 +0000 | [diff] [blame] | 349 | isdocstring = 0 |
Barry Warsaw | 50cf706 | 2001-05-24 23:06:13 +0000 | [diff] [blame] | 350 | # If the entry was gleaned out of a docstring, then add a |
| 351 | # comment stating so. This is to aid translators who may wish |
| 352 | # to skip translating some unimportant docstrings. |
| 353 | if reduce(operator.__add__, v.values()): |
Barry Warsaw | 5c94ce5 | 2001-06-20 19:41:40 +0000 | [diff] [blame] | 354 | isdocstring = 1 |
Barry Warsaw | 50cf706 | 2001-05-24 23:06:13 +0000 | [diff] [blame] | 355 | # k is the message string, v is a dictionary-set of (filename, |
| 356 | # lineno) tuples. We want to sort the entries in v first by |
| 357 | # file name and then by line number. |
| 358 | v = v.keys() |
| 359 | v.sort() |
| 360 | if not options.writelocations: |
| 361 | pass |
| 362 | # location comments are different b/w Solaris and GNU: |
| 363 | elif options.locationstyle == options.SOLARIS: |
| 364 | for filename, lineno in v: |
| 365 | d = {'filename': filename, 'lineno': lineno} |
| 366 | print >>fp, _( |
| 367 | '# File: %(filename)s, line: %(lineno)d') % d |
| 368 | elif options.locationstyle == options.GNU: |
| 369 | # fit as many locations on one line, as long as the |
| 370 | # resulting line length doesn't exceeds 'options.width' |
| 371 | locline = '#:' |
| 372 | for filename, lineno in v: |
| 373 | d = {'filename': filename, 'lineno': lineno} |
| 374 | s = _(' %(filename)s:%(lineno)d') % d |
| 375 | if len(locline) + len(s) <= options.width: |
| 376 | locline = locline + s |
| 377 | else: |
| 378 | print >> fp, locline |
| 379 | locline = "#:" + s |
| 380 | if len(locline) > 2: |
Barry Warsaw | 08a8a35 | 2000-10-27 04:56:28 +0000 | [diff] [blame] | 381 | print >> fp, locline |
Barry Warsaw | 5c94ce5 | 2001-06-20 19:41:40 +0000 | [diff] [blame] | 382 | if isdocstring: |
| 383 | print >> fp, '#, docstring' |
Barry Warsaw | 50cf706 | 2001-05-24 23:06:13 +0000 | [diff] [blame] | 384 | print >> fp, 'msgid', normalize(k) |
| 385 | print >> fp, 'msgstr ""\n' |
Barry Warsaw | 08a8a35 | 2000-10-27 04:56:28 +0000 | [diff] [blame] | 386 | |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 387 | |
| 388 | |
| 389 | def main(): |
Barry Warsaw | a17e0f1 | 2000-03-08 15:18:35 +0000 | [diff] [blame] | 390 | global default_keywords |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 391 | try: |
| 392 | opts, args = getopt.getopt( |
| 393 | sys.argv[1:], |
Barry Warsaw | 63ce5af | 2001-07-27 16:47:18 +0000 | [diff] [blame] | 394 | 'ad:DEhk:Kno:p:S:Vvw:x:X:', |
Barry Warsaw | 2b63969 | 2001-05-21 19:58:23 +0000 | [diff] [blame] | 395 | ['extract-all', 'default-domain=', 'escape', 'help', |
Barry Warsaw | a17e0f1 | 2000-03-08 15:18:35 +0000 | [diff] [blame] | 396 | 'keyword=', 'no-default-keywords', |
Barry Warsaw | c8f0892 | 2000-02-26 20:56:47 +0000 | [diff] [blame] | 397 | 'add-location', 'no-location', 'output=', 'output-dir=', |
Barry Warsaw | a17e0f1 | 2000-03-08 15:18:35 +0000 | [diff] [blame] | 398 | 'style=', 'verbose', 'version', 'width=', 'exclude-file=', |
Barry Warsaw | 63ce5af | 2001-07-27 16:47:18 +0000 | [diff] [blame] | 399 | 'docstrings', 'no-docstrings', |
Barry Warsaw | c8f0892 | 2000-02-26 20:56:47 +0000 | [diff] [blame] | 400 | ]) |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 401 | except getopt.error, msg: |
| 402 | usage(1, msg) |
| 403 | |
| 404 | # for holding option values |
| 405 | class Options: |
| 406 | # constants |
| 407 | GNU = 1 |
| 408 | SOLARIS = 2 |
| 409 | # defaults |
Barry Warsaw | c8f0892 | 2000-02-26 20:56:47 +0000 | [diff] [blame] | 410 | extractall = 0 # FIXME: currently this option has no effect at all. |
| 411 | escape = 0 |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 412 | keywords = [] |
Barry Warsaw | c8f0892 | 2000-02-26 20:56:47 +0000 | [diff] [blame] | 413 | outpath = '' |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 414 | outfile = 'messages.pot' |
Barry Warsaw | a17e0f1 | 2000-03-08 15:18:35 +0000 | [diff] [blame] | 415 | writelocations = 1 |
| 416 | locationstyle = GNU |
Barry Warsaw | 5dbf526 | 1999-11-03 18:47:52 +0000 | [diff] [blame] | 417 | verbose = 0 |
Barry Warsaw | c8f0892 | 2000-02-26 20:56:47 +0000 | [diff] [blame] | 418 | width = 78 |
| 419 | excludefilename = '' |
Barry Warsaw | 08a8a35 | 2000-10-27 04:56:28 +0000 | [diff] [blame] | 420 | docstrings = 0 |
Barry Warsaw | 63ce5af | 2001-07-27 16:47:18 +0000 | [diff] [blame] | 421 | nodocstrings = {} |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 422 | |
| 423 | options = Options() |
| 424 | locations = {'gnu' : options.GNU, |
| 425 | 'solaris' : options.SOLARIS, |
| 426 | } |
| 427 | |
| 428 | # parse options |
| 429 | for opt, arg in opts: |
| 430 | if opt in ('-h', '--help'): |
| 431 | usage(0) |
Barry Warsaw | c8f0892 | 2000-02-26 20:56:47 +0000 | [diff] [blame] | 432 | elif opt in ('-a', '--extract-all'): |
| 433 | options.extractall = 1 |
| 434 | elif opt in ('-d', '--default-domain'): |
| 435 | options.outfile = arg + '.pot' |
| 436 | elif opt in ('-E', '--escape'): |
| 437 | options.escape = 1 |
Barry Warsaw | 08a8a35 | 2000-10-27 04:56:28 +0000 | [diff] [blame] | 438 | elif opt in ('-D', '--docstrings'): |
| 439 | options.docstrings = 1 |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 440 | elif opt in ('-k', '--keyword'): |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 441 | options.keywords.append(arg) |
Barry Warsaw | a17e0f1 | 2000-03-08 15:18:35 +0000 | [diff] [blame] | 442 | elif opt in ('-K', '--no-default-keywords'): |
| 443 | default_keywords = [] |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 444 | elif opt in ('-n', '--add-location'): |
Barry Warsaw | a17e0f1 | 2000-03-08 15:18:35 +0000 | [diff] [blame] | 445 | options.writelocations = 1 |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 446 | elif opt in ('--no-location',): |
Barry Warsaw | a17e0f1 | 2000-03-08 15:18:35 +0000 | [diff] [blame] | 447 | options.writelocations = 0 |
| 448 | elif opt in ('-S', '--style'): |
| 449 | options.locationstyle = locations.get(arg.lower()) |
| 450 | if options.locationstyle is None: |
| 451 | usage(1, _('Invalid value for --style: %s') % arg) |
Barry Warsaw | c8f0892 | 2000-02-26 20:56:47 +0000 | [diff] [blame] | 452 | elif opt in ('-o', '--output'): |
| 453 | options.outfile = arg |
| 454 | elif opt in ('-p', '--output-dir'): |
| 455 | options.outpath = arg |
Barry Warsaw | 5dbf526 | 1999-11-03 18:47:52 +0000 | [diff] [blame] | 456 | elif opt in ('-v', '--verbose'): |
| 457 | options.verbose = 1 |
Barry Warsaw | c8f0892 | 2000-02-26 20:56:47 +0000 | [diff] [blame] | 458 | elif opt in ('-V', '--version'): |
| 459 | print _('pygettext.py (xgettext for Python) %s') % __version__ |
| 460 | sys.exit(0) |
| 461 | elif opt in ('-w', '--width'): |
| 462 | try: |
| 463 | options.width = int(arg) |
| 464 | except ValueError: |
Barry Warsaw | a17e0f1 | 2000-03-08 15:18:35 +0000 | [diff] [blame] | 465 | usage(1, _('--width argument must be an integer: %s') % arg) |
Barry Warsaw | c8f0892 | 2000-02-26 20:56:47 +0000 | [diff] [blame] | 466 | elif opt in ('-x', '--exclude-file'): |
| 467 | options.excludefilename = arg |
Barry Warsaw | 63ce5af | 2001-07-27 16:47:18 +0000 | [diff] [blame] | 468 | elif opt in ('-X', '--no-docstrings'): |
| 469 | fp = open(arg) |
| 470 | try: |
| 471 | while 1: |
| 472 | line = fp.readline() |
| 473 | if not line: |
| 474 | break |
| 475 | options.nodocstrings[line[:-1]] = 1 |
| 476 | finally: |
| 477 | fp.close() |
Barry Warsaw | c8f0892 | 2000-02-26 20:56:47 +0000 | [diff] [blame] | 478 | |
| 479 | # calculate escapes |
Barry Warsaw | 7733e12 | 2000-02-27 14:30:48 +0000 | [diff] [blame] | 480 | make_escapes(options.escape) |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 481 | |
| 482 | # calculate all keywords |
| 483 | options.keywords.extend(default_keywords) |
| 484 | |
Barry Warsaw | c8f0892 | 2000-02-26 20:56:47 +0000 | [diff] [blame] | 485 | # initialize list of strings to exclude |
| 486 | if options.excludefilename: |
| 487 | try: |
| 488 | fp = open(options.excludefilename) |
| 489 | options.toexclude = fp.readlines() |
| 490 | fp.close() |
| 491 | except IOError: |
Barry Warsaw | 6e97241 | 2001-05-21 19:35:20 +0000 | [diff] [blame] | 492 | print >> sys.stderr, _( |
| 493 | "Can't read --exclude-file: %s") % options.excludefilename |
Barry Warsaw | c8f0892 | 2000-02-26 20:56:47 +0000 | [diff] [blame] | 494 | sys.exit(1) |
| 495 | else: |
| 496 | options.toexclude = [] |
| 497 | |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 498 | # slurp through all the files |
| 499 | eater = TokenEater(options) |
| 500 | for filename in args: |
Barry Warsaw | a17e0f1 | 2000-03-08 15:18:35 +0000 | [diff] [blame] | 501 | if filename == '-': |
| 502 | if options.verbose: |
| 503 | print _('Reading standard input') |
| 504 | fp = sys.stdin |
| 505 | closep = 0 |
| 506 | else: |
| 507 | if options.verbose: |
| 508 | print _('Working on %s') % filename |
| 509 | fp = open(filename) |
| 510 | closep = 1 |
| 511 | try: |
| 512 | eater.set_filename(filename) |
Barry Warsaw | 75ee8f5 | 2001-02-26 04:46:53 +0000 | [diff] [blame] | 513 | try: |
| 514 | tokenize.tokenize(fp.readline, eater) |
| 515 | except tokenize.TokenError, e: |
Barry Warsaw | 6e97241 | 2001-05-21 19:35:20 +0000 | [diff] [blame] | 516 | print >> sys.stderr, '%s: %s, line %d, column %d' % ( |
| 517 | e[0], filename, e[1][0], e[1][1]) |
Barry Warsaw | a17e0f1 | 2000-03-08 15:18:35 +0000 | [diff] [blame] | 518 | finally: |
| 519 | if closep: |
| 520 | fp.close() |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 521 | |
Barry Warsaw | a17e0f1 | 2000-03-08 15:18:35 +0000 | [diff] [blame] | 522 | # write the output |
| 523 | if options.outfile == '-': |
| 524 | fp = sys.stdout |
| 525 | closep = 0 |
| 526 | else: |
| 527 | if options.outpath: |
| 528 | options.outfile = os.path.join(options.outpath, options.outfile) |
| 529 | fp = open(options.outfile, 'w') |
| 530 | closep = 1 |
| 531 | try: |
| 532 | eater.write(fp) |
| 533 | finally: |
| 534 | if closep: |
| 535 | fp.close() |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 536 | |
| 537 | |
| 538 | if __name__ == '__main__': |
| 539 | main() |
Barry Warsaw | 75a6e67 | 2000-05-02 19:28:30 +0000 | [diff] [blame] | 540 | # some more test strings |
| 541 | _(u'a unicode string') |