~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/export_pot.py

  • Committer: Patch Queue Manager
  • Date: 2011-09-22 13:22:18 UTC
  • mfrom: (6155.2.2 migrate-config-options)
  • Revision ID: pqm@pqm.ubuntu.com-20110922132218-nitl31j5slbcmnm2
(vila) Migrate dpush_strict,
 push_strict and send_strict options to the stack based config design,
 introducing get_config_stack for branches (Vincent Ladeuil)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2011 Canonical Ltd
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
# The normalize function is taken from pygettext which is distributed
 
18
# with Python under the Python License, which is GPL compatible.
 
19
 
 
20
"""Extract docstrings from Bazaar commands.
 
21
"""
 
22
 
 
23
import inspect
 
24
import os
 
25
 
 
26
from bzrlib import (
 
27
    commands as _mod_commands,
 
28
    errors,
 
29
    help_topics,
 
30
    plugin,
 
31
    help,
 
32
    )
 
33
from bzrlib.trace import (
 
34
    mutter,
 
35
    note,
 
36
    )
 
37
from bzrlib.i18n import gettext
 
38
 
 
39
 
 
40
def _escape(s):
 
41
    s = (s.replace('\\', '\\\\')
 
42
        .replace('\n', '\\n')
 
43
        .replace('\r', '\\r')
 
44
        .replace('\t', '\\t')
 
45
        .replace('"', '\\"')
 
46
        )
 
47
    return s
 
48
 
 
49
def _normalize(s):
 
50
    # This converts the various Python string types into a format that
 
51
    # is appropriate for .po files, namely much closer to C style.
 
52
    lines = s.split('\n')
 
53
    if len(lines) == 1:
 
54
        s = '"' + _escape(s) + '"'
 
55
    else:
 
56
        if not lines[-1]:
 
57
            del lines[-1]
 
58
            lines[-1] = lines[-1] + '\n'
 
59
        lines = map(_escape, lines)
 
60
        lineterm = '\\n"\n"'
 
61
        s = '""\n"' + lineterm.join(lines) + '"'
 
62
    return s
 
63
 
 
64
 
 
65
_FOUND_MSGID = None # set by entry function.
 
66
 
 
67
def _poentry(outf, path, lineno, s, comment=None):
 
68
    if s in _FOUND_MSGID:
 
69
        return
 
70
    _FOUND_MSGID.add(s)
 
71
    if comment is None:
 
72
        comment = ''
 
73
    else:
 
74
        comment = "# %s\n" % comment
 
75
    mutter("Exporting msg %r at line %d in %r", s[:20], lineno, path)
 
76
    print >>outf, ('#: %s:%d\n' % (path, lineno) +
 
77
           comment+
 
78
           'msgid %s\n' % _normalize(s) +
 
79
           'msgstr ""\n')
 
80
 
 
81
def _poentry_per_paragraph(outf, path, lineno, msgid, filter=lambda x: False):
 
82
    # TODO: How to split long help?
 
83
    paragraphs = msgid.split('\n\n')
 
84
    for p in paragraphs:
 
85
        if filter(p):
 
86
            continue
 
87
        _poentry(outf, path, lineno, p)
 
88
        lineno += p.count('\n') + 2
 
89
 
 
90
_LAST_CACHE = _LAST_CACHED_SRC = None
 
91
 
 
92
def _offsets_of_literal(src):
 
93
    global _LAST_CACHE, _LAST_CACHED_SRC
 
94
    if src == _LAST_CACHED_SRC:
 
95
        return _LAST_CACHE.copy()
 
96
 
 
97
    import ast
 
98
    root = ast.parse(src)
 
99
    offsets = {}
 
100
    for node in ast.walk(root):
 
101
        if not isinstance(node, ast.Str):
 
102
            continue
 
103
        offsets[node.s] = node.lineno - node.s.count('\n')
 
104
 
 
105
    _LAST_CACHED_SRC = src
 
106
    _LAST_CACHE = offsets.copy()
 
107
    return offsets
 
108
 
 
109
def _standard_options(outf):
 
110
    from bzrlib.option import Option
 
111
    src = inspect.findsource(Option)[0]
 
112
    src = ''.join(src)
 
113
    path = 'bzrlib/option.py'
 
114
    offsets = _offsets_of_literal(src)
 
115
 
 
116
    for name in sorted(Option.OPTIONS.keys()):
 
117
        opt = Option.OPTIONS[name]
 
118
        if getattr(opt, 'hidden', False):
 
119
            continue
 
120
        if getattr(opt, 'title', None):
 
121
            lineno = offsets.get(opt.title, 9999)
 
122
            if lineno == 9999:
 
123
                note(gettext("%r is not found in bzrlib/option.py") % opt.title)
 
124
            _poentry(outf, path, lineno, opt.title,
 
125
                     'title of %r option' % name)
 
126
        if getattr(opt, 'help', None):
 
127
            lineno = offsets.get(opt.help, 9999)
 
128
            if lineno == 9999:
 
129
                note(gettext("%r is not found in bzrlib/option.py") % opt.help)
 
130
            _poentry(outf, path, lineno, opt.help,
 
131
                     'help of %r option' % name)
 
132
 
 
133
def _command_options(outf, path, cmd):
 
134
    src, default_lineno = inspect.findsource(cmd.__class__)
 
135
    offsets = _offsets_of_literal(''.join(src))
 
136
    for opt in cmd.takes_options:
 
137
        if isinstance(opt, str):
 
138
            continue
 
139
        if getattr(opt, 'hidden', False):
 
140
            continue
 
141
        name = opt.name
 
142
        if getattr(opt, 'title', None):
 
143
            lineno = offsets.get(opt.title, default_lineno)
 
144
            _poentry(outf, path, lineno, opt.title,
 
145
                     'title of %r option of %r command' % (name, cmd.name()))
 
146
        if getattr(opt, 'help', None):
 
147
            lineno = offsets.get(opt.help, default_lineno)
 
148
            _poentry(outf, path, lineno, opt.help,
 
149
                     'help of %r option of %r command' % (name, cmd.name()))
 
150
 
 
151
 
 
152
def _write_command_help(outf, cmd):
 
153
    path = inspect.getfile(cmd.__class__)
 
154
    if path.endswith('.pyc'):
 
155
        path = path[:-1]
 
156
    path = os.path.relpath(path)
 
157
    src, lineno = inspect.findsource(cmd.__class__)
 
158
    offsets = _offsets_of_literal(''.join(src))
 
159
    lineno = offsets[cmd.__doc__]
 
160
    doc = inspect.getdoc(cmd)
 
161
 
 
162
    def filter(p):
 
163
        # ':Usage:' has special meaning in help topics.
 
164
        # This is usage example of command and should not be translated.
 
165
        if p.splitlines()[0] == ':Usage:':
 
166
            return True
 
167
 
 
168
    _poentry_per_paragraph(outf, path, lineno, doc, filter)
 
169
    _command_options(outf, path, cmd)
 
170
 
 
171
 
 
172
def _command_helps(outf):
 
173
    """Extract docstrings from path.
 
174
 
 
175
    This respects the Bazaar cmdtable/table convention and will
 
176
    only extract docstrings from functions mentioned in these tables.
 
177
    """
 
178
    from glob import glob
 
179
 
 
180
    # builtin commands
 
181
    for cmd_name in _mod_commands.builtin_command_names():
 
182
        command = _mod_commands.get_cmd_object(cmd_name, False)
 
183
        if command.hidden:
 
184
            continue
 
185
        note(gettext("Exporting messages from builtin command: %s"), cmd_name)
 
186
        _write_command_help(outf, command)
 
187
 
 
188
    plugin_path = plugin.get_core_plugin_path()
 
189
    core_plugins = glob(plugin_path + '/*/__init__.py')
 
190
    core_plugins = [os.path.basename(os.path.dirname(p))
 
191
                        for p in core_plugins]
 
192
    # core plugins
 
193
    for cmd_name in _mod_commands.plugin_command_names():
 
194
        command = _mod_commands.get_cmd_object(cmd_name, False)
 
195
        if command.hidden:
 
196
            continue
 
197
        if command.plugin_name() not in core_plugins:
 
198
            # skip non-core plugins
 
199
            # TODO: Support extracting from third party plugins.
 
200
            continue
 
201
        note(gettext("Exporting messages from plugin command: {0} in {1}").format(
 
202
             cmd_name, command.plugin_name() ))
 
203
        _write_command_help(outf, command)
 
204
 
 
205
 
 
206
def _error_messages(outf):
 
207
    """Extract fmt string from bzrlib.errors."""
 
208
    path = errors.__file__
 
209
    if path.endswith('.pyc'):
 
210
        path = path[:-1]
 
211
    offsets = _offsets_of_literal(open(path).read())
 
212
 
 
213
    base_klass = errors.BzrError
 
214
    for name in dir(errors):
 
215
        klass = getattr(errors, name)
 
216
        if not inspect.isclass(klass):
 
217
            continue
 
218
        if not issubclass(klass, base_klass):
 
219
            continue
 
220
        if klass is base_klass:
 
221
            continue
 
222
        if klass.internal_error:
 
223
            continue
 
224
        fmt = getattr(klass, "_fmt", None)
 
225
        if fmt:
 
226
            note(gettext("Exporting message from error: %s"), name)
 
227
            _poentry(outf, 'bzrlib/errors.py',
 
228
                     offsets.get(fmt, 9999), fmt)
 
229
 
 
230
def _help_topics(outf):
 
231
    topic_registry = help_topics.topic_registry
 
232
    for key in topic_registry.keys():
 
233
        doc = topic_registry.get(key)
 
234
        if isinstance(doc, str):
 
235
            _poentry_per_paragraph(
 
236
                    outf,
 
237
                    'dummy/help_topics/'+key+'/detail.txt',
 
238
                    1, doc)
 
239
        elif callable(doc): # help topics from files
 
240
            _poentry_per_paragraph(
 
241
                    outf,
 
242
                    'en/help_topics/'+key+'.txt',
 
243
                    1, doc(key))
 
244
        summary = topic_registry.get_summary(key)
 
245
        if summary is not None:
 
246
            _poentry(outf, 'dummy/help_topics/'+key+'/summary.txt',
 
247
                     1, summary)
 
248
 
 
249
def export_pot(outf):
 
250
    global _FOUND_MSGID
 
251
    _FOUND_MSGID = set()
 
252
    _standard_options(outf)
 
253
    _command_helps(outf)
 
254
    _error_messages(outf)
 
255
    _help_topics(outf)