annotate babel/messages/frontend.py @ 86:9d539a4dc502

Fixed a bug on the CLI frontend introduced on [81]. Thanks pjenvey!
author palgarvio
date Sun, 10 Jun 2007 22:41:50 +0000
parents 4ff9cc26c11b
children f259182f9baf
rev   line source
62
84d400066b71 The order of extraction methods is now preserved (see #10).
cmlenz
parents: 59
diff changeset
1 #!/usr/bin/env python
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
2 # -*- coding: utf-8 -*-
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
3 #
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
4 # Copyright (C) 2007 Edgewall Software
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
5 # All rights reserved.
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
6 #
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
7 # This software is licensed as described in the file COPYING, which
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
8 # you should have received as part of this distribution. The terms
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
9 # are also available at http://babel.edgewall.org/wiki/License.
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
10 #
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
11 # This software consists of voluntary contributions made by many
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
12 # individuals. For the exact contribution history, see the revision
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
13 # history and logs, available at http://babel.edgewall.org/log/.
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
14
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
15 """Frontends for the message extraction functionality."""
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
16
48
bd647e3760e0 Move the mapping configuration file format to `ConfigParser`, and add some more documentation about it.
cmlenz
parents: 47
diff changeset
17 from ConfigParser import RawConfigParser
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
18 from distutils import log
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
19 from distutils.cmd import Command
49
daf35e2ad044 Support a `message_extractors` keyword argument directly in `setup()`. Closes #4.
cmlenz
parents: 48
diff changeset
20 from distutils.errors import DistutilsOptionError, DistutilsSetupError
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
21 from optparse import OptionParser
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
22 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
23 import re
49
daf35e2ad044 Support a `message_extractors` keyword argument directly in `setup()`. Closes #4.
cmlenz
parents: 48
diff changeset
24 from StringIO import StringIO
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
25 import sys
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
26
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
27 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
28 from babel import Locale
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
29 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
30 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
31 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
32 DEFAULT_MAPPING
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.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
34 from babel.messages.plurals import PLURALS
62
84d400066b71 The order of extraction methods is now preserved (see #10).
cmlenz
parents: 59
diff changeset
35 from babel.util import odict
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
36
52
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
37 __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
38 'check_message_extractors', 'main']
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
39 __docformat__ = 'restructuredtext en'
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
40
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
41
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
42 class extract_messages(Command):
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
43 """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
44
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
45 If correctly installed, this command is available to Setuptools-using
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
46 setup scripts automatically. For projects using plain old ``distutils``,
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
47 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
48
54
b3395b285104 Rename the `babel.catalog` package to `babel.messages` for consistency with the other package names.
cmlenz
parents: 52
diff changeset
49 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
50
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
51 setup(
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
52 ...
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
53 cmdclass = {'extract_messages': extract_messages}
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
54 )
52
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
55
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
56 :see: `Integrating new distutils commands <http://docs.python.org/dist/node32.html>`_
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
57 :see: `setuptools <http://peak.telecommunity.com/DevCenter/setuptools>`_
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
58 """
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
59
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
60 description = 'extract localizable strings from the project code'
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
61 user_options = [
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
62 ('charset=', None,
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
63 'charset to use in the output file'),
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
64 ('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
65 'space-separated list of keywords to look for in addition to the '
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
66 '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
67 ('no-default-keywords', None,
12
a2c54ef107c2 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
68 '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
69 ('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
70 'path to the mapping configuration file'),
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
71 ('no-location', None,
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
72 'do not include location comments with filename and line number'),
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
73 ('omit-header', None,
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
74 '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
75 ('output-file=', 'o',
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
76 'name of the output file'),
23
bd45ff425485 Added line-wrap support for `write_po`.
palgarvio
parents: 12
diff changeset
77 ('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
78 'set output line width (default 76)'),
23
bd45ff425485 Added line-wrap support for `write_po`.
palgarvio
parents: 12
diff changeset
79 ('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
80 'do not break long message lines, longer than the output line width, '
58
dcd8ceccf416 Fix typo in [58].
cmlenz
parents: 57
diff changeset
81 'into several lines'),
71
ea4cb904df8f Implemented message sorting, see #7.
palgarvio
parents: 66
diff changeset
82 ('sort-output', None,
ea4cb904df8f Implemented message sorting, see #7.
palgarvio
parents: 66
diff changeset
83 'generate sorted output (default False)'),
ea4cb904df8f Implemented message sorting, see #7.
palgarvio
parents: 66
diff changeset
84 ('sort-by-file', None,
ea4cb904df8f Implemented message sorting, see #7.
palgarvio
parents: 66
diff changeset
85 'sort output by file location (default False)'),
78
ee043bb666f0 Fixed the plurals header on `Catalog` which should only be written if it's not a catalog template.
palgarvio
parents: 71
diff changeset
86 ('msgid-bugs-address=', None,
ee043bb666f0 Fixed the plurals header on `Catalog` which should only be written if it's not a catalog template.
palgarvio
parents: 71
diff changeset
87 'set report address for msgid'),
79
9a05230571f8 Implemented item 4 from #12. Set the copyright holder in the output.
palgarvio
parents: 78
diff changeset
88 ('copyright-holder=', None,
9a05230571f8 Implemented item 4 from #12. Set the copyright holder in the output.
palgarvio
parents: 78
diff changeset
89 'set copyright holder in output'),
80
9c84b9fa5d30 Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents: 79
diff changeset
90 ('add-comments=', 'c',
9c84b9fa5d30 Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents: 79
diff changeset
91 'place comment block with TAG (or those preceding keyword lines) in '
9c84b9fa5d30 Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents: 79
diff changeset
92 'output file. Seperate multiple TAGs with commas(,)'),
59
5b5d35117a73 Fix 2nd typo of [58].
palgarvio
parents: 58
diff changeset
93 ('input-dirs=', None,
57
a6183d300a6e * The `extract_messages` distutils command now operators on configurable input directories again, instead of the complete current directory. The `input_dirs` default to the package directories.
cmlenz
parents: 56
diff changeset
94 'directories that should be scanned for messages'),
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
95 ]
23
bd45ff425485 Added line-wrap support for `write_po`.
palgarvio
parents: 12
diff changeset
96 boolean_options = [
71
ea4cb904df8f Implemented message sorting, see #7.
palgarvio
parents: 66
diff changeset
97 'no-default-keywords', 'no-location', 'omit-header', 'no-wrap',
ea4cb904df8f Implemented message sorting, see #7.
palgarvio
parents: 66
diff changeset
98 'sort-output', 'sort-by-file'
23
bd45ff425485 Added line-wrap support for `write_po`.
palgarvio
parents: 12
diff changeset
99 ]
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
100
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
101 def initialize_options(self):
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
102 self.charset = 'utf-8'
23
bd45ff425485 Added line-wrap support for `write_po`.
palgarvio
parents: 12
diff changeset
103 self.keywords = self._keywords = DEFAULT_KEYWORDS.copy()
12
a2c54ef107c2 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
104 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
105 self.mapping_file = None
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
106 self.no_location = False
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
107 self.omit_header = False
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
108 self.output_file = None
57
a6183d300a6e * The `extract_messages` distutils command now operators on configurable input directories again, instead of the complete current directory. The `input_dirs` default to the package directories.
cmlenz
parents: 56
diff changeset
109 self.input_dirs = 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
110 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
111 self.no_wrap = False
71
ea4cb904df8f Implemented message sorting, see #7.
palgarvio
parents: 66
diff changeset
112 self.sort_output = False
ea4cb904df8f Implemented message sorting, see #7.
palgarvio
parents: 66
diff changeset
113 self.sort_by_file = False
78
ee043bb666f0 Fixed the plurals header on `Catalog` which should only be written if it's not a catalog template.
palgarvio
parents: 71
diff changeset
114 self.msgid_bugs_address = None
79
9a05230571f8 Implemented item 4 from #12. Set the copyright holder in the output.
palgarvio
parents: 78
diff changeset
115 self.copyright_holder = None
80
9c84b9fa5d30 Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents: 79
diff changeset
116 self.add_comments = None
9c84b9fa5d30 Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents: 79
diff changeset
117 self._add_comments = None
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
118
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
119 def finalize_options(self):
23
bd45ff425485 Added line-wrap support for `write_po`.
palgarvio
parents: 12
diff changeset
120 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
121 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
122 'disable the default ones')
12
a2c54ef107c2 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
123 if self.no_default_keywords:
23
bd45ff425485 Added line-wrap support for `write_po`.
palgarvio
parents: 12
diff changeset
124 self._keywords = {}
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
125 if isinstance(self.keywords, basestring):
23
bd45ff425485 Added line-wrap support for `write_po`.
palgarvio
parents: 12
diff changeset
126 self._keywords.update(parse_keywords(self.keywords.split()))
bd45ff425485 Added line-wrap support for `write_po`.
palgarvio
parents: 12
diff changeset
127 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
128
23
bd45ff425485 Added line-wrap support for `write_po`.
palgarvio
parents: 12
diff changeset
129 if self.no_wrap and self.width:
71
ea4cb904df8f Implemented message sorting, see #7.
palgarvio
parents: 66
diff changeset
130 raise DistutilsOptionError("'--no-wrap' and '--width' are mutually "
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
131 "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
132 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
133 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
134 else:
23
bd45ff425485 Added line-wrap support for `write_po`.
palgarvio
parents: 12
diff changeset
135 self.width = int(self.width)
71
ea4cb904df8f Implemented message sorting, see #7.
palgarvio
parents: 66
diff changeset
136
ea4cb904df8f Implemented message sorting, see #7.
palgarvio
parents: 66
diff changeset
137 if self.sort_output and self.sort_by_file:
ea4cb904df8f Implemented message sorting, see #7.
palgarvio
parents: 66
diff changeset
138 raise DistutilsOptionError("'--sort-output' and '--sort-by-file' "
ea4cb904df8f Implemented message sorting, see #7.
palgarvio
parents: 66
diff changeset
139 "are mutually exclusive")
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
140
57
a6183d300a6e * The `extract_messages` distutils command now operators on configurable input directories again, instead of the complete current directory. The `input_dirs` default to the package directories.
cmlenz
parents: 56
diff changeset
141 if not self.input_dirs:
a6183d300a6e * The `extract_messages` distutils command now operators on configurable input directories again, instead of the complete current directory. The `input_dirs` default to the package directories.
cmlenz
parents: 56
diff changeset
142 self.input_dirs = dict.fromkeys([k.split('.',1)[0]
a6183d300a6e * The `extract_messages` distutils command now operators on configurable input directories again, instead of the complete current directory. The `input_dirs` default to the package directories.
cmlenz
parents: 56
diff changeset
143 for k in self.distribution.packages
a6183d300a6e * The `extract_messages` distutils command now operators on configurable input directories again, instead of the complete current directory. The `input_dirs` default to the package directories.
cmlenz
parents: 56
diff changeset
144 ]).keys()
80
9c84b9fa5d30 Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents: 79
diff changeset
145
9c84b9fa5d30 Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents: 79
diff changeset
146 if self.add_comments:
9c84b9fa5d30 Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents: 79
diff changeset
147 self._add_comments = self.add_comments.split(',')
57
a6183d300a6e * The `extract_messages` distutils command now operators on configurable input directories again, instead of the complete current directory. The `input_dirs` default to the package directories.
cmlenz
parents: 56
diff changeset
148
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
149 def run(self):
62
84d400066b71 The order of extraction methods is now preserved (see #10).
cmlenz
parents: 59
diff changeset
150 mappings = self._get_mappings()
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
151 outfile = open(self.output_file, 'w')
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
152 try:
78
ee043bb666f0 Fixed the plurals header on `Catalog` which should only be written if it's not a catalog template.
palgarvio
parents: 71
diff changeset
153 catalog = Catalog(msgid_bugs_address=self.msgid_bugs_address)
62
84d400066b71 The order of extraction methods is now preserved (see #10).
cmlenz
parents: 59
diff changeset
154 for dirname, (method_map, options_map) in mappings.items():
57
a6183d300a6e * The `extract_messages` distutils command now operators on configurable input directories again, instead of the complete current directory. The `input_dirs` default to the package directories.
cmlenz
parents: 56
diff changeset
155 def callback(filename, method, options):
a6183d300a6e * The `extract_messages` distutils command now operators on configurable input directories again, instead of the complete current directory. The `input_dirs` default to the package directories.
cmlenz
parents: 56
diff changeset
156 if method == 'ignore':
a6183d300a6e * The `extract_messages` distutils command now operators on configurable input directories again, instead of the complete current directory. The `input_dirs` default to the package directories.
cmlenz
parents: 56
diff changeset
157 return
a6183d300a6e * The `extract_messages` distutils command now operators on configurable input directories again, instead of the complete current directory. The `input_dirs` default to the package directories.
cmlenz
parents: 56
diff changeset
158 filepath = os.path.normpath(os.path.join(dirname, filename))
a6183d300a6e * The `extract_messages` distutils command now operators on configurable input directories again, instead of the complete current directory. The `input_dirs` default to the package directories.
cmlenz
parents: 56
diff changeset
159 optstr = ''
a6183d300a6e * The `extract_messages` distutils command now operators on configurable input directories again, instead of the complete current directory. The `input_dirs` default to the package directories.
cmlenz
parents: 56
diff changeset
160 if options:
a6183d300a6e * The `extract_messages` distutils command now operators on configurable input directories again, instead of the complete current directory. The `input_dirs` default to the package directories.
cmlenz
parents: 56
diff changeset
161 optstr = ' (%s)' % ', '.join(['%s="%s"' % (k, v) for
a6183d300a6e * The `extract_messages` distutils command now operators on configurable input directories again, instead of the complete current directory. The `input_dirs` default to the package directories.
cmlenz
parents: 56
diff changeset
162 k, v in options.items()])
a6183d300a6e * The `extract_messages` distutils command now operators on configurable input directories again, instead of the complete current directory. The `input_dirs` default to the package directories.
cmlenz
parents: 56
diff changeset
163 log.info('extracting messages from %s%s'
a6183d300a6e * The `extract_messages` distutils command now operators on configurable input directories again, instead of the complete current directory. The `input_dirs` default to the package directories.
cmlenz
parents: 56
diff changeset
164 % (filepath, optstr))
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
165
57
a6183d300a6e * The `extract_messages` distutils command now operators on configurable input directories again, instead of the complete current directory. The `input_dirs` default to the package directories.
cmlenz
parents: 56
diff changeset
166 extracted = extract_from_dir(dirname, method_map, options_map,
a6183d300a6e * The `extract_messages` distutils command now operators on configurable input directories again, instead of the complete current directory. The `input_dirs` default to the package directories.
cmlenz
parents: 56
diff changeset
167 keywords=self.keywords,
84
4ff9cc26c11b Some cosmetic changes for the new translator comments support.
cmlenz
parents: 80
diff changeset
168 comment_tags=self._add_comments,
57
a6183d300a6e * The `extract_messages` distutils command now operators on configurable input directories again, instead of the complete current directory. The `input_dirs` default to the package directories.
cmlenz
parents: 56
diff changeset
169 callback=callback)
80
9c84b9fa5d30 Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents: 79
diff changeset
170 for filename, lineno, message, comments in extracted:
57
a6183d300a6e * The `extract_messages` distutils command now operators on configurable input directories again, instead of the complete current directory. The `input_dirs` default to the package directories.
cmlenz
parents: 56
diff changeset
171 filepath = os.path.normpath(os.path.join(dirname, filename))
80
9c84b9fa5d30 Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents: 79
diff changeset
172 catalog.add(message, None, [(filepath, lineno)],
9c84b9fa5d30 Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents: 79
diff changeset
173 comments=comments)
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
174
51
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
175 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
176 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
177 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
178 charset=self.charset, no_location=self.no_location,
71
ea4cb904df8f Implemented message sorting, see #7.
palgarvio
parents: 66
diff changeset
179 omit_header=self.omit_header, sort_output=self.sort_output,
79
9a05230571f8 Implemented item 4 from #12. Set the copyright holder in the output.
palgarvio
parents: 78
diff changeset
180 sort_by_file=self.sort_by_file,
9a05230571f8 Implemented item 4 from #12. Set the copyright holder in the output.
palgarvio
parents: 78
diff changeset
181 copyright_holder=self.copyright_holder)
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
182 finally:
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
183 outfile.close()
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
184
62
84d400066b71 The order of extraction methods is now preserved (see #10).
cmlenz
parents: 59
diff changeset
185 def _get_mappings(self):
84d400066b71 The order of extraction methods is now preserved (see #10).
cmlenz
parents: 59
diff changeset
186 mappings = {}
84d400066b71 The order of extraction methods is now preserved (see #10).
cmlenz
parents: 59
diff changeset
187
84d400066b71 The order of extraction methods is now preserved (see #10).
cmlenz
parents: 59
diff changeset
188 if self.mapping_file:
84d400066b71 The order of extraction methods is now preserved (see #10).
cmlenz
parents: 59
diff changeset
189 fileobj = open(self.mapping_file, 'U')
84d400066b71 The order of extraction methods is now preserved (see #10).
cmlenz
parents: 59
diff changeset
190 try:
84d400066b71 The order of extraction methods is now preserved (see #10).
cmlenz
parents: 59
diff changeset
191 method_map, options_map = parse_mapping(fileobj)
84d400066b71 The order of extraction methods is now preserved (see #10).
cmlenz
parents: 59
diff changeset
192 for dirname in self.input_dirs:
84d400066b71 The order of extraction methods is now preserved (see #10).
cmlenz
parents: 59
diff changeset
193 mappings[dirname] = method_map, options_map
84d400066b71 The order of extraction methods is now preserved (see #10).
cmlenz
parents: 59
diff changeset
194 finally:
84d400066b71 The order of extraction methods is now preserved (see #10).
cmlenz
parents: 59
diff changeset
195 fileobj.close()
84d400066b71 The order of extraction methods is now preserved (see #10).
cmlenz
parents: 59
diff changeset
196
84d400066b71 The order of extraction methods is now preserved (see #10).
cmlenz
parents: 59
diff changeset
197 elif self.distribution.message_extractors:
84d400066b71 The order of extraction methods is now preserved (see #10).
cmlenz
parents: 59
diff changeset
198 message_extractors = self.distribution.message_extractors
84d400066b71 The order of extraction methods is now preserved (see #10).
cmlenz
parents: 59
diff changeset
199 for dirname, mapping in message_extractors.items():
84d400066b71 The order of extraction methods is now preserved (see #10).
cmlenz
parents: 59
diff changeset
200 if isinstance(mapping, basestring):
84d400066b71 The order of extraction methods is now preserved (see #10).
cmlenz
parents: 59
diff changeset
201 method_map, options_map = parse_mapping(StringIO(mapping))
84d400066b71 The order of extraction methods is now preserved (see #10).
cmlenz
parents: 59
diff changeset
202 else:
84d400066b71 The order of extraction methods is now preserved (see #10).
cmlenz
parents: 59
diff changeset
203 method_map, options_map = [], {}
84d400066b71 The order of extraction methods is now preserved (see #10).
cmlenz
parents: 59
diff changeset
204 for pattern, method, options in mapping:
84d400066b71 The order of extraction methods is now preserved (see #10).
cmlenz
parents: 59
diff changeset
205 method_map.append((pattern, method))
84d400066b71 The order of extraction methods is now preserved (see #10).
cmlenz
parents: 59
diff changeset
206 options_map[pattern] = options or {}
84d400066b71 The order of extraction methods is now preserved (see #10).
cmlenz
parents: 59
diff changeset
207 mappings[dirname] = method_map, options_map
84d400066b71 The order of extraction methods is now preserved (see #10).
cmlenz
parents: 59
diff changeset
208
84d400066b71 The order of extraction methods is now preserved (see #10).
cmlenz
parents: 59
diff changeset
209 else:
84d400066b71 The order of extraction methods is now preserved (see #10).
cmlenz
parents: 59
diff changeset
210 for dirname in self.input_dirs:
84d400066b71 The order of extraction methods is now preserved (see #10).
cmlenz
parents: 59
diff changeset
211 mappings[dirname] = DEFAULT_MAPPING, {}
84d400066b71 The order of extraction methods is now preserved (see #10).
cmlenz
parents: 59
diff changeset
212
84d400066b71 The order of extraction methods is now preserved (see #10).
cmlenz
parents: 59
diff changeset
213 return mappings
84d400066b71 The order of extraction methods is now preserved (see #10).
cmlenz
parents: 59
diff changeset
214
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
215
52
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
216 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
217 """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
218
52
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
219 :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
220 :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
221 "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
222 :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
223 :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
224 :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
225 <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
226 """
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
227 assert name == 'message_extractors'
62
84d400066b71 The order of extraction methods is now preserved (see #10).
cmlenz
parents: 59
diff changeset
228 if not isinstance(value, dict):
84d400066b71 The order of extraction methods is now preserved (see #10).
cmlenz
parents: 59
diff changeset
229 raise DistutilsSetupError('the value of the "message_extractors" '
84d400066b71 The order of extraction methods is now preserved (see #10).
cmlenz
parents: 59
diff changeset
230 'parameter must be a dictionary')
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
231
52
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
232
51
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
233 class new_catalog(Command):
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
234 """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
235
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
236 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
237 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
238 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
239
54
b3395b285104 Rename the `babel.catalog` package to `babel.messages` for consistency with the other package names.
cmlenz
parents: 52
diff changeset
240 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
241
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
242 setup(
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
243 ...
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
244 cmdclass = {'new_catalog': new_catalog}
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
245 )
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
246
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
247 :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
248 :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
249 """
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
250
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
251 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
252 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
253 ('domain=', 'D',
66
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
254 "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
255 ('input-file=', 'i',
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
256 'name of the input file'),
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
257 ('output-dir=', 'd',
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
258 'path to output directory'),
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
259 ('output-file=', 'o',
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
260 "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
261 "'<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
262 ('locale=', 'l',
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
263 '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
264 ('first-author=', None,
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
265 'name of first author'),
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
266 ('first-author-email=', None,
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
267 'email of first author')
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
268 ]
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
269
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
270 def initialize_options(self):
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
271 self.output_dir = None
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
272 self.output_file = None
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
273 self.input_file = None
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
274 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
275 self.domain = None
51
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
276 self.first_author = None
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
277 self.first_author_email = None
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
278
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
279 def finalize_options(self):
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
280 if not self.input_file:
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
281 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
282
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
283 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
284 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
285
51
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
286 if not self.locale:
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
287 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
288 'new catalog')
51
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
289 else:
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
290 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
291 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
292 except UnknownLocaleError, error:
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
293 log.error(error)
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
294 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
295
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
296 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
297 # 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
298 # 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
299 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
300
51
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
301 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
302 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
303
51
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
304 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
305 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
306 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
307 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
308 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
309 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
310
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
311 def run(self):
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
312 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
313 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
314
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
315 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
316 # 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
317 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
318 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
319 # 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
320 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
321 else:
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
322 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
323
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
324 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
325 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
326 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
327 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
328
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
329 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
330 project=self.distribution.get_name(),
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
331 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
332 plurals=plurals,
51
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
333 first_author=self.first_author,
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
334 first_author_email=self.first_author_email)
66
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
335
51
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
336 infile.close()
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
337 outfile.close()
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
338
7f61453c1bea Fixed a bug regarding plural msgid's handling when writing the `.pot` file.
palgarvio
parents: 49
diff changeset
339
52
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
340 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
341 """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
342
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
343 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
344 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
345 """
52
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
346
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
347 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
348 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
349 commands = ['extract', 'init']
63
ac684a515b72 Added the available commands list to the `--help` output of Babel's binary.
palgarvio
parents: 62
diff changeset
350 command_descriptions = {
ac684a515b72 Added the available commands list to the `--help` output of Babel's binary.
palgarvio
parents: 62
diff changeset
351 'extract': 'extract messages from source files and generate a POT file',
ac684a515b72 Added the available commands list to the `--help` output of Babel's binary.
palgarvio
parents: 62
diff changeset
352 'init': 'create new message catalogs from a template'
ac684a515b72 Added the available commands list to the `--help` output of Babel's binary.
palgarvio
parents: 62
diff changeset
353 }
52
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
354
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
355 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
356 """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
357
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
358 :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
359 """
63
ac684a515b72 Added the available commands list to the `--help` output of Babel's binary.
palgarvio
parents: 62
diff changeset
360 self.parser = OptionParser(usage=self.usage % ('subcommand', '[args]'),
52
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
361 version=self.version)
63
ac684a515b72 Added the available commands list to the `--help` output of Babel's binary.
palgarvio
parents: 62
diff changeset
362 self.parser.disable_interspersed_args()
ac684a515b72 Added the available commands list to the `--help` output of Babel's binary.
palgarvio
parents: 62
diff changeset
363 self.parser.print_help = self._help
ac684a515b72 Added the available commands list to the `--help` output of Babel's binary.
palgarvio
parents: 62
diff changeset
364 options, args = self.parser.parse_args(argv[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
365 if not args:
63
ac684a515b72 Added the available commands list to the `--help` output of Babel's binary.
palgarvio
parents: 62
diff changeset
366 self.parser.error('incorrect number of arguments')
52
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
367
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
368 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
369 if cmdname not in self.commands:
63
ac684a515b72 Added the available commands list to the `--help` output of Babel's binary.
palgarvio
parents: 62
diff changeset
370 self.parser.error('unknown subcommand "%s"' % cmdname)
52
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
371
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
372 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
373
63
ac684a515b72 Added the available commands list to the `--help` output of Babel's binary.
palgarvio
parents: 62
diff changeset
374 def _help(self):
ac684a515b72 Added the available commands list to the `--help` output of Babel's binary.
palgarvio
parents: 62
diff changeset
375 print self.parser.format_help()
66
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
376 print "Subcommands:"
63
ac684a515b72 Added the available commands list to the `--help` output of Babel's binary.
palgarvio
parents: 62
diff changeset
377 longest = max([len(command) for command in self.commands])
ac684a515b72 Added the available commands list to the `--help` output of Babel's binary.
palgarvio
parents: 62
diff changeset
378 format = " %" + str(longest) + "s %s"
ac684a515b72 Added the available commands list to the `--help` output of Babel's binary.
palgarvio
parents: 62
diff changeset
379 self.commands.sort()
ac684a515b72 Added the available commands list to the `--help` output of Babel's binary.
palgarvio
parents: 62
diff changeset
380 for command in self.commands:
ac684a515b72 Added the available commands list to the `--help` output of Babel's binary.
palgarvio
parents: 62
diff changeset
381 print format % (command, self.command_descriptions[command])
ac684a515b72 Added the available commands list to the `--help` output of Babel's binary.
palgarvio
parents: 62
diff changeset
382
52
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
383 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
384 """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
385 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
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 :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
388 """
66
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
389 parser = OptionParser(usage=self.usage % ('extract', 'dir1 <dir2> ...'),
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
390 description=self.command_descriptions['extract'])
52
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.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
392 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
393 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
394 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
395 '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
396 '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
397 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
398 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
399 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
400 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
401 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
402 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
403 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
404 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
405 '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
406 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
407 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
408 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
409 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
410 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
411 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
412 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
413 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
414 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
415 'the output line width, into several lines')
71
ea4cb904df8f Implemented message sorting, see #7.
palgarvio
parents: 66
diff changeset
416 parser.add_option('--sort-output', dest='sort_output',
ea4cb904df8f Implemented message sorting, see #7.
palgarvio
parents: 66
diff changeset
417 action='store_true',
ea4cb904df8f Implemented message sorting, see #7.
palgarvio
parents: 66
diff changeset
418 help='generate sorted output (default False)')
ea4cb904df8f Implemented message sorting, see #7.
palgarvio
parents: 66
diff changeset
419 parser.add_option('--sort-by-file', dest='sort_by_file',
ea4cb904df8f Implemented message sorting, see #7.
palgarvio
parents: 66
diff changeset
420 action='store_true',
ea4cb904df8f Implemented message sorting, see #7.
palgarvio
parents: 66
diff changeset
421 help='sort output by file location (default False)')
78
ee043bb666f0 Fixed the plurals header on `Catalog` which should only be written if it's not a catalog template.
palgarvio
parents: 71
diff changeset
422 parser.add_option('--msgid-bugs-address', dest='msgid_bugs_address',
ee043bb666f0 Fixed the plurals header on `Catalog` which should only be written if it's not a catalog template.
palgarvio
parents: 71
diff changeset
423 metavar='EMAIL@ADDRESS',
ee043bb666f0 Fixed the plurals header on `Catalog` which should only be written if it's not a catalog template.
palgarvio
parents: 71
diff changeset
424 help='set report address for msgid')
79
9a05230571f8 Implemented item 4 from #12. Set the copyright holder in the output.
palgarvio
parents: 78
diff changeset
425 parser.add_option('--copyright-holder', dest='copyright_holder',
9a05230571f8 Implemented item 4 from #12. Set the copyright holder in the output.
palgarvio
parents: 78
diff changeset
426 help='set copyright holder in output')
80
9c84b9fa5d30 Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents: 79
diff changeset
427 parser.add_option('--add-comments', '-c', dest='add_comments',
9c84b9fa5d30 Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents: 79
diff changeset
428 metavar='TAG', action='append',
9c84b9fa5d30 Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents: 79
diff changeset
429 help='place comment block with TAG (or those '
9c84b9fa5d30 Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents: 79
diff changeset
430 'preceding keyword lines) in output file. One '
9c84b9fa5d30 Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents: 79
diff changeset
431 'TAG per argument call')
52
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
432
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
433 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
434 no_default_keywords=False, no_location=False,
71
ea4cb904df8f Implemented message sorting, see #7.
palgarvio
parents: 66
diff changeset
435 omit_header = False, width=76, no_wrap=False,
80
9c84b9fa5d30 Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents: 79
diff changeset
436 sort_output=False, sort_by_file=False,
9c84b9fa5d30 Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents: 79
diff changeset
437 add_comments=[])
52
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
438 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
439 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
440 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
441
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
442 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
443 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
444 else:
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
445 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
446
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
447 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
448 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
449 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
450 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
451 '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
452 keywords = {}
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
453 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
454 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
455
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
456 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
457 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
458 try:
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
459 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
460 finally:
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
461 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
462 else:
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
463 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
464 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
465
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
466 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
467 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
468 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
469 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
470 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
471 options.width = 0
71
ea4cb904df8f Implemented message sorting, see #7.
palgarvio
parents: 66
diff changeset
472
ea4cb904df8f Implemented message sorting, see #7.
palgarvio
parents: 66
diff changeset
473 if options.sort_output and options.sort_by_file:
ea4cb904df8f Implemented message sorting, see #7.
palgarvio
parents: 66
diff changeset
474 parser.error("'--sort-output' and '--sort-by-file' are mutually "
ea4cb904df8f Implemented message sorting, see #7.
palgarvio
parents: 66
diff changeset
475 "exclusive")
52
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
476
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
477 try:
78
ee043bb666f0 Fixed the plurals header on `Catalog` which should only be written if it's not a catalog template.
palgarvio
parents: 71
diff changeset
478 catalog = Catalog(msgid_bugs_address=options.msgid_bugs_address)
52
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
479 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
480 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
481 parser.error('%r is not a directory' % dirname)
80
9c84b9fa5d30 Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents: 79
diff changeset
482 extracted = extract_from_dir(dirname, method_map,
9c84b9fa5d30 Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents: 79
diff changeset
483 options_map, keywords,
86
9d539a4dc502 Fixed a bug on the CLI frontend introduced on [81]. Thanks pjenvey!
palgarvio
parents: 84
diff changeset
484 comment_tags=options.comments)
80
9c84b9fa5d30 Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents: 79
diff changeset
485 for filename, lineno, message, comments 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
486 filepath = os.path.normpath(os.path.join(dirname, filename))
80
9c84b9fa5d30 Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents: 79
diff changeset
487 catalog.add(message, None, [(filepath, lineno)],
9c84b9fa5d30 Added support for translator comments at the API and frontends levels.(See #12, item 1). Updated docs and tests accordingly.
palgarvio
parents: 79
diff changeset
488 comments=comments)
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
489
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
490 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
491 charset=options.charset, no_location=options.no_location,
71
ea4cb904df8f Implemented message sorting, see #7.
palgarvio
parents: 66
diff changeset
492 omit_header=options.omit_header,
ea4cb904df8f Implemented message sorting, see #7.
palgarvio
parents: 66
diff changeset
493 sort_output=options.sort_output,
79
9a05230571f8 Implemented item 4 from #12. Set the copyright holder in the output.
palgarvio
parents: 78
diff changeset
494 sort_by_file=options.sort_by_file,
9a05230571f8 Implemented item 4 from #12. Set the copyright holder in the output.
palgarvio
parents: 78
diff changeset
495 copyright_holder=options.copyright_holder)
52
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
496 finally:
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
497 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
498 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
499
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
500 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
501 """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
502
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
503 :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
504 """
66
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
505 parser = OptionParser(usage=self.usage % ('init',''),
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
506 description=self.command_descriptions['init'])
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
507 parser.add_option('--domain', '-D', dest='domain',
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
508 help="domain of PO file (defaults to lower-cased "
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
509 "project name)")
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
510 parser.add_option('--input-file', '-i', dest='input_file',
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
511 help='name of the input file')
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
512 parser.add_option('--output-dir', '-d', dest='output_dir',
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
513 help='path to output directory')
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
514 parser.add_option('--output-file', '-o', dest='output_file',
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
515 help="name of the output file (default "
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
516 "'<output_dir>/<locale>/<domain>.po')")
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
517 parser.add_option('--locale', '-l', dest='locale',
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
518 help='locale for the new localized catalog')
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
519 parser.add_option('--first-author', dest='first_author',
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
520 metavar='FIRST_AUTHOR_NAME',
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
521 help='name of first author')
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
522 parser.add_option('--first-author-email', dest='first_author_email',
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
523 help='email of first author')
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
524 parser.add_option('--project-name', dest='project_name', metavar='NAME',
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
525 default='PROJECT', help='the project name')
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
526 parser.add_option('--project-version', dest='project_version',
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
527 metavar='VERSION', help='the project version')
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
528
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
529 options, args = parser.parse_args(argv)
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
530
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
531 if not options.project_name:
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
532 parser.error('please provide the project name')
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
533
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
534 if not options.project_version:
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
535 parser.error('please provide the project version')
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
536
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
537 if not options.input_file:
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
538 parser.error('you must specify the input file')
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
539
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
540 if not options.domain:
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
541 options.domain = options.project_name.lower()
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
542
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
543 if not options.locale:
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
544 parser.error('you must provide a locale for the new catalog')
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
545 else:
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
546 try:
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
547 _locale = Locale.parse(options.locale)
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
548 except UnknownLocaleError, error:
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
549 parser.error(error)
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
550
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
551 if _locale.territory.lower() == _locale.language:
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
552 # Remove country part if equal to language
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
553 # XXX: This might not be the best behaviour, investigate
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
554 options.locale = _locale.language
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
555
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
556 if not options.output_file and not options.output_dir:
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
557 parser.error('you must specify the output directory')
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
558
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
559 if not options.output_file and options.output_dir:
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
560 options.output_file = os.path.join(options.output_dir,
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
561 options.locale,
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
562 options.domain + '.po')
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
563 if not os.path.exists(os.path.dirname(options.output_file)):
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
564 os.makedirs(os.path.dirname(options.output_file))
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
565
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
566 outfile = open(options.output_file, 'w')
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
567 infile = open(options.input_file, 'r')
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
568
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
569 if PLURALS.has_key(str(_locale)):
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
570 # Try <language>_<COUNTRY> if passed by user
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
571 plurals = PLURALS[str(_locale)]
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
572 elif PLURALS.has_key(_locale.language):
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
573 # Try <language>
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
574 plurals = PLURALS[_locale.language]
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
575 else:
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
576 plurals = ('INTEGER', 'EXPRESSION')
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
577
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
578 print 'Creating %s %r PO from %r PO template' % (_locale.english_name,
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
579 options.output_file,
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
580 options.input_file)
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
581
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
582 write_po(outfile, infile, _locale,
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
583 project=options.project_name,
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
584 version=options.project_version,
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
585 plurals=plurals,
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
586 first_author=options.first_author,
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
587 first_author_email=options.first_author_email)
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
588
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
589 infile.close()
72e43f6881a9 Implemented the `init` subcommand, aka, `new_catalog` for the distutils/setuptools implmentation.
palgarvio
parents: 63
diff changeset
590 outfile.close()
52
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
591
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
592 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
593 CommandLineInterface().run(sys.argv)
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
594
48
bd647e3760e0 Move the mapping configuration file format to `ConfigParser`, and add some more documentation about it.
cmlenz
parents: 47
diff changeset
595 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
596 """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
597
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
598 >>> 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
599 ... # Python source files
62
84d400066b71 The order of extraction methods is now preserved (see #10).
cmlenz
parents: 59
diff changeset
600 ... [python: **.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
601 ...
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
602 ... # Genshi templates
62
84d400066b71 The order of extraction methods is now preserved (see #10).
cmlenz
parents: 59
diff changeset
603 ... [genshi: **/templates/**.html]
48
bd647e3760e0 Move the mapping configuration file format to `ConfigParser`, and add some more documentation about it.
cmlenz
parents: 47
diff changeset
604 ... include_attrs =
62
84d400066b71 The order of extraction methods is now preserved (see #10).
cmlenz
parents: 59
diff changeset
605 ... [genshi: **/templates/**.txt]
48
bd647e3760e0 Move the mapping configuration file format to `ConfigParser`, and add some more documentation about it.
cmlenz
parents: 47
diff changeset
606 ... 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
607 ... 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
608 ... ''')
52
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
609
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
610 >>> 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
611
62
84d400066b71 The order of extraction methods is now preserved (see #10).
cmlenz
parents: 59
diff changeset
612 >>> method_map[0]
84d400066b71 The order of extraction methods is now preserved (see #10).
cmlenz
parents: 59
diff changeset
613 ('**.py', 'python')
84d400066b71 The order of extraction methods is now preserved (see #10).
cmlenz
parents: 59
diff changeset
614 >>> options_map['**.py']
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
615 {}
62
84d400066b71 The order of extraction methods is now preserved (see #10).
cmlenz
parents: 59
diff changeset
616 >>> method_map[1]
84d400066b71 The order of extraction methods is now preserved (see #10).
cmlenz
parents: 59
diff changeset
617 ('**/templates/**.html', 'genshi')
84d400066b71 The order of extraction methods is now preserved (see #10).
cmlenz
parents: 59
diff changeset
618 >>> options_map['**/templates/**.html']['include_attrs']
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
619 ''
62
84d400066b71 The order of extraction methods is now preserved (see #10).
cmlenz
parents: 59
diff changeset
620 >>> method_map[2]
84d400066b71 The order of extraction methods is now preserved (see #10).
cmlenz
parents: 59
diff changeset
621 ('**/templates/**.txt', 'genshi')
84d400066b71 The order of extraction methods is now preserved (see #10).
cmlenz
parents: 59
diff changeset
622 >>> options_map['**/templates/**.txt']['template_class']
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
623 'genshi.template.text.TextTemplate'
62
84d400066b71 The order of extraction methods is now preserved (see #10).
cmlenz
parents: 59
diff changeset
624 >>> options_map['**/templates/**.txt']['encoding']
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
625 '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
626
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
627 :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
628 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
629 :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
630 :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
631 :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
632 """
62
84d400066b71 The order of extraction methods is now preserved (see #10).
cmlenz
parents: 59
diff changeset
633 method_map = []
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
634 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
635
48
bd647e3760e0 Move the mapping configuration file format to `ConfigParser`, and add some more documentation about it.
cmlenz
parents: 47
diff changeset
636 parser = RawConfigParser()
62
84d400066b71 The order of extraction methods is now preserved (see #10).
cmlenz
parents: 59
diff changeset
637 parser._sections = odict(parser._sections) # We need ordered sections
48
bd647e3760e0 Move the mapping configuration file format to `ConfigParser`, and add some more documentation about it.
cmlenz
parents: 47
diff changeset
638 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
639 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
640 method, pattern = [part.strip() for part in section.split(':', 1)]
62
84d400066b71 The order of extraction methods is now preserved (see #10).
cmlenz
parents: 59
diff changeset
641 method_map.append((pattern, method))
48
bd647e3760e0 Move the mapping configuration file format to `ConfigParser`, and add some more documentation about it.
cmlenz
parents: 47
diff changeset
642 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
643
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
644 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
645
12
a2c54ef107c2 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
646 def parse_keywords(strings=[]):
a2c54ef107c2 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
647 """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
648
12
a2c54ef107c2 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
649 >>> kw = parse_keywords(['_', 'dgettext:2', 'dngettext:2,3'])
a2c54ef107c2 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
650 >>> for keyword, indices in sorted(kw.items()):
a2c54ef107c2 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
651 ... print (keyword, indices)
a2c54ef107c2 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
652 ('_', None)
a2c54ef107c2 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
653 ('dgettext', (2,))
a2c54ef107c2 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
654 ('dngettext', (2, 3))
a2c54ef107c2 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
655 """
a2c54ef107c2 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
656 keywords = {}
a2c54ef107c2 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
657 for string in strings:
a2c54ef107c2 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
658 if ':' in string:
a2c54ef107c2 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
659 funcname, indices = string.split(':')
a2c54ef107c2 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
660 else:
a2c54ef107c2 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
661 funcname, indices = string, None
a2c54ef107c2 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
662 if funcname not in keywords:
a2c54ef107c2 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
663 if indices:
a2c54ef107c2 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
664 indices = tuple([(int(x)) for x in indices.split(',')])
a2c54ef107c2 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
665 keywords[funcname] = indices
a2c54ef107c2 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
666 return keywords
a2c54ef107c2 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
667
52
1e724c305460 Support sub-commands in command-line interface, and renamed the generated script wrapper to `babel`. See #9.
cmlenz
parents: 51
diff changeset
668
1
f71ca60f2a4a Import of initial code base.
cmlenz
parents:
diff changeset
669 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
670 main()
Copyright (C) 2012-2017 Edgewall Software