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