annotate babel/messages/frontend.py @ 56:27fba894d3ca

Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
author cmlenz
date Fri, 08 Jun 2007 11:08:03 +0000
parents c3291ad6b010
children a6183d300a6e
rev   line source
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
1 # -*- coding: utf-8 -*-
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
2 #
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
3 # Copyright (C) 2007 Edgewall Software
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
4 # All rights reserved.
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
5 #
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
6 # This software is licensed as described in the file COPYING, which
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
7 # you should have received as part of this distribution. The terms
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
8 # are also available at http://babel.edgewall.org/wiki/License.
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
9 #
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
10 # This software consists of voluntary contributions made by many
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
11 # individuals. For the exact contribution history, see the revision
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
12 # history and logs, available at http://babel.edgewall.org/log/.
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
13
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
14 """Frontends for the message extraction functionality."""
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
15
48
bd647e3760e0 Move the mapping configuration file format to `ConfigParser`, and add some more documentation about it.
cmlenz
parents: 47
diff changeset
16 from ConfigParser import RawConfigParser
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
17 from distutils import log
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
18 from distutils.cmd import Command
49
daf35e2ad044 Support a `message_extractors` keyword argument directly in `setup()`. Closes #4.
cmlenz
parents: 48
diff changeset
19 from distutils.errors import DistutilsOptionError, DistutilsSetupError
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
20 from optparse import OptionParser
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
21 import os
47
76381d4b3635 Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents: 24
diff changeset
22 import re
49
daf35e2ad044 Support a `message_extractors` keyword argument directly in `setup()`. Closes #4.
cmlenz
parents: 48
diff changeset
23 from StringIO import StringIO
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
24 import sys
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
25
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
26 from babel import __version__ as VERSION
51
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
27 from babel import Locale
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
28 from babel.core import UnknownLocaleError
56
27fba894d3ca Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents: 55
diff changeset
29 from babel.messages.catalog import Catalog
54
b3395b285104 Rename the `babel.catalog` package to `babel.messages` for consistency with the other package names.
cmlenz
parents: 52
diff changeset
30 from babel.messages.extract import extract_from_dir, DEFAULT_KEYWORDS, \
b3395b285104 Rename the `babel.catalog` package to `babel.messages` for consistency with the other package names.
cmlenz
parents: 52
diff changeset
31 DEFAULT_MAPPING
b3395b285104 Rename the `babel.catalog` package to `babel.messages` for consistency with the other package names.
cmlenz
parents: 52
diff changeset
32 from babel.messages.pofile import write_po, write_pot
b3395b285104 Rename the `babel.catalog` package to `babel.messages` for consistency with the other package names.
cmlenz
parents: 52
diff changeset
33 from babel.messages.plurals import PLURALS
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
34
52
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
35 __all__ = ['CommandLineInterface', 'extract_messages',
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
36 'check_message_extractors', 'main']
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
37 __docformat__ = 'restructuredtext en'
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
38
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
39
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
40 class extract_messages(Command):
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
41 """Message extraction command for use in ``setup.py`` scripts.
52
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
42
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
43 If correctly installed, this command is available to Setuptools-using
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
44 setup scripts automatically. For projects using plain old ``distutils``,
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
45 the command needs to be registered explicitly in ``setup.py``::
52
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
46
54
b3395b285104 Rename the `babel.catalog` package to `babel.messages` for consistency with the other package names.
cmlenz
parents: 52
diff changeset
47 from babel.messages.frontend import extract_messages
52
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
48
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
49 setup(
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
50 ...
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
51 cmdclass = {'extract_messages': extract_messages}
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
52 )
52
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
53
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
54 :see: `Integrating new distutils commands <http://docs.python.org/dist/node32.html>`_
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
55 :see: `setuptools <http://peak.telecommunity.com/DevCenter/setuptools>`_
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
56 """
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
57
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
58 description = 'extract localizable strings from the project code'
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
59 user_options = [
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
60 ('charset=', None,
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
61 'charset to use in the output file'),
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
62 ('keywords=', 'k',
10
b24987f7318d Both Babel's [source:trunk/babel/catalog/frontend.py frontend] and [source:trunk/babel/catalog/extract.py extract] now handle keyword indices. Also added an extra boolean flag so that the default keywords defined by Babel are not included in the keywords to search for when extracting strings.
palgarvio
parents: 7
diff changeset
63 'space-separated list of keywords to look for in addition to the '
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
64 'defaults'),
10
b24987f7318d Both Babel's [source:trunk/babel/catalog/frontend.py frontend] and [source:trunk/babel/catalog/extract.py extract] now handle keyword indices. Also added an extra boolean flag so that the default keywords defined by Babel are not included in the keywords to search for when extracting strings.
palgarvio
parents: 7
diff changeset
65 ('no-default-keywords', None,
12
a2c54ef107c2 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
66 'do not include the default keywords'),
47
76381d4b3635 Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents: 24
diff changeset
67 ('mapping-file=', 'F',
76381d4b3635 Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents: 24
diff changeset
68 'path to the mapping configuration file'),
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
69 ('no-location', None,
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
70 'do not include location comments with filename and line number'),
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
71 ('omit-header', None,
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
72 'do not include msgid "" entry in header'),
5
50ad95bee876 * The creation-date header in generated PO files now includes the timezone offset.
cmlenz
parents: 1
diff changeset
73 ('output-file=', 'o',
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
74 'name of the output file'),
23
bd45ff425485 Added line-wrap support for `write_po`.
palgarvio
parents: 12
diff changeset
75 ('width=', 'w',
24
4fad20ab7cca Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents: 23
diff changeset
76 'set output line width (default 76)'),
23
bd45ff425485 Added line-wrap support for `write_po`.
palgarvio
parents: 12
diff changeset
77 ('no-wrap', None,
24
4fad20ab7cca Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents: 23
diff changeset
78 'do not break long message lines, longer than the output line width, '
4fad20ab7cca Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents: 23
diff changeset
79 'into several lines')
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
80 ]
23
bd45ff425485 Added line-wrap support for `write_po`.
palgarvio
parents: 12
diff changeset
81 boolean_options = [
bd45ff425485 Added line-wrap support for `write_po`.
palgarvio
parents: 12
diff changeset
82 'no-default-keywords', 'no-location', 'omit-header', 'no-wrap'
bd45ff425485 Added line-wrap support for `write_po`.
palgarvio
parents: 12
diff changeset
83 ]
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
84
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
85 def initialize_options(self):
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
86 self.charset = 'utf-8'
23
bd45ff425485 Added line-wrap support for `write_po`.
palgarvio
parents: 12
diff changeset
87 self.keywords = self._keywords = DEFAULT_KEYWORDS.copy()
12
a2c54ef107c2 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
88 self.no_default_keywords = False
47
76381d4b3635 Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents: 24
diff changeset
89 self.mapping_file = None
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
90 self.no_location = False
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
91 self.omit_header = False
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
92 self.output_file = None
47
76381d4b3635 Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents: 24
diff changeset
93 self.width = 76
76381d4b3635 Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents: 24
diff changeset
94 self.no_wrap = False
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
95
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
96 def finalize_options(self):
23
bd45ff425485 Added line-wrap support for `write_po`.
palgarvio
parents: 12
diff changeset
97 if self.no_default_keywords and not self.keywords:
24
4fad20ab7cca Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents: 23
diff changeset
98 raise DistutilsOptionError('you must specify new keywords if you '
4fad20ab7cca Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents: 23
diff changeset
99 'disable the default ones')
12
a2c54ef107c2 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
100 if self.no_default_keywords:
23
bd45ff425485 Added line-wrap support for `write_po`.
palgarvio
parents: 12
diff changeset
101 self._keywords = {}
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
102 if isinstance(self.keywords, basestring):
23
bd45ff425485 Added line-wrap support for `write_po`.
palgarvio
parents: 12
diff changeset
103 self._keywords.update(parse_keywords(self.keywords.split()))
bd45ff425485 Added line-wrap support for `write_po`.
palgarvio
parents: 12
diff changeset
104 self.keywords = self._keywords
24
4fad20ab7cca Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents: 23
diff changeset
105
23
bd45ff425485 Added line-wrap support for `write_po`.
palgarvio
parents: 12
diff changeset
106 if self.no_wrap and self.width:
24
4fad20ab7cca Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents: 23
diff changeset
107 raise DistutilsOptionError("'--no-wrap' and '--width' are mutually"
4fad20ab7cca Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents: 23
diff changeset
108 "exclusive")
4fad20ab7cca Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents: 23
diff changeset
109 if self.no_wrap:
4fad20ab7cca Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents: 23
diff changeset
110 self.width = None
4fad20ab7cca Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents: 23
diff changeset
111 else:
23
bd45ff425485 Added line-wrap support for `write_po`.
palgarvio
parents: 12
diff changeset
112 self.width = int(self.width)
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
113
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
114 def run(self):
47
76381d4b3635 Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents: 24
diff changeset
115 if self.mapping_file:
76381d4b3635 Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents: 24
diff changeset
116 fileobj = open(self.mapping_file, 'U')
76381d4b3635 Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents: 24
diff changeset
117 try:
76381d4b3635 Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents: 24
diff changeset
118 method_map, options_map = parse_mapping(fileobj)
76381d4b3635 Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents: 24
diff changeset
119 finally:
76381d4b3635 Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents: 24
diff changeset
120 fileobj.close()
49
daf35e2ad044 Support a `message_extractors` keyword argument directly in `setup()`. Closes #4.
cmlenz
parents: 48
diff changeset
121 elif self.distribution.message_extractors:
daf35e2ad044 Support a `message_extractors` keyword argument directly in `setup()`. Closes #4.
cmlenz
parents: 48
diff changeset
122 message_extractors = self.distribution.message_extractors
daf35e2ad044 Support a `message_extractors` keyword argument directly in `setup()`. Closes #4.
cmlenz
parents: 48
diff changeset
123 if isinstance(message_extractors, basestring):
daf35e2ad044 Support a `message_extractors` keyword argument directly in `setup()`. Closes #4.
cmlenz
parents: 48
diff changeset
124 method_map, options_map = parse_mapping(StringIO(
daf35e2ad044 Support a `message_extractors` keyword argument directly in `setup()`. Closes #4.
cmlenz
parents: 48
diff changeset
125 message_extractors
daf35e2ad044 Support a `message_extractors` keyword argument directly in `setup()`. Closes #4.
cmlenz
parents: 48
diff changeset
126 ))
daf35e2ad044 Support a `message_extractors` keyword argument directly in `setup()`. Closes #4.
cmlenz
parents: 48
diff changeset
127 else:
daf35e2ad044 Support a `message_extractors` keyword argument directly in `setup()`. Closes #4.
cmlenz
parents: 48
diff changeset
128 method_map = {}
daf35e2ad044 Support a `message_extractors` keyword argument directly in `setup()`. Closes #4.
cmlenz
parents: 48
diff changeset
129 options_map = {}
daf35e2ad044 Support a `message_extractors` keyword argument directly in `setup()`. Closes #4.
cmlenz
parents: 48
diff changeset
130 for pattern, (method, options) in message_extractors.items():
daf35e2ad044 Support a `message_extractors` keyword argument directly in `setup()`. Closes #4.
cmlenz
parents: 48
diff changeset
131 method_map[pattern] = method
daf35e2ad044 Support a `message_extractors` keyword argument directly in `setup()`. Closes #4.
cmlenz
parents: 48
diff changeset
132 options_map[pattern] = options
47
76381d4b3635 Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents: 24
diff changeset
133 else:
76381d4b3635 Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents: 24
diff changeset
134 method_map = DEFAULT_MAPPING
76381d4b3635 Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents: 24
diff changeset
135 options_map = {}
76381d4b3635 Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents: 24
diff changeset
136
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
137 outfile = open(self.output_file, 'w')
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
138 try:
47
76381d4b3635 Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents: 24
diff changeset
139 def callback(filename, options):
76381d4b3635 Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents: 24
diff changeset
140 optstr = ''
76381d4b3635 Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents: 24
diff changeset
141 if options:
76381d4b3635 Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents: 24
diff changeset
142 optstr = ' (%s)' % ', '.join(['%s="%s"' % (k, v) for k, v
76381d4b3635 Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents: 24
diff changeset
143 in options.items()])
76381d4b3635 Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents: 24
diff changeset
144 log.info('extracting messages from %s%s' % (filename, optstr))
76381d4b3635 Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents: 24
diff changeset
145
56
27fba894d3ca Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents: 55
diff changeset
146 catalog = Catalog()
47
76381d4b3635 Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents: 24
diff changeset
147 extracted = extract_from_dir(method_map=method_map,
76381d4b3635 Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents: 24
diff changeset
148 options_map=options_map,
76381d4b3635 Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents: 24
diff changeset
149 keywords=self.keywords,
76381d4b3635 Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents: 24
diff changeset
150 callback=callback)
56
27fba894d3ca Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents: 55
diff changeset
151 for filename, lineno, message in extracted:
47
76381d4b3635 Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents: 24
diff changeset
152 filepath = os.path.normpath(filename)
56
27fba894d3ca Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents: 55
diff changeset
153 catalog.add(message, None, [(filepath, lineno)])
24
4fad20ab7cca Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents: 23
diff changeset
154
51
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
155 log.info('writing PO template file to %s' % self.output_file)
56
27fba894d3ca Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents: 55
diff changeset
156 write_pot(outfile, catalog, project=self.distribution.get_name(),
24
4fad20ab7cca Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents: 23
diff changeset
157 version=self.distribution.get_version(), width=self.width,
5
50ad95bee876 * The creation-date header in generated PO files now includes the timezone offset.
cmlenz
parents: 1
diff changeset
158 charset=self.charset, no_location=self.no_location,
24
4fad20ab7cca Reimplement line wrapping for PO writing (as the `textwrap` module is too destructive with white space) and move it to the `normalize` function (which was already doing some handling of line breaks).
cmlenz
parents: 23
diff changeset
159 omit_header=self.omit_header)
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
160 finally:
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
161 outfile.close()
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
162
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
163
52
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
164 def check_message_extractors(dist, name, value):
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
165 """Validate the ``message_extractors`` keyword argument to ``setup()``.
47
76381d4b3635 Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents: 24
diff changeset
166
52
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
167 :param dist: the distutils/setuptools ``Distribution`` object
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
168 :param name: the name of the keyword argument (should always be
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
169 "message_extractors")
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
170 :param value: the value of the keyword argument
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
171 :raise `DistutilsSetupError`: if the value is not valid
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
172 :see: `Adding setup() arguments
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
173 <http://peak.telecommunity.com/DevCenter/setuptools#adding-setup-arguments>`_
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
174 """
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
175 assert name == 'message_extractors'
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
176 if not isinstance(value, (basestring, dict)):
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
177 raise DistutilsSetupError('the value of the "extract_messages" '
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
178 'parameter must be a string or dictionary')
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
179
52
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
180
51
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
181 class new_catalog(Command):
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
182 """New catalog command for use in ``setup.py`` scripts.
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
183
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
184 If correctly installed, this command is available to Setuptools-using
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
185 setup scripts automatically. For projects using plain old ``distutils``,
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
186 the command needs to be registered explicitly in ``setup.py``::
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
187
54
b3395b285104 Rename the `babel.catalog` package to `babel.messages` for consistency with the other package names.
cmlenz
parents: 52
diff changeset
188 from babel.messages.frontend import new_catalog
51
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
189
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
190 setup(
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
191 ...
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
192 cmdclass = {'new_catalog': new_catalog}
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
193 )
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
194
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
195 :see: `Integrating new distutils commands <http://docs.python.org/dist/node32.html>`_
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
196 :see: `setuptools <http://peak.telecommunity.com/DevCenter/setuptools>`_
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
197 """
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
198
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
199 description = 'create new catalogs based on a catalog template'
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
200 user_options = [
55
c3291ad6b010 `new_catalog` now accepts another argument, `--domain`, which is used to build the output file path, which now is of the form `<output_dir>/<locale>/<domain>.po`, the correct form.
palgarvio
parents: 54
diff changeset
201 ('domain=', 'D',
c3291ad6b010 `new_catalog` now accepts another argument, `--domain`, which is used to build the output file path, which now is of the form `<output_dir>/<locale>/<domain>.po`, the correct form.
palgarvio
parents: 54
diff changeset
202 "domain of PO file (defaults to lower-cased project name"),
51
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
203 ('input-file=', 'i',
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
204 'name of the input file'),
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
205 ('output-dir=', 'd',
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
206 'path to output directory'),
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
207 ('output-file=', 'o',
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
208 "name of the output file (default "
55
c3291ad6b010 `new_catalog` now accepts another argument, `--domain`, which is used to build the output file path, which now is of the form `<output_dir>/<locale>/<domain>.po`, the correct form.
palgarvio
parents: 54
diff changeset
209 "'<output_dir>/<locale>/<domain>.po')"),
51
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
210 ('locale=', 'l',
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
211 'locale for the new localized catalog'),
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
212 ('first-author=', None,
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
213 'name of first author'),
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
214 ('first-author-email=', None,
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
215 'email of first author')
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
216 ]
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
217
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
218 def initialize_options(self):
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
219 self.output_dir = None
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
220 self.output_file = None
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
221 self.input_file = None
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
222 self.locale = None
55
c3291ad6b010 `new_catalog` now accepts another argument, `--domain`, which is used to build the output file path, which now is of the form `<output_dir>/<locale>/<domain>.po`, the correct form.
palgarvio
parents: 54
diff changeset
223 self.domain = None
51
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
224 self.first_author = None
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
225 self.first_author_email = None
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
226
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
227 def finalize_options(self):
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
228 if not self.input_file:
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
229 raise DistutilsOptionError('you must specify the input file')
55
c3291ad6b010 `new_catalog` now accepts another argument, `--domain`, which is used to build the output file path, which now is of the form `<output_dir>/<locale>/<domain>.po`, the correct form.
palgarvio
parents: 54
diff changeset
230
c3291ad6b010 `new_catalog` now accepts another argument, `--domain`, which is used to build the output file path, which now is of the form `<output_dir>/<locale>/<domain>.po`, the correct form.
palgarvio
parents: 54
diff changeset
231 if not self.domain:
c3291ad6b010 `new_catalog` now accepts another argument, `--domain`, which is used to build the output file path, which now is of the form `<output_dir>/<locale>/<domain>.po`, the correct form.
palgarvio
parents: 54
diff changeset
232 self.domain = self.distribution.get_name().lower()
52
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
233
51
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
234 if not self.locale:
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
235 raise DistutilsOptionError('you must provide a locale for the '
55
c3291ad6b010 `new_catalog` now accepts another argument, `--domain`, which is used to build the output file path, which now is of the form `<output_dir>/<locale>/<domain>.po`, the correct form.
palgarvio
parents: 54
diff changeset
236 'new catalog')
51
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
237 else:
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
238 try:
55
c3291ad6b010 `new_catalog` now accepts another argument, `--domain`, which is used to build the output file path, which now is of the form `<output_dir>/<locale>/<domain>.po`, the correct form.
palgarvio
parents: 54
diff changeset
239 self._locale = Locale.parse(self.locale)
51
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
240 except UnknownLocaleError, error:
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
241 log.error(error)
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
242 sys.exit(1)
52
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
243
55
c3291ad6b010 `new_catalog` now accepts another argument, `--domain`, which is used to build the output file path, which now is of the form `<output_dir>/<locale>/<domain>.po`, the correct form.
palgarvio
parents: 54
diff changeset
244 if self._locale.territory.lower() == self._locale.language:
c3291ad6b010 `new_catalog` now accepts another argument, `--domain`, which is used to build the output file path, which now is of the form `<output_dir>/<locale>/<domain>.po`, the correct form.
palgarvio
parents: 54
diff changeset
245 # Remove country part if equal to language
c3291ad6b010 `new_catalog` now accepts another argument, `--domain`, which is used to build the output file path, which now is of the form `<output_dir>/<locale>/<domain>.po`, the correct form.
palgarvio
parents: 54
diff changeset
246 # XXX: This might not be the best behaviour, investigate
c3291ad6b010 `new_catalog` now accepts another argument, `--domain`, which is used to build the output file path, which now is of the form `<output_dir>/<locale>/<domain>.po`, the correct form.
palgarvio
parents: 54
diff changeset
247 self.locale = self._locale.language
52
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
248
51
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
249 if not self.output_file and not self.output_dir:
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
250 raise DistutilsOptionError('you must specify the output directory')
52
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
251
51
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
252 if not self.output_file and self.output_dir:
55
c3291ad6b010 `new_catalog` now accepts another argument, `--domain`, which is used to build the output file path, which now is of the form `<output_dir>/<locale>/<domain>.po`, the correct form.
palgarvio
parents: 54
diff changeset
253 self.output_file = os.path.join(self.output_dir,
c3291ad6b010 `new_catalog` now accepts another argument, `--domain`, which is used to build the output file path, which now is of the form `<output_dir>/<locale>/<domain>.po`, the correct form.
palgarvio
parents: 54
diff changeset
254 self.locale,
c3291ad6b010 `new_catalog` now accepts another argument, `--domain`, which is used to build the output file path, which now is of the form `<output_dir>/<locale>/<domain>.po`, the correct form.
palgarvio
parents: 54
diff changeset
255 self.domain + '.po')
c3291ad6b010 `new_catalog` now accepts another argument, `--domain`, which is used to build the output file path, which now is of the form `<output_dir>/<locale>/<domain>.po`, the correct form.
palgarvio
parents: 54
diff changeset
256 if not os.path.exists(os.path.dirname(self.output_file)):
c3291ad6b010 `new_catalog` now accepts another argument, `--domain`, which is used to build the output file path, which now is of the form `<output_dir>/<locale>/<domain>.po`, the correct form.
palgarvio
parents: 54
diff changeset
257 os.makedirs(os.path.dirname(self.output_file))
51
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
258
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
259 def run(self):
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
260 outfile = open(self.output_file, 'w')
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
261 infile = open(self.input_file, 'r')
52
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
262
55
c3291ad6b010 `new_catalog` now accepts another argument, `--domain`, which is used to build the output file path, which now is of the form `<output_dir>/<locale>/<domain>.po`, the correct form.
palgarvio
parents: 54
diff changeset
263 if PLURALS.has_key(str(self._locale)):
c3291ad6b010 `new_catalog` now accepts another argument, `--domain`, which is used to build the output file path, which now is of the form `<output_dir>/<locale>/<domain>.po`, the correct form.
palgarvio
parents: 54
diff changeset
264 # Try <language>_<COUNTRY> if passed by user
c3291ad6b010 `new_catalog` now accepts another argument, `--domain`, which is used to build the output file path, which now is of the form `<output_dir>/<locale>/<domain>.po`, the correct form.
palgarvio
parents: 54
diff changeset
265 plurals = PLURALS[str(self._locale)]
c3291ad6b010 `new_catalog` now accepts another argument, `--domain`, which is used to build the output file path, which now is of the form `<output_dir>/<locale>/<domain>.po`, the correct form.
palgarvio
parents: 54
diff changeset
266 elif PLURALS.has_key(self._locale.language):
51
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
267 # Try <language>
55
c3291ad6b010 `new_catalog` now accepts another argument, `--domain`, which is used to build the output file path, which now is of the form `<output_dir>/<locale>/<domain>.po`, the correct form.
palgarvio
parents: 54
diff changeset
268 plurals = PLURALS[self._locale.language]
51
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
269 else:
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
270 plurals = ('INTEGER', 'EXPRESSION')
52
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
271
55
c3291ad6b010 `new_catalog` now accepts another argument, `--domain`, which is used to build the output file path, which now is of the form `<output_dir>/<locale>/<domain>.po`, the correct form.
palgarvio
parents: 54
diff changeset
272 log.info('Creating %s %r PO from %r PO template',
c3291ad6b010 `new_catalog` now accepts another argument, `--domain`, which is used to build the output file path, which now is of the form `<output_dir>/<locale>/<domain>.po`, the correct form.
palgarvio
parents: 54
diff changeset
273 self._locale.english_name,
c3291ad6b010 `new_catalog` now accepts another argument, `--domain`, which is used to build the output file path, which now is of the form `<output_dir>/<locale>/<domain>.po`, the correct form.
palgarvio
parents: 54
diff changeset
274 self.output_file,
c3291ad6b010 `new_catalog` now accepts another argument, `--domain`, which is used to build the output file path, which now is of the form `<output_dir>/<locale>/<domain>.po`, the correct form.
palgarvio
parents: 54
diff changeset
275 self.input_file)
52
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
276
55
c3291ad6b010 `new_catalog` now accepts another argument, `--domain`, which is used to build the output file path, which now is of the form `<output_dir>/<locale>/<domain>.po`, the correct form.
palgarvio
parents: 54
diff changeset
277 write_po(outfile, infile, self._locale,
51
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
278 project=self.distribution.get_name(),
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
279 version=self.distribution.get_version(),
55
c3291ad6b010 `new_catalog` now accepts another argument, `--domain`, which is used to build the output file path, which now is of the form `<output_dir>/<locale>/<domain>.po`, the correct form.
palgarvio
parents: 54
diff changeset
280 plurals=plurals,
51
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
281 first_author=self.first_author,
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
282 first_author_email=self.first_author_email)
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
283 infile.close()
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
284 outfile.close()
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
285
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
286
52
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
287 class CommandLineInterface(object):
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
288 """Command-line interface.
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
289
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
290 This class provides a simple command-line interface to the message
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
291 extraction and PO file generation functionality.
51
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
292 """
52
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
293
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
294 usage = '%%prog %s [options] %s'
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
295 version = '%%prog %s' % VERSION
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
296 commands = ['extract', 'init']
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
297
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
298 def run(self, argv=sys.argv):
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
299 """Main entry point of the command-line interface.
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
300
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
301 :param argv: list of arguments passed on the command-line
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
302 """
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
303 parser = OptionParser(usage=self.usage % ('subcommand', '[args]'),
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
304 version=self.version)
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
305 parser.disable_interspersed_args()
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
306 options, args = parser.parse_args(argv[1:])
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
307 if not args:
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
308 parser.error('incorrect number of arguments')
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
309
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
310 cmdname = args[0]
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
311 if cmdname not in self.commands:
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
312 parser.error('unknown subcommand "%s"' % cmdname)
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
313
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
314 getattr(self, cmdname)(args[1:])
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
315
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
316 def extract(self, argv):
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
317 """Subcommand for extracting messages from source files and generating
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
318 a POT file.
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
319
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
320 :param argv: the command arguments
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
321 """
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
322 parser = OptionParser(usage=self.usage % ('extract', 'dir1 <dir2> ...'))
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
323 parser.add_option('--charset', dest='charset',
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
324 help='charset to use in the output')
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
325 parser.add_option('-k', '--keyword', dest='keywords', action='append',
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
326 help='keywords to look for in addition to the '
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
327 'defaults. You can specify multiple -k flags on '
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
328 'the command line.')
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
329 parser.add_option('--no-default-keywords', dest='no_default_keywords',
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
330 action='store_true',
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
331 help="do not include the default keywords")
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
332 parser.add_option('--mapping', '-F', dest='mapping_file',
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
333 help='path to the extraction mapping file')
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
334 parser.add_option('--no-location', dest='no_location',
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
335 action='store_true',
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
336 help='do not include location comments with filename '
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
337 'and line number')
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
338 parser.add_option('--omit-header', dest='omit_header',
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
339 action='store_true',
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
340 help='do not include msgid "" entry in header')
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
341 parser.add_option('-o', '--output', dest='output',
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
342 help='path to the output POT file')
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
343 parser.add_option('-w', '--width', dest='width', type='int',
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
344 help="set output line width (default %default)")
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
345 parser.add_option('--no-wrap', dest='no_wrap', action = 'store_true',
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
346 help='do not break long message lines, longer than '
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
347 'the output line width, into several lines')
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
348
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
349 parser.set_defaults(charset='utf-8', keywords=[],
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
350 no_default_keywords=False, no_location=False,
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
351 omit_header = False, width=76, no_wrap=False)
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
352 options, args = parser.parse_args(argv)
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
353 if not args:
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
354 parser.error('incorrect number of arguments')
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
355
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
356 if options.output not in (None, '-'):
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
357 outfile = open(options.output, 'w')
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
358 else:
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
359 outfile = sys.stdout
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
360
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
361 keywords = DEFAULT_KEYWORDS.copy()
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
362 if options.no_default_keywords:
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
363 if not options.keywords:
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
364 parser.error('you must specify new keywords if you disable the '
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
365 'default ones')
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
366 keywords = {}
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
367 if options.keywords:
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
368 keywords.update(parse_keywords(options.keywords))
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
369
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
370 if options.mapping_file:
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
371 fileobj = open(options.mapping_file, 'U')
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
372 try:
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
373 method_map, options_map = parse_mapping(fileobj)
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
374 finally:
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
375 fileobj.close()
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
376 else:
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
377 method_map = DEFAULT_MAPPING
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
378 options_map = {}
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
379
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
380 if options.width and options.no_wrap:
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
381 parser.error("'--no-wrap' and '--width' are mutually exclusive.")
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
382 elif not options.width and not options.no_wrap:
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
383 options.width = 76
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
384 elif not options.width and options.no_wrap:
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
385 options.width = 0
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
386
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
387 try:
56
27fba894d3ca Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents: 55
diff changeset
388 catalog = Catalog()
52
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
389 for dirname in args:
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
390 if not os.path.isdir(dirname):
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
391 parser.error('%r is not a directory' % dirname)
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
392 extracted = extract_from_dir(dirname, method_map, options_map,
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
393 keywords)
56
27fba894d3ca Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents: 55
diff changeset
394 for filename, lineno, message in extracted:
52
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
395 filepath = os.path.normpath(os.path.join(dirname, filename))
56
27fba894d3ca Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents: 55
diff changeset
396 catalog.add(message, None, [(filepath, lineno)])
27fba894d3ca Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents: 55
diff changeset
397
27fba894d3ca Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents: 55
diff changeset
398 write_pot(outfile, catalog, width=options.width,
54
b3395b285104 Rename the `babel.catalog` package to `babel.messages` for consistency with the other package names.
cmlenz
parents: 52
diff changeset
399 charset=options.charset, no_location=options.no_location,
b3395b285104 Rename the `babel.catalog` package to `babel.messages` for consistency with the other package names.
cmlenz
parents: 52
diff changeset
400 omit_header=options.omit_header)
52
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
401 finally:
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
402 if options.output:
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
403 outfile.close()
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
404
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
405 def init(self, argv):
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
406 """Subcommand for creating new message catalogs from a template.
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
407
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
408 :param argv: the command arguments
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
409 """
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
410 raise NotImplementedError
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
411
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
412 def main():
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
413 CommandLineInterface().run(sys.argv)
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
414
48
bd647e3760e0 Move the mapping configuration file format to `ConfigParser`, and add some more documentation about it.
cmlenz
parents: 47
diff changeset
415 def parse_mapping(fileobj, filename=None):
47
76381d4b3635 Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents: 24
diff changeset
416 """Parse an extraction method mapping from a file-like object.
52
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
417
47
76381d4b3635 Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents: 24
diff changeset
418 >>> buf = StringIO('''
76381d4b3635 Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents: 24
diff changeset
419 ... # Python source files
48
bd647e3760e0 Move the mapping configuration file format to `ConfigParser`, and add some more documentation about it.
cmlenz
parents: 47
diff changeset
420 ... [python: foobar/**.py]
52
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
421 ...
47
76381d4b3635 Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents: 24
diff changeset
422 ... # Genshi templates
48
bd647e3760e0 Move the mapping configuration file format to `ConfigParser`, and add some more documentation about it.
cmlenz
parents: 47
diff changeset
423 ... [genshi: foobar/**/templates/**.html]
bd647e3760e0 Move the mapping configuration file format to `ConfigParser`, and add some more documentation about it.
cmlenz
parents: 47
diff changeset
424 ... include_attrs =
bd647e3760e0 Move the mapping configuration file format to `ConfigParser`, and add some more documentation about it.
cmlenz
parents: 47
diff changeset
425 ... [genshi: foobar/**/templates/**.txt]
bd647e3760e0 Move the mapping configuration file format to `ConfigParser`, and add some more documentation about it.
cmlenz
parents: 47
diff changeset
426 ... template_class = genshi.template.text.TextTemplate
bd647e3760e0 Move the mapping configuration file format to `ConfigParser`, and add some more documentation about it.
cmlenz
parents: 47
diff changeset
427 ... encoding = latin-1
47
76381d4b3635 Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents: 24
diff changeset
428 ... ''')
52
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
429
47
76381d4b3635 Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents: 24
diff changeset
430 >>> method_map, options_map = parse_mapping(buf)
52
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
431
47
76381d4b3635 Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents: 24
diff changeset
432 >>> method_map['foobar/**.py']
76381d4b3635 Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents: 24
diff changeset
433 'python'
76381d4b3635 Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents: 24
diff changeset
434 >>> options_map['foobar/**.py']
76381d4b3635 Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents: 24
diff changeset
435 {}
76381d4b3635 Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents: 24
diff changeset
436 >>> method_map['foobar/**/templates/**.html']
76381d4b3635 Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents: 24
diff changeset
437 'genshi'
76381d4b3635 Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents: 24
diff changeset
438 >>> options_map['foobar/**/templates/**.html']['include_attrs']
76381d4b3635 Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents: 24
diff changeset
439 ''
76381d4b3635 Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents: 24
diff changeset
440 >>> method_map['foobar/**/templates/**.txt']
76381d4b3635 Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents: 24
diff changeset
441 'genshi'
76381d4b3635 Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents: 24
diff changeset
442 >>> options_map['foobar/**/templates/**.txt']['template_class']
76381d4b3635 Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents: 24
diff changeset
443 'genshi.template.text.TextTemplate'
76381d4b3635 Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents: 24
diff changeset
444 >>> options_map['foobar/**/templates/**.txt']['encoding']
76381d4b3635 Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents: 24
diff changeset
445 'latin-1'
52
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
446
47
76381d4b3635 Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents: 24
diff changeset
447 :param fileobj: a readable file-like object containing the configuration
76381d4b3635 Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents: 24
diff changeset
448 text to parse
76381d4b3635 Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents: 24
diff changeset
449 :return: a `(method_map, options_map)` tuple
76381d4b3635 Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents: 24
diff changeset
450 :rtype: `tuple`
76381d4b3635 Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents: 24
diff changeset
451 :see: `extract_from_directory`
76381d4b3635 Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents: 24
diff changeset
452 """
76381d4b3635 Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents: 24
diff changeset
453 method_map = {}
76381d4b3635 Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents: 24
diff changeset
454 options_map = {}
76381d4b3635 Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents: 24
diff changeset
455
48
bd647e3760e0 Move the mapping configuration file format to `ConfigParser`, and add some more documentation about it.
cmlenz
parents: 47
diff changeset
456 parser = RawConfigParser()
bd647e3760e0 Move the mapping configuration file format to `ConfigParser`, and add some more documentation about it.
cmlenz
parents: 47
diff changeset
457 parser.readfp(fileobj, filename)
bd647e3760e0 Move the mapping configuration file format to `ConfigParser`, and add some more documentation about it.
cmlenz
parents: 47
diff changeset
458 for section in parser.sections():
bd647e3760e0 Move the mapping configuration file format to `ConfigParser`, and add some more documentation about it.
cmlenz
parents: 47
diff changeset
459 method, pattern = [part.strip() for part in section.split(':', 1)]
bd647e3760e0 Move the mapping configuration file format to `ConfigParser`, and add some more documentation about it.
cmlenz
parents: 47
diff changeset
460 method_map[pattern] = method
bd647e3760e0 Move the mapping configuration file format to `ConfigParser`, and add some more documentation about it.
cmlenz
parents: 47
diff changeset
461 options_map[pattern] = dict(parser.items(section))
47
76381d4b3635 Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents: 24
diff changeset
462
76381d4b3635 Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents: 24
diff changeset
463 return (method_map, options_map)
76381d4b3635 Support passing extraction method mapping and options from the frontends (see #4). No distutils/setuptools keyword supported yet, but the rest seems to be working okay.
cmlenz
parents: 24
diff changeset
464
12
a2c54ef107c2 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
465 def parse_keywords(strings=[]):
a2c54ef107c2 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
466 """Parse keywords specifications from the given list of strings.
52
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
467
12
a2c54ef107c2 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
468 >>> kw = parse_keywords(['_', 'dgettext:2', 'dngettext:2,3'])
a2c54ef107c2 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
469 >>> for keyword, indices in sorted(kw.items()):
a2c54ef107c2 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
470 ... print (keyword, indices)
a2c54ef107c2 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
471 ('_', None)
a2c54ef107c2 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
472 ('dgettext', (2,))
a2c54ef107c2 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
473 ('dngettext', (2, 3))
a2c54ef107c2 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
474 """
a2c54ef107c2 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
475 keywords = {}
a2c54ef107c2 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
476 for string in strings:
a2c54ef107c2 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
477 if ':' in string:
a2c54ef107c2 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
478 funcname, indices = string.split(':')
a2c54ef107c2 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
479 else:
a2c54ef107c2 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
480 funcname, indices = string, None
a2c54ef107c2 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
481 if funcname not in keywords:
a2c54ef107c2 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
482 if indices:
a2c54ef107c2 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
483 indices = tuple([(int(x)) for x in indices.split(',')])
a2c54ef107c2 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
484 keywords[funcname] = indices
a2c54ef107c2 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
485 return keywords
a2c54ef107c2 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
486
52
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
487
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
488 if __name__ == '__main__':
55
c3291ad6b010 `new_catalog` now accepts another argument, `--domain`, which is used to build the output file path, which now is of the form `<output_dir>/<locale>/<domain>.po`, the correct form.
palgarvio
parents: 54
diff changeset
489 main()
Copyright (C) 2012-2017 Edgewall Software