/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/export_pot.py

  • Committer: Vincent Ladeuil
  • Date: 2011-11-24 15:48:29 UTC
  • mfrom: (6289 +trunk)
  • mto: This revision was merged to the branch mainline in revision 6337.
  • Revision ID: v.ladeuil+lp@free.fr-20111124154829-avowjpsxdl8yp2vz
merge trunk resolving conflicts

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
# with Python under the Python License, which is GPL compatible.
19
19
 
20
20
"""Extract docstrings from Bazaar commands.
 
21
 
 
22
This module only handles bzrlib objects that use strings not directly wrapped
 
23
by a gettext() call. To generate a complete translation template file, this
 
24
output needs to be combined with that of xgettext or a similar command for
 
25
extracting those strings, as is done in the bzr Makefile. Sorting the output
 
26
is also left to that stage of the process.
21
27
"""
22
28
 
23
29
import inspect
28
34
    errors,
29
35
    help_topics,
30
36
    plugin,
 
37
    help,
31
38
    )
32
39
from bzrlib.trace import (
33
40
    mutter,
34
41
    note,
35
42
    )
 
43
from bzrlib.i18n import gettext
36
44
 
37
45
 
38
46
def _escape(s):
60
68
    return s
61
69
 
62
70
 
63
 
_FOUND_MSGID = None # set by entry function.
64
 
 
65
 
def _poentry(outf, path, lineno, s, comment=None):
66
 
    if s in _FOUND_MSGID:
67
 
        return
68
 
    _FOUND_MSGID.add(s)
69
 
    if comment is None:
70
 
        comment = ''
71
 
    else:
72
 
        comment = "# %s\n" % comment
73
 
    mutter("Exporting msg %r at line %d in %r", s[:20], lineno, path)
74
 
    print >>outf, ('#: %s:%d\n' % (path, lineno) +
75
 
           comment+
76
 
           'msgid %s\n' % _normalize(s) +
77
 
           'msgstr ""\n')
78
 
 
79
 
def _poentry_per_paragraph(outf, path, lineno, msgid, filter=lambda x: False):
80
 
    # TODO: How to split long help?
81
 
    paragraphs = msgid.split('\n\n')
82
 
    for p in paragraphs:
83
 
        if filter(p):
84
 
            continue
85
 
        _poentry(outf, path, lineno, p)
86
 
        lineno += p.count('\n') + 2
 
71
class _PotExporter(object):
 
72
    """Write message details to output stream in .pot file format"""
 
73
 
 
74
    def __init__(self, outf):
 
75
        self.outf = outf
 
76
        self._msgids = set()
 
77
 
 
78
    def poentry(self, path, lineno, s, comment=None):
 
79
        if s in self._msgids:
 
80
            return
 
81
        self._msgids.add(s)
 
82
        if comment is None:
 
83
            comment = ''
 
84
        else:
 
85
            comment = "# %s\n" % comment
 
86
        mutter("Exporting msg %r at line %d in %r", s[:20], lineno, path)
 
87
        self.outf.write(
 
88
            "#: {path}:{lineno}\n"
 
89
            "{comment}"
 
90
            "msgid {msg}\n"
 
91
            "msgstr \"\"\n"
 
92
            "\n".format(
 
93
                path=path, lineno=lineno, comment=comment, msg=_normalize(s)))
 
94
 
 
95
    def poentry_per_paragraph(self, path, lineno, msgid, include=None):
 
96
        # TODO: How to split long help?
 
97
        paragraphs = msgid.split('\n\n')
 
98
        if include is not None:
 
99
            paragraphs = filter(include, paragraphs)
 
100
        for p in paragraphs:
 
101
            self.poentry(path, lineno, p)
 
102
            lineno += p.count('\n') + 2
 
103
 
87
104
 
88
105
_LAST_CACHE = _LAST_CACHED_SRC = None
89
106
 
104
121
    _LAST_CACHE = offsets.copy()
105
122
    return offsets
106
123
 
107
 
def _standard_options(outf):
 
124
def _standard_options(exporter):
108
125
    from bzrlib.option import Option
109
126
    src = inspect.findsource(Option)[0]
110
127
    src = ''.join(src)
118
135
        if getattr(opt, 'title', None):
119
136
            lineno = offsets.get(opt.title, 9999)
120
137
            if lineno == 9999:
121
 
                note("%r is not found in bzrlib/option.py" % opt.title)
122
 
            _poentry(outf, path, lineno, opt.title,
 
138
                note(gettext("%r is not found in bzrlib/option.py") % opt.title)
 
139
            exporter.poentry(path, lineno, opt.title,
123
140
                     'title of %r option' % name)
124
141
        if getattr(opt, 'help', None):
125
142
            lineno = offsets.get(opt.help, 9999)
126
143
            if lineno == 9999:
127
 
                note("%r is not found in bzrlib/option.py" % opt.help)
128
 
            _poentry(outf, path, lineno, opt.help,
 
144
                note(gettext("%r is not found in bzrlib/option.py") % opt.help)
 
145
            exporter.poentry(path, lineno, opt.help,
129
146
                     'help of %r option' % name)
130
147
 
131
 
def _command_options(outf, path, cmd):
 
148
def _command_options(exporter, path, cmd):
132
149
    src, default_lineno = inspect.findsource(cmd.__class__)
133
150
    offsets = _offsets_of_literal(''.join(src))
134
151
    for opt in cmd.takes_options:
139
156
        name = opt.name
140
157
        if getattr(opt, 'title', None):
141
158
            lineno = offsets.get(opt.title, default_lineno)
142
 
            _poentry(outf, path, lineno, opt.title,
 
159
            exporter.poentry(path, lineno, opt.title,
143
160
                     'title of %r option of %r command' % (name, cmd.name()))
144
161
        if getattr(opt, 'help', None):
145
162
            lineno = offsets.get(opt.help, default_lineno)
146
 
            _poentry(outf, path, lineno, opt.help,
 
163
            exporter.poentry(path, lineno, opt.help,
147
164
                     'help of %r option of %r command' % (name, cmd.name()))
148
165
 
149
166
 
150
 
def _write_command_help(outf, cmd):
 
167
def _write_command_help(exporter, cmd):
151
168
    path = inspect.getfile(cmd.__class__)
152
169
    if path.endswith('.pyc'):
153
170
        path = path[:-1]
157
174
    lineno = offsets[cmd.__doc__]
158
175
    doc = inspect.getdoc(cmd)
159
176
 
160
 
    def filter(p):
 
177
    def exclude_usage(p):
161
178
        # ':Usage:' has special meaning in help topics.
162
179
        # This is usage example of command and should not be translated.
163
 
        if p.splitlines()[0] == ':Usage:':
 
180
        if p.splitlines()[0] != ':Usage:':
164
181
            return True
165
182
 
166
 
    _poentry_per_paragraph(outf, path, lineno, doc, filter)
167
 
    _command_options(outf, path, cmd)
168
 
 
169
 
 
170
 
def _command_helps(outf):
 
183
    exporter.poentry_per_paragraph(path, lineno, doc, exclude_usage)
 
184
    _command_options(exporter, path, cmd)
 
185
 
 
186
 
 
187
def _command_helps(exporter, plugin_name=None):
171
188
    """Extract docstrings from path.
172
189
 
173
190
    This respects the Bazaar cmdtable/table convention and will
180
197
        command = _mod_commands.get_cmd_object(cmd_name, False)
181
198
        if command.hidden:
182
199
            continue
183
 
        note("Exporting messages from builtin command: %s", cmd_name)
184
 
        _write_command_help(outf, command)
 
200
        if plugin_name is not None:
 
201
            # only export builtins if we are not exporting plugin commands
 
202
            continue
 
203
        note(gettext("Exporting messages from builtin command: %s"), cmd_name)
 
204
        _write_command_help(exporter, command)
185
205
 
186
206
    plugin_path = plugin.get_core_plugin_path()
187
207
    core_plugins = glob(plugin_path + '/*/__init__.py')
188
208
    core_plugins = [os.path.basename(os.path.dirname(p))
189
209
                        for p in core_plugins]
190
 
    # core plugins
 
210
    # plugins
191
211
    for cmd_name in _mod_commands.plugin_command_names():
192
212
        command = _mod_commands.get_cmd_object(cmd_name, False)
193
213
        if command.hidden:
194
214
            continue
195
 
        if command.plugin_name() not in core_plugins:
 
215
        if plugin_name is not None and command.plugin_name() != plugin_name:
 
216
            # if we are exporting plugin commands, skip plugins we have not specified.
 
217
            continue
 
218
        if plugin_name is None and command.plugin_name() not in core_plugins:
196
219
            # skip non-core plugins
197
220
            # TODO: Support extracting from third party plugins.
198
221
            continue
199
 
        note("Exporting messages from plugin command: %s in %s",
200
 
             cmd_name, command.plugin_name())
201
 
        _write_command_help(outf, command)
202
 
 
203
 
 
204
 
def _error_messages(outf):
 
222
        note(gettext("Exporting messages from plugin command: {0} in {1}").format(
 
223
             cmd_name, command.plugin_name() ))
 
224
        _write_command_help(exporter, command)
 
225
 
 
226
 
 
227
def _error_messages(exporter):
205
228
    """Extract fmt string from bzrlib.errors."""
206
229
    path = errors.__file__
207
230
    if path.endswith('.pyc'):
221
244
            continue
222
245
        fmt = getattr(klass, "_fmt", None)
223
246
        if fmt:
224
 
            note("Exporting message from error: %s", name)
225
 
            _poentry(outf, 'bzrlib/errors.py',
 
247
            note(gettext("Exporting message from error: %s"), name)
 
248
            exporter.poentry('bzrlib/errors.py',
226
249
                     offsets.get(fmt, 9999), fmt)
227
250
 
228
 
def _help_topics(outf):
 
251
def _help_topics(exporter):
229
252
    topic_registry = help_topics.topic_registry
230
253
    for key in topic_registry.keys():
231
254
        doc = topic_registry.get(key)
232
255
        if isinstance(doc, str):
233
 
            _poentry_per_paragraph(
234
 
                    outf,
 
256
            exporter.poentry_per_paragraph(
235
257
                    'dummy/help_topics/'+key+'/detail.txt',
236
258
                    1, doc)
237
 
 
 
259
        elif callable(doc): # help topics from files
 
260
            exporter.poentry_per_paragraph(
 
261
                    'en/help_topics/'+key+'.txt',
 
262
                    1, doc(key))
238
263
        summary = topic_registry.get_summary(key)
239
264
        if summary is not None:
240
 
            _poentry(outf, 'dummy/help_topics/'+key+'/summary.txt',
 
265
            exporter.poentry('dummy/help_topics/'+key+'/summary.txt',
241
266
                     1, summary)
242
267
 
243
 
def export_pot(outf):
244
 
    global _FOUND_MSGID
245
 
    _FOUND_MSGID = set()
246
 
    _standard_options(outf)
247
 
    _command_helps(outf)
248
 
    _error_messages(outf)
249
 
    # disable exporting help topics until we decide  how to translate it.
250
 
    #_help_topics(outf)
 
268
def export_pot(outf, plugin=None):
 
269
    exporter = _PotExporter(outf)
 
270
    if plugin is None:
 
271
        _standard_options(exporter)
 
272
        _command_helps(exporter)
 
273
        _error_messages(exporter)
 
274
        _help_topics(exporter)
 
275
    else:
 
276
        _command_helps(exporter, plugin)