annotate babel/catalog/frontend.py @ 47:f8469ab4b257 trunk

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.
author cmlenz
date Wed, 06 Jun 2007 21:03:24 +0000
parents b09e90803d1b
children 22b90b3b161a
rev   line source
1
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
1 # -*- coding: utf-8 -*-
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
2 #
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
3 # Copyright (C) 2007 Edgewall Software
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
4 # All rights reserved.
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
5 #
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
6 # This software is licensed as described in the file COPYING, which
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
7 # you should have received as part of this distribution. The terms
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
8 # are also available at http://babel.edgewall.org/wiki/License.
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
9 #
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
10 # This software consists of voluntary contributions made by many
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
11 # individuals. For the exact contribution history, see the revision
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
12 # history and logs, available at http://babel.edgewall.org/log/.
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
13
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
14 """Frontends for the message extraction functionality."""
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
15
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
16 from distutils import log
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
17 from distutils.cmd import Command
23
a05c25898be4 Added line-wrap support for `write_po`.
palgarvio
parents: 12
diff changeset
18 from distutils.errors import DistutilsOptionError
1
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
19 from optparse import OptionParser
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
20 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
21 import re
1
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
22 import sys
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
23
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
24 from babel import __version__ as VERSION
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
25 from babel.catalog.extract import extract_from_dir, DEFAULT_KEYWORDS, \
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
26 DEFAULT_MAPPING
1
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
27 from babel.catalog.pofile import write_po
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
28
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
29 __all__ = ['extract_messages', 'main']
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
30 __docformat__ = 'restructuredtext en'
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
31
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
32
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
33 class extract_messages(Command):
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
34 """Message extraction command for use in ``setup.py`` scripts.
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
35
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
36 If correctly installed, this command is available to Setuptools-using
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
37 setup scripts automatically. For projects using plain old ``distutils``,
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
38 the command needs to be registered explicitly in ``setup.py``::
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
39
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
40 from babel.catalog.frontend import extract_messages
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
41
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
42 setup(
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
43 ...
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
44 cmdclass = {'extract_messages': extract_messages}
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
45 )
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
46
1
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
47 :see: `Integrating new distutils commands <http://docs.python.org/dist/node32.html>`_
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
48 :see: `setuptools <http://peak.telecommunity.com/DevCenter/setuptools>`_
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
49 """
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
50
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
51 description = 'extract localizable strings from the project code'
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
52 user_options = [
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
53 ('charset=', None,
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
54 'charset to use in the output file'),
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
55 ('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
56 'space-separated list of keywords to look for in addition to the '
1
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
57 '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
58 ('no-default-keywords', None,
12
e6ba3e878b10 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
59 '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
60 ('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
61 'path to the mapping configuration file'),
1
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
62 ('no-location', None,
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
63 'do not include location comments with filename and line number'),
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
64 ('omit-header', None,
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
65 '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
66 ('output-file=', 'o',
1
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
67 'name of the output file'),
23
a05c25898be4 Added line-wrap support for `write_po`.
palgarvio
parents: 12
diff changeset
68 ('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
69 'set output line width (default 76)'),
23
a05c25898be4 Added line-wrap support for `write_po`.
palgarvio
parents: 12
diff changeset
70 ('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
71 'do not break long message lines, longer than the output line width, '
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
72 'into several lines')
1
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
73 ]
23
a05c25898be4 Added line-wrap support for `write_po`.
palgarvio
parents: 12
diff changeset
74 boolean_options = [
a05c25898be4 Added line-wrap support for `write_po`.
palgarvio
parents: 12
diff changeset
75 'no-default-keywords', 'no-location', 'omit-header', 'no-wrap'
a05c25898be4 Added line-wrap support for `write_po`.
palgarvio
parents: 12
diff changeset
76 ]
1
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
77
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
78 def initialize_options(self):
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
79 self.charset = 'utf-8'
23
a05c25898be4 Added line-wrap support for `write_po`.
palgarvio
parents: 12
diff changeset
80 self.keywords = self._keywords = DEFAULT_KEYWORDS.copy()
12
e6ba3e878b10 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
81 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
82 self.mapping_file = None
1
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
83 self.no_location = False
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
84 self.omit_header = False
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
85 self.output_file = 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
86 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
87 self.no_wrap = False
1
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
88
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
89 def finalize_options(self):
23
a05c25898be4 Added line-wrap support for `write_po`.
palgarvio
parents: 12
diff changeset
90 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
91 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
92 'disable the default ones')
12
e6ba3e878b10 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
93 if self.no_default_keywords:
23
a05c25898be4 Added line-wrap support for `write_po`.
palgarvio
parents: 12
diff changeset
94 self._keywords = {}
1
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
95 if isinstance(self.keywords, basestring):
23
a05c25898be4 Added line-wrap support for `write_po`.
palgarvio
parents: 12
diff changeset
96 self._keywords.update(parse_keywords(self.keywords.split()))
a05c25898be4 Added line-wrap support for `write_po`.
palgarvio
parents: 12
diff changeset
97 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
98
23
a05c25898be4 Added line-wrap support for `write_po`.
palgarvio
parents: 12
diff changeset
99 if self.no_wrap and self.width:
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
100 raise DistutilsOptionError("'--no-wrap' and '--width' are mutually"
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
101 "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
102 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
103 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
104 else:
23
a05c25898be4 Added line-wrap support for `write_po`.
palgarvio
parents: 12
diff changeset
105 self.width = int(self.width)
1
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
106
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
107 def run(self):
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
108 if self.mapping_file:
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
109 fileobj = open(self.mapping_file, 'U')
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
110 try:
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
111 method_map, options_map = parse_mapping(fileobj)
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
112 finally:
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
113 fileobj.close()
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
114 else:
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
115 method_map = DEFAULT_MAPPING
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
116 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
117
1
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
118 outfile = open(self.output_file, 'w')
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
119 try:
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
120 def callback(filename, options):
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
121 optstr = ''
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
122 if options:
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
123 optstr = ' (%s)' % ', '.join(['%s="%s"' % (k, v) for k, v
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
124 in options.items()])
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
125 log.info('extracting messages from %s%s' % (filename, optstr))
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
126
1
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
127 messages = []
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
128 extracted = extract_from_dir(method_map=method_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
129 options_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
130 keywords=self.keywords,
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
131 callback=callback)
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
132 for filename, lineno, funcname, message in extracted:
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
133 filepath = os.path.normpath(filename)
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
134 messages.append((filepath, lineno, funcname, message, 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
135
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
136 log.info('writing PO file to %s' % self.output_file)
5
132526dcd074 * The creation-date header in generated PO files now includes the timezone offset.
cmlenz
parents: 1
diff changeset
137 write_po(outfile, messages, 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
138 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
139 charset=self.charset, no_location=self.no_location,
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
140 omit_header=self.omit_header)
1
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
141 finally:
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
142 outfile.close()
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
143
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
144 def main(argv=sys.argv):
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
145 """Command-line interface.
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
146
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
147 This function provides a simple command-line interface to the message
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
148 extraction and PO file generation functionality.
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
149
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
150 :param argv: list of arguments passed on the command-line
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
151 """
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
152 parser = OptionParser(usage='%prog [options] dirname1 <dirname2> ...',
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
153 version='%%prog %s' % VERSION)
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
154 parser.add_option('--charset', dest='charset', default='utf-8',
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
155 help='charset to use in the output')
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
156 parser.add_option('-k', '--keyword', dest='keywords',
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
157 default=[], action='append',
1
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
158 help='keywords to look for in addition to the defaults. '
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
159 'You can specify multiple -k flags on the command '
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
160 'line.')
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
161 parser.add_option('--no-default-keywords', dest='no_default_keywords',
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
162 action='store_true', default=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
163 help="do not include the default keywords")
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
164 parser.add_option('--mapping', '-F', dest='mapping_file',
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
165 help='path to the extraction mapping file')
1
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
166 parser.add_option('--no-location', dest='no_location', default=False,
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
167 action='store_true',
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
168 help='do not include location comments with filename and '
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
169 'line number')
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
170 parser.add_option('--omit-header', dest='omit_header', default=False,
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
171 action='store_true',
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
172 help='do not include msgid "" entry in header')
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
173 parser.add_option('-o', '--output', dest='output',
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
174 help='path to the output POT file')
23
a05c25898be4 Added line-wrap support for `write_po`.
palgarvio
parents: 12
diff changeset
175 parser.add_option('-w', '--width', dest='width', type='int',
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
176 help="set output line width (default 76)")
23
a05c25898be4 Added line-wrap support for `write_po`.
palgarvio
parents: 12
diff changeset
177 parser.add_option('--no-wrap', dest='no_wrap', default=False,
a05c25898be4 Added line-wrap support for `write_po`.
palgarvio
parents: 12
diff changeset
178 action = 'store_true', help='do not break long message '
a05c25898be4 Added line-wrap support for `write_po`.
palgarvio
parents: 12
diff changeset
179 'lines, longer than the output line width, into several '
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
180 'lines')
1
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
181 options, args = parser.parse_args(argv[1:])
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
182 if not args:
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
183 parser.error('incorrect number of arguments')
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
184
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
185 if options.output not in (None, '-'):
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
186 outfile = open(options.output, 'w')
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
187 else:
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
188 outfile = sys.stdout
12
e6ba3e878b10 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
189
e6ba3e878b10 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
190 keywords = DEFAULT_KEYWORDS.copy()
e6ba3e878b10 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
191 if options.no_default_keywords:
e6ba3e878b10 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
192 if not options.keywords:
e6ba3e878b10 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
193 parser.error('you must specify new keywords if you disable the '
e6ba3e878b10 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
194 'default ones')
e6ba3e878b10 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
195 keywords = {}
e6ba3e878b10 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
196 if options.keywords:
e6ba3e878b10 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
197 keywords.update(parse_keywords(options.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
198
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
199 if options.mapping_file:
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
200 fileobj = open(options.mapping_file, 'U')
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
201 try:
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
202 method_map, options_map = parse_mapping(fileobj)
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
203 finally:
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 fileobj.close()
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
205 else:
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
206 method_map = DEFAULT_MAPPING
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
207 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
208
23
a05c25898be4 Added line-wrap support for `write_po`.
palgarvio
parents: 12
diff changeset
209 if options.width and options.no_wrap:
a05c25898be4 Added line-wrap support for `write_po`.
palgarvio
parents: 12
diff changeset
210 parser.error("'--no-wrap' and '--width' are mutually exclusive.")
a05c25898be4 Added line-wrap support for `write_po`.
palgarvio
parents: 12
diff changeset
211 elif not options.width and not options.no_wrap:
a05c25898be4 Added line-wrap support for `write_po`.
palgarvio
parents: 12
diff changeset
212 options.width = 76
a05c25898be4 Added line-wrap support for `write_po`.
palgarvio
parents: 12
diff changeset
213 elif not options.width and options.no_wrap:
a05c25898be4 Added line-wrap support for `write_po`.
palgarvio
parents: 12
diff changeset
214 options.width = 0
1
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
215
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
216 try:
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
217 messages = []
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
218 for dirname in args:
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
219 if not os.path.isdir(dirname):
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
220 parser.error('%r is not a directory' % dirname)
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
221 extracted = extract_from_dir(dirname, 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
222 keywords)
1
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
223 for filename, lineno, funcname, message in extracted:
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
224 filepath = os.path.normpath(os.path.join(dirname, filename))
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
225 messages.append((filepath, lineno, funcname, message, 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
226 write_po(outfile, messages, width=options.width,
1
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
227 charset=options.charset, no_location=options.no_location,
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
228 omit_header=options.omit_header)
1
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
229 finally:
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
230 if options.output:
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
231 outfile.close()
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
232
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
233 def parse_mapping(fileobj):
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
234 """Parse an extraction method mapping from a file-like object.
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
235
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
236 >>> from StringIO import 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
237 >>> 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
238 ... # Python source files
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
239 ... python: foobar/**.py
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
240 ...
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
241 ... # Genshi templates
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
242 ... genshi: foobar/**/templates/**.html
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
243 ... include_attrs =
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
244 ... genshi: foobar/**/templates/**.txt
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
245 ... template_class = genshi.template.text.TextTemplate
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
246 ... encoding = latin-1
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
247 ... ''')
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
248
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
249 >>> method_map, options_map = parse_mapping(buf)
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
250
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
251 >>> method_map['foobar/**.py']
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
252 'python'
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
253 >>> options_map['foobar/**.py']
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
254 {}
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
255 >>> method_map['foobar/**/templates/**.html']
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
256 'genshi'
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
257 >>> options_map['foobar/**/templates/**.html']['include_attrs']
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
258 ''
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
259 >>> method_map['foobar/**/templates/**.txt']
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
260 'genshi'
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
261 >>> options_map['foobar/**/templates/**.txt']['template_class']
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
262 'genshi.template.text.TextTemplate'
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
263 >>> options_map['foobar/**/templates/**.txt']['encoding']
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
264 'latin-1'
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
265
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
266 :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
267 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
268 :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
269 :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
270 :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
271 """
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
272 method_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
273 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
274
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
275 method = None
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
276 for line in fileobj.readlines():
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
277 if line.startswith('#'): # comment
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
278 continue
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
279 match = re.match('(\w+): (.+)', line)
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
280 if match:
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
281 method, pattern = match.group(1, 2)
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
282 method_map[pattern] = method
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
283 options_map[pattern] = {}
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
284 elif method:
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
285 match = re.match('\s+(\w+)\s*=\s*(.*)', line)
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
286 if match:
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
287 option, value = match.group(1, 2)
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
288 options_map[pattern][option] = value.strip()
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
289
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
290 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
291
12
e6ba3e878b10 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
292 def parse_keywords(strings=[]):
e6ba3e878b10 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
293 """Parse keywords specifications from the given list of strings.
e6ba3e878b10 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
294
e6ba3e878b10 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
295 >>> kw = parse_keywords(['_', 'dgettext:2', 'dngettext:2,3'])
e6ba3e878b10 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
296 >>> for keyword, indices in sorted(kw.items()):
e6ba3e878b10 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
297 ... print (keyword, indices)
e6ba3e878b10 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
298 ('_', None)
e6ba3e878b10 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
299 ('dgettext', (2,))
e6ba3e878b10 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
300 ('dngettext', (2, 3))
e6ba3e878b10 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
301 """
e6ba3e878b10 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
302 keywords = {}
e6ba3e878b10 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
303 for string in strings:
e6ba3e878b10 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
304 if ':' in string:
e6ba3e878b10 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
305 funcname, indices = string.split(':')
e6ba3e878b10 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
306 else:
e6ba3e878b10 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
307 funcname, indices = string, None
e6ba3e878b10 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
308 if funcname not in keywords:
e6ba3e878b10 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
309 if indices:
e6ba3e878b10 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
310 indices = tuple([(int(x)) for x in indices.split(',')])
e6ba3e878b10 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
311 keywords[funcname] = indices
e6ba3e878b10 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
312 return keywords
e6ba3e878b10 * Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents: 10
diff changeset
313
1
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
314 if __name__ == '__main__':
7870274479f5 Import of initial code base.
cmlenz
parents:
diff changeset
315 main()
Copyright (C) 2012-2017 Edgewall Software