annotate babel/messages/frontend.py @ 78:d0d8d6cd8601 trunk

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