Barry Warsaw | af57251 | 1999-08-11 21:40:38 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
Barry Warsaw | 6e97241 | 2001-05-21 19:35:20 +0000 | [diff] [blame] | 2 | # Originally written by Barry Warsaw <barry@digicool.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 |
| 52 | Extract all strings |
| 53 | |
Barry Warsaw | c8f0892 | 2000-02-26 20:56:47 +0000 | [diff] [blame] | 54 | -d name |
| 55 | --default-domain=name |
| 56 | Rename the default output file from messages.pot to name.pot |
| 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 |
| 66 | consider them docstrings. |
Barry Warsaw | c8f0892 | 2000-02-26 20:56:47 +0000 | [diff] [blame] | 67 | |
| 68 | -h |
| 69 | --help |
| 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 | a17e0f1 | 2000-03-08 15:18:35 +0000 | [diff] [blame] | 131 | If `inputfile' is -, standard input is read. |
| 132 | |
Barry Warsaw | 08a8a35 | 2000-10-27 04:56:28 +0000 | [diff] [blame] | 133 | """ |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 134 | |
| 135 | import os |
| 136 | import sys |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 137 | import time |
| 138 | import getopt |
| 139 | import tokenize |
Barry Warsaw | 16b62c1 | 2001-05-21 19:51:26 +0000 | [diff] [blame] | 140 | import operator |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 141 | |
Barry Warsaw | 08a8a35 | 2000-10-27 04:56:28 +0000 | [diff] [blame] | 142 | # for selftesting |
| 143 | try: |
| 144 | import fintl |
| 145 | _ = fintl.gettext |
| 146 | except ImportError: |
| 147 | def _(s): return s |
| 148 | |
Martin v. Löwis | 0f6b383 | 2001-03-01 22:56:17 +0000 | [diff] [blame] | 149 | __version__ = '1.3' |
Barry Warsaw | a17e0f1 | 2000-03-08 15:18:35 +0000 | [diff] [blame] | 150 | |
| 151 | default_keywords = ['_'] |
| 152 | DEFAULTKEYWORDS = ', '.join(default_keywords) |
| 153 | |
| 154 | EMPTYSTRING = '' |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 155 | |
| 156 | |
| 157 | |
Barry Warsaw | 5dbf526 | 1999-11-03 18:47:52 +0000 | [diff] [blame] | 158 | # The normal pot-file header. msgmerge and EMACS' po-mode work better if |
| 159 | # it's there. |
| 160 | pot_header = _('''\ |
| 161 | # SOME DESCRIPTIVE TITLE. |
| 162 | # Copyright (C) YEAR ORGANIZATION |
| 163 | # FIRST AUTHOR <EMAIL@ADDRESS>, YEAR. |
| 164 | # |
| 165 | msgid "" |
| 166 | msgstr "" |
| 167 | "Project-Id-Version: PACKAGE VERSION\\n" |
Martin v. Löwis | 0f6b383 | 2001-03-01 22:56:17 +0000 | [diff] [blame] | 168 | "POT-Creation-Date: %(time)s\\n" |
| 169 | "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\\n" |
Barry Warsaw | 5dbf526 | 1999-11-03 18:47:52 +0000 | [diff] [blame] | 170 | "Last-Translator: FULL NAME <EMAIL@ADDRESS>\\n" |
| 171 | "Language-Team: LANGUAGE <LL@li.org>\\n" |
| 172 | "MIME-Version: 1.0\\n" |
| 173 | "Content-Type: text/plain; charset=CHARSET\\n" |
| 174 | "Content-Transfer-Encoding: ENCODING\\n" |
| 175 | "Generated-By: pygettext.py %(version)s\\n" |
| 176 | |
| 177 | ''') |
| 178 | |
| 179 | |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 180 | def usage(code, msg=''): |
Barry Warsaw | 08a8a35 | 2000-10-27 04:56:28 +0000 | [diff] [blame] | 181 | print >> sys.stderr, _(__doc__) % globals() |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 182 | if msg: |
Barry Warsaw | 08a8a35 | 2000-10-27 04:56:28 +0000 | [diff] [blame] | 183 | print >> sys.stderr, msg |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 184 | sys.exit(code) |
| 185 | |
Barry Warsaw | c8f0892 | 2000-02-26 20:56:47 +0000 | [diff] [blame] | 186 | |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 187 | |
Barry Warsaw | 5dbf526 | 1999-11-03 18:47:52 +0000 | [diff] [blame] | 188 | escapes = [] |
Barry Warsaw | 5dbf526 | 1999-11-03 18:47:52 +0000 | [diff] [blame] | 189 | |
Barry Warsaw | c8f0892 | 2000-02-26 20:56:47 +0000 | [diff] [blame] | 190 | def make_escapes(pass_iso8859): |
| 191 | global escapes |
Barry Warsaw | 7733e12 | 2000-02-27 14:30:48 +0000 | [diff] [blame] | 192 | if pass_iso8859: |
| 193 | # Allow iso-8859 characters to pass through so that e.g. 'msgid |
| 194 | # "Höhe"' would result not result in 'msgid "H\366he"'. Otherwise we |
| 195 | # escape any character outside the 32..126 range. |
| 196 | mod = 128 |
| 197 | else: |
| 198 | mod = 256 |
Barry Warsaw | c8f0892 | 2000-02-26 20:56:47 +0000 | [diff] [blame] | 199 | for i in range(256): |
Barry Warsaw | 7733e12 | 2000-02-27 14:30:48 +0000 | [diff] [blame] | 200 | if 32 <= (i % mod) <= 126: |
Barry Warsaw | c8f0892 | 2000-02-26 20:56:47 +0000 | [diff] [blame] | 201 | escapes.append(chr(i)) |
| 202 | else: |
| 203 | escapes.append("\\%03o" % i) |
| 204 | escapes[ord('\\')] = '\\\\' |
| 205 | escapes[ord('\t')] = '\\t' |
| 206 | escapes[ord('\r')] = '\\r' |
| 207 | escapes[ord('\n')] = '\\n' |
| 208 | escapes[ord('\"')] = '\\"' |
| 209 | |
Barry Warsaw | 5dbf526 | 1999-11-03 18:47:52 +0000 | [diff] [blame] | 210 | |
| 211 | def escape(s): |
Barry Warsaw | c8f0892 | 2000-02-26 20:56:47 +0000 | [diff] [blame] | 212 | global escapes |
Barry Warsaw | 5dbf526 | 1999-11-03 18:47:52 +0000 | [diff] [blame] | 213 | s = list(s) |
| 214 | for i in range(len(s)): |
| 215 | s[i] = escapes[ord(s[i])] |
Barry Warsaw | a17e0f1 | 2000-03-08 15:18:35 +0000 | [diff] [blame] | 216 | return EMPTYSTRING.join(s) |
Barry Warsaw | 5dbf526 | 1999-11-03 18:47:52 +0000 | [diff] [blame] | 217 | |
| 218 | |
| 219 | def safe_eval(s): |
| 220 | # unwrap quotes, safely |
| 221 | return eval(s, {'__builtins__':{}}, {}) |
| 222 | |
| 223 | |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 224 | def normalize(s): |
| 225 | # This converts the various Python string types into a format that is |
| 226 | # appropriate for .po files, namely much closer to C style. |
Barry Warsaw | a17e0f1 | 2000-03-08 15:18:35 +0000 | [diff] [blame] | 227 | lines = s.split('\n') |
Barry Warsaw | 5dbf526 | 1999-11-03 18:47:52 +0000 | [diff] [blame] | 228 | if len(lines) == 1: |
| 229 | s = '"' + escape(s) + '"' |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 230 | else: |
Barry Warsaw | 5dbf526 | 1999-11-03 18:47:52 +0000 | [diff] [blame] | 231 | if not lines[-1]: |
| 232 | del lines[-1] |
| 233 | lines[-1] = lines[-1] + '\n' |
| 234 | for i in range(len(lines)): |
| 235 | lines[i] = escape(lines[i]) |
Barry Warsaw | a17e0f1 | 2000-03-08 15:18:35 +0000 | [diff] [blame] | 236 | lineterm = '\\n"\n"' |
| 237 | s = '""\n"' + lineterm.join(lines) + '"' |
Barry Warsaw | 5dbf526 | 1999-11-03 18:47:52 +0000 | [diff] [blame] | 238 | return s |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 239 | |
| 240 | |
| 241 | |
| 242 | class TokenEater: |
| 243 | def __init__(self, options): |
| 244 | self.__options = options |
| 245 | self.__messages = {} |
| 246 | self.__state = self.__waiting |
| 247 | self.__data = [] |
| 248 | self.__lineno = -1 |
Barry Warsaw | 08a8a35 | 2000-10-27 04:56:28 +0000 | [diff] [blame] | 249 | self.__freshmodule = 1 |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 250 | |
| 251 | def __call__(self, ttype, tstring, stup, etup, line): |
| 252 | # dispatch |
Barry Warsaw | 08a8a35 | 2000-10-27 04:56:28 +0000 | [diff] [blame] | 253 | ## import token |
| 254 | ## print >> sys.stderr, 'ttype:', token.tok_name[ttype], \ |
| 255 | ## 'tstring:', tstring |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 256 | self.__state(ttype, tstring, stup[0]) |
| 257 | |
| 258 | def __waiting(self, ttype, tstring, lineno): |
Barry Warsaw | 08a8a35 | 2000-10-27 04:56:28 +0000 | [diff] [blame] | 259 | # Do docstring extractions, if enabled |
| 260 | if self.__options.docstrings: |
| 261 | # module docstring? |
| 262 | if self.__freshmodule: |
| 263 | if ttype == tokenize.STRING: |
Barry Warsaw | 16b62c1 | 2001-05-21 19:51:26 +0000 | [diff] [blame] | 264 | self.__addentry(safe_eval(tstring), lineno, isdocstring=1) |
Barry Warsaw | 08a8a35 | 2000-10-27 04:56:28 +0000 | [diff] [blame] | 265 | self.__freshmodule = 0 |
| 266 | elif ttype not in (tokenize.COMMENT, tokenize.NL): |
| 267 | self.__freshmodule = 0 |
| 268 | return |
| 269 | # class docstring? |
| 270 | if ttype == tokenize.NAME and tstring in ('class', 'def'): |
| 271 | self.__state = self.__suiteseen |
| 272 | return |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 273 | if ttype == tokenize.NAME and tstring in self.__options.keywords: |
| 274 | self.__state = self.__keywordseen |
| 275 | |
Barry Warsaw | 08a8a35 | 2000-10-27 04:56:28 +0000 | [diff] [blame] | 276 | def __suiteseen(self, ttype, tstring, lineno): |
| 277 | # ignore anything until we see the colon |
| 278 | if ttype == tokenize.OP and tstring == ':': |
| 279 | self.__state = self.__suitedocstring |
| 280 | |
| 281 | def __suitedocstring(self, ttype, tstring, lineno): |
| 282 | # ignore any intervening noise |
| 283 | if ttype == tokenize.STRING: |
Barry Warsaw | 16b62c1 | 2001-05-21 19:51:26 +0000 | [diff] [blame] | 284 | self.__addentry(safe_eval(tstring), lineno, isdocstring=1) |
Barry Warsaw | 08a8a35 | 2000-10-27 04:56:28 +0000 | [diff] [blame] | 285 | self.__state = self.__waiting |
| 286 | elif ttype not in (tokenize.NEWLINE, tokenize.INDENT, |
| 287 | tokenize.COMMENT): |
| 288 | # there was no class docstring |
| 289 | self.__state = self.__waiting |
| 290 | |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 291 | def __keywordseen(self, ttype, tstring, lineno): |
| 292 | if ttype == tokenize.OP and tstring == '(': |
| 293 | self.__data = [] |
| 294 | self.__lineno = lineno |
| 295 | self.__state = self.__openseen |
| 296 | else: |
| 297 | self.__state = self.__waiting |
| 298 | |
| 299 | def __openseen(self, ttype, tstring, lineno): |
| 300 | if ttype == tokenize.OP and tstring == ')': |
| 301 | # We've seen the last of the translatable strings. Record the |
| 302 | # line number of the first line of the strings and update the list |
| 303 | # of messages seen. Reset state for the next batch. If there |
| 304 | # were no strings inside _(), then just ignore this entry. |
| 305 | if self.__data: |
Barry Warsaw | 08a8a35 | 2000-10-27 04:56:28 +0000 | [diff] [blame] | 306 | self.__addentry(EMPTYSTRING.join(self.__data)) |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 307 | self.__state = self.__waiting |
| 308 | elif ttype == tokenize.STRING: |
Barry Warsaw | 5dbf526 | 1999-11-03 18:47:52 +0000 | [diff] [blame] | 309 | self.__data.append(safe_eval(tstring)) |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 310 | # TBD: should we warn if we seen anything else? |
| 311 | |
Barry Warsaw | 16b62c1 | 2001-05-21 19:51:26 +0000 | [diff] [blame] | 312 | def __addentry(self, msg, lineno=None, isdocstring=0): |
Barry Warsaw | 08a8a35 | 2000-10-27 04:56:28 +0000 | [diff] [blame] | 313 | if lineno is None: |
| 314 | lineno = self.__lineno |
| 315 | if not msg in self.__options.toexclude: |
| 316 | entry = (self.__curfile, lineno) |
Barry Warsaw | 16b62c1 | 2001-05-21 19:51:26 +0000 | [diff] [blame] | 317 | self.__messages.setdefault(msg, {})[entry] = isdocstring |
Barry Warsaw | 08a8a35 | 2000-10-27 04:56:28 +0000 | [diff] [blame] | 318 | |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 319 | def set_filename(self, filename): |
| 320 | self.__curfile = filename |
| 321 | |
| 322 | def write(self, fp): |
| 323 | options = self.__options |
| 324 | timestamp = time.ctime(time.time()) |
Barry Warsaw | 08a8a35 | 2000-10-27 04:56:28 +0000 | [diff] [blame] | 325 | # The time stamp in the header doesn't have the same format as that |
| 326 | # generated by xgettext... |
| 327 | print >> fp, pot_header % {'time': timestamp, 'version': __version__} |
Barry Warsaw | 128c77d | 2001-05-23 16:59:45 +0000 | [diff] [blame] | 328 | # Sort the entries. First sort each particular entry's keys, then |
| 329 | # sort all the entries by their first item. |
| 330 | reverse = {} |
Fred Drake | 33e2c3e | 2000-10-26 03:49:15 +0000 | [diff] [blame] | 331 | for k, v in self.__messages.items(): |
Barry Warsaw | 128c77d | 2001-05-23 16:59:45 +0000 | [diff] [blame] | 332 | keys = v.keys() |
| 333 | keys.sort() |
Barry Warsaw | 50cf706 | 2001-05-24 23:06:13 +0000 | [diff] [blame^] | 334 | reverse.setdefault(tuple(keys), []).append((k, v)) |
Barry Warsaw | 128c77d | 2001-05-23 16:59:45 +0000 | [diff] [blame] | 335 | rkeys = reverse.keys() |
| 336 | rkeys.sort() |
| 337 | for rkey in rkeys: |
Barry Warsaw | 50cf706 | 2001-05-24 23:06:13 +0000 | [diff] [blame^] | 338 | rentries = reverse[rkey] |
| 339 | rentries.sort() |
| 340 | for k, v in rentries: |
| 341 | # If the entry was gleaned out of a docstring, then add a |
| 342 | # comment stating so. This is to aid translators who may wish |
| 343 | # to skip translating some unimportant docstrings. |
| 344 | if reduce(operator.__add__, v.values()): |
| 345 | print >> fp, '#. docstring' |
| 346 | # k is the message string, v is a dictionary-set of (filename, |
| 347 | # lineno) tuples. We want to sort the entries in v first by |
| 348 | # file name and then by line number. |
| 349 | v = v.keys() |
| 350 | v.sort() |
| 351 | if not options.writelocations: |
| 352 | pass |
| 353 | # location comments are different b/w Solaris and GNU: |
| 354 | elif options.locationstyle == options.SOLARIS: |
| 355 | for filename, lineno in v: |
| 356 | d = {'filename': filename, 'lineno': lineno} |
| 357 | print >>fp, _( |
| 358 | '# File: %(filename)s, line: %(lineno)d') % d |
| 359 | elif options.locationstyle == options.GNU: |
| 360 | # fit as many locations on one line, as long as the |
| 361 | # resulting line length doesn't exceeds 'options.width' |
| 362 | locline = '#:' |
| 363 | for filename, lineno in v: |
| 364 | d = {'filename': filename, 'lineno': lineno} |
| 365 | s = _(' %(filename)s:%(lineno)d') % d |
| 366 | if len(locline) + len(s) <= options.width: |
| 367 | locline = locline + s |
| 368 | else: |
| 369 | print >> fp, locline |
| 370 | locline = "#:" + s |
| 371 | if len(locline) > 2: |
Barry Warsaw | 08a8a35 | 2000-10-27 04:56:28 +0000 | [diff] [blame] | 372 | print >> fp, locline |
Barry Warsaw | 50cf706 | 2001-05-24 23:06:13 +0000 | [diff] [blame^] | 373 | print >> fp, 'msgid', normalize(k) |
| 374 | print >> fp, 'msgstr ""\n' |
Barry Warsaw | 08a8a35 | 2000-10-27 04:56:28 +0000 | [diff] [blame] | 375 | |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 376 | |
| 377 | |
| 378 | def main(): |
Barry Warsaw | a17e0f1 | 2000-03-08 15:18:35 +0000 | [diff] [blame] | 379 | global default_keywords |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 380 | try: |
| 381 | opts, args = getopt.getopt( |
| 382 | sys.argv[1:], |
Barry Warsaw | 08a8a35 | 2000-10-27 04:56:28 +0000 | [diff] [blame] | 383 | 'ad:DEhk:Kno:p:S:Vvw:x:', |
Barry Warsaw | 2b63969 | 2001-05-21 19:58:23 +0000 | [diff] [blame] | 384 | ['extract-all', 'default-domain=', 'escape', 'help', |
Barry Warsaw | a17e0f1 | 2000-03-08 15:18:35 +0000 | [diff] [blame] | 385 | 'keyword=', 'no-default-keywords', |
Barry Warsaw | c8f0892 | 2000-02-26 20:56:47 +0000 | [diff] [blame] | 386 | 'add-location', 'no-location', 'output=', 'output-dir=', |
Barry Warsaw | a17e0f1 | 2000-03-08 15:18:35 +0000 | [diff] [blame] | 387 | 'style=', 'verbose', 'version', 'width=', 'exclude-file=', |
Barry Warsaw | 08a8a35 | 2000-10-27 04:56:28 +0000 | [diff] [blame] | 388 | 'docstrings', |
Barry Warsaw | c8f0892 | 2000-02-26 20:56:47 +0000 | [diff] [blame] | 389 | ]) |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 390 | except getopt.error, msg: |
| 391 | usage(1, msg) |
| 392 | |
| 393 | # for holding option values |
| 394 | class Options: |
| 395 | # constants |
| 396 | GNU = 1 |
| 397 | SOLARIS = 2 |
| 398 | # defaults |
Barry Warsaw | c8f0892 | 2000-02-26 20:56:47 +0000 | [diff] [blame] | 399 | extractall = 0 # FIXME: currently this option has no effect at all. |
| 400 | escape = 0 |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 401 | keywords = [] |
Barry Warsaw | c8f0892 | 2000-02-26 20:56:47 +0000 | [diff] [blame] | 402 | outpath = '' |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 403 | outfile = 'messages.pot' |
Barry Warsaw | a17e0f1 | 2000-03-08 15:18:35 +0000 | [diff] [blame] | 404 | writelocations = 1 |
| 405 | locationstyle = GNU |
Barry Warsaw | 5dbf526 | 1999-11-03 18:47:52 +0000 | [diff] [blame] | 406 | verbose = 0 |
Barry Warsaw | c8f0892 | 2000-02-26 20:56:47 +0000 | [diff] [blame] | 407 | width = 78 |
| 408 | excludefilename = '' |
Barry Warsaw | 08a8a35 | 2000-10-27 04:56:28 +0000 | [diff] [blame] | 409 | docstrings = 0 |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 410 | |
| 411 | options = Options() |
| 412 | locations = {'gnu' : options.GNU, |
| 413 | 'solaris' : options.SOLARIS, |
| 414 | } |
| 415 | |
| 416 | # parse options |
| 417 | for opt, arg in opts: |
| 418 | if opt in ('-h', '--help'): |
| 419 | usage(0) |
Barry Warsaw | c8f0892 | 2000-02-26 20:56:47 +0000 | [diff] [blame] | 420 | elif opt in ('-a', '--extract-all'): |
| 421 | options.extractall = 1 |
| 422 | elif opt in ('-d', '--default-domain'): |
| 423 | options.outfile = arg + '.pot' |
| 424 | elif opt in ('-E', '--escape'): |
| 425 | options.escape = 1 |
Barry Warsaw | 08a8a35 | 2000-10-27 04:56:28 +0000 | [diff] [blame] | 426 | elif opt in ('-D', '--docstrings'): |
| 427 | options.docstrings = 1 |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 428 | elif opt in ('-k', '--keyword'): |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 429 | options.keywords.append(arg) |
Barry Warsaw | a17e0f1 | 2000-03-08 15:18:35 +0000 | [diff] [blame] | 430 | elif opt in ('-K', '--no-default-keywords'): |
| 431 | default_keywords = [] |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 432 | elif opt in ('-n', '--add-location'): |
Barry Warsaw | a17e0f1 | 2000-03-08 15:18:35 +0000 | [diff] [blame] | 433 | options.writelocations = 1 |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 434 | elif opt in ('--no-location',): |
Barry Warsaw | a17e0f1 | 2000-03-08 15:18:35 +0000 | [diff] [blame] | 435 | options.writelocations = 0 |
| 436 | elif opt in ('-S', '--style'): |
| 437 | options.locationstyle = locations.get(arg.lower()) |
| 438 | if options.locationstyle is None: |
| 439 | usage(1, _('Invalid value for --style: %s') % arg) |
Barry Warsaw | c8f0892 | 2000-02-26 20:56:47 +0000 | [diff] [blame] | 440 | elif opt in ('-o', '--output'): |
| 441 | options.outfile = arg |
| 442 | elif opt in ('-p', '--output-dir'): |
| 443 | options.outpath = arg |
Barry Warsaw | 5dbf526 | 1999-11-03 18:47:52 +0000 | [diff] [blame] | 444 | elif opt in ('-v', '--verbose'): |
| 445 | options.verbose = 1 |
Barry Warsaw | c8f0892 | 2000-02-26 20:56:47 +0000 | [diff] [blame] | 446 | elif opt in ('-V', '--version'): |
| 447 | print _('pygettext.py (xgettext for Python) %s') % __version__ |
| 448 | sys.exit(0) |
| 449 | elif opt in ('-w', '--width'): |
| 450 | try: |
| 451 | options.width = int(arg) |
| 452 | except ValueError: |
Barry Warsaw | a17e0f1 | 2000-03-08 15:18:35 +0000 | [diff] [blame] | 453 | usage(1, _('--width argument must be an integer: %s') % arg) |
Barry Warsaw | c8f0892 | 2000-02-26 20:56:47 +0000 | [diff] [blame] | 454 | elif opt in ('-x', '--exclude-file'): |
| 455 | options.excludefilename = arg |
| 456 | |
| 457 | # calculate escapes |
Barry Warsaw | 7733e12 | 2000-02-27 14:30:48 +0000 | [diff] [blame] | 458 | make_escapes(options.escape) |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 459 | |
| 460 | # calculate all keywords |
| 461 | options.keywords.extend(default_keywords) |
| 462 | |
Barry Warsaw | c8f0892 | 2000-02-26 20:56:47 +0000 | [diff] [blame] | 463 | # initialize list of strings to exclude |
| 464 | if options.excludefilename: |
| 465 | try: |
| 466 | fp = open(options.excludefilename) |
| 467 | options.toexclude = fp.readlines() |
| 468 | fp.close() |
| 469 | except IOError: |
Barry Warsaw | 6e97241 | 2001-05-21 19:35:20 +0000 | [diff] [blame] | 470 | print >> sys.stderr, _( |
| 471 | "Can't read --exclude-file: %s") % options.excludefilename |
Barry Warsaw | c8f0892 | 2000-02-26 20:56:47 +0000 | [diff] [blame] | 472 | sys.exit(1) |
| 473 | else: |
| 474 | options.toexclude = [] |
| 475 | |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 476 | # slurp through all the files |
| 477 | eater = TokenEater(options) |
| 478 | for filename in args: |
Barry Warsaw | a17e0f1 | 2000-03-08 15:18:35 +0000 | [diff] [blame] | 479 | if filename == '-': |
| 480 | if options.verbose: |
| 481 | print _('Reading standard input') |
| 482 | fp = sys.stdin |
| 483 | closep = 0 |
| 484 | else: |
| 485 | if options.verbose: |
| 486 | print _('Working on %s') % filename |
| 487 | fp = open(filename) |
| 488 | closep = 1 |
| 489 | try: |
| 490 | eater.set_filename(filename) |
Barry Warsaw | 75ee8f5 | 2001-02-26 04:46:53 +0000 | [diff] [blame] | 491 | try: |
| 492 | tokenize.tokenize(fp.readline, eater) |
| 493 | except tokenize.TokenError, e: |
Barry Warsaw | 6e97241 | 2001-05-21 19:35:20 +0000 | [diff] [blame] | 494 | print >> sys.stderr, '%s: %s, line %d, column %d' % ( |
| 495 | e[0], filename, e[1][0], e[1][1]) |
Barry Warsaw | a17e0f1 | 2000-03-08 15:18:35 +0000 | [diff] [blame] | 496 | finally: |
| 497 | if closep: |
| 498 | fp.close() |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 499 | |
Barry Warsaw | a17e0f1 | 2000-03-08 15:18:35 +0000 | [diff] [blame] | 500 | # write the output |
| 501 | if options.outfile == '-': |
| 502 | fp = sys.stdout |
| 503 | closep = 0 |
| 504 | else: |
| 505 | if options.outpath: |
| 506 | options.outfile = os.path.join(options.outpath, options.outfile) |
| 507 | fp = open(options.outfile, 'w') |
| 508 | closep = 1 |
| 509 | try: |
| 510 | eater.write(fp) |
| 511 | finally: |
| 512 | if closep: |
| 513 | fp.close() |
Barry Warsaw | e27db5a | 1999-08-13 20:59:48 +0000 | [diff] [blame] | 514 | |
| 515 | |
| 516 | if __name__ == '__main__': |
| 517 | main() |
Barry Warsaw | 75a6e67 | 2000-05-02 19:28:30 +0000 | [diff] [blame] | 518 | # some more test strings |
| 519 | _(u'a unicode string') |