/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
5830.2.12 by INADA Naoki
Make "export-pot" hidden command.
1
# Copyright (C) 2011 Canonical Ltd
5830.2.1 by INADA Naoki
Add update-pot command to Makefile and tools/bzrgettext script that
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
5830.2.12 by INADA Naoki
Make "export-pot" hidden command.
23
import inspect
24
import os
25
26
from bzrlib import (
27
    commands as _mod_commands,
28
    errors,
29
    help_topics,
30
    plugin,
5830.2.15 by INADA Naoki
Add debug trace.
31
    )
32
from bzrlib.trace import (
33
    mutter,
34
    note,
5830.2.12 by INADA Naoki
Make "export-pot" hidden command.
35
    )
36
37
38
def _escape(s):
5830.2.1 by INADA Naoki
Add update-pot command to Makefile and tools/bzrgettext script that
39
    s = (s.replace('\\', '\\\\')
40
        .replace('\n', '\\n')
41
        .replace('\r', '\\r')
42
        .replace('\t', '\\t')
43
        .replace('"', '\\"')
44
        )
45
    return s
46
5830.2.12 by INADA Naoki
Make "export-pot" hidden command.
47
def _normalize(s):
5830.2.1 by INADA Naoki
Add update-pot command to Makefile and tools/bzrgettext script that
48
    # This converts the various Python string types into a format that
49
    # is appropriate for .po files, namely much closer to C style.
50
    lines = s.split('\n')
51
    if len(lines) == 1:
5830.2.12 by INADA Naoki
Make "export-pot" hidden command.
52
        s = '"' + _escape(s) + '"'
5830.2.1 by INADA Naoki
Add update-pot command to Makefile and tools/bzrgettext script that
53
    else:
54
        if not lines[-1]:
55
            del lines[-1]
56
            lines[-1] = lines[-1] + '\n'
5830.2.12 by INADA Naoki
Make "export-pot" hidden command.
57
        lines = map(_escape, lines)
5830.2.1 by INADA Naoki
Add update-pot command to Makefile and tools/bzrgettext script that
58
        lineterm = '\\n"\n"'
59
        s = '""\n"' + lineterm.join(lines) + '"'
60
    return s
61
62
5830.2.12 by INADA Naoki
Make "export-pot" hidden command.
63
_FOUND_MSGID = None # set by entry function.
5830.2.3 by INADA Naoki
bzrgettext extracts help message of command option too.
64
5830.2.12 by INADA Naoki
Make "export-pot" hidden command.
65
def _poentry(outf, path, lineno, s, comment=None):
66
    if s in _FOUND_MSGID:
5830.2.3 by INADA Naoki
bzrgettext extracts help message of command option too.
67
        return
5830.2.12 by INADA Naoki
Make "export-pot" hidden command.
68
    _FOUND_MSGID.add(s)
5830.2.3 by INADA Naoki
bzrgettext extracts help message of command option too.
69
    if comment is None:
70
        comment = ''
71
    else:
72
        comment = "# %s\n" % comment
5830.2.15 by INADA Naoki
Add debug trace.
73
    mutter("Exporting msg %r at line %d in %r", s[:20], lineno, path)
5830.2.12 by INADA Naoki
Make "export-pot" hidden command.
74
    print >>outf, ('#: %s:%d\n' % (path, lineno) +
5830.2.3 by INADA Naoki
bzrgettext extracts help message of command option too.
75
           comment+
5830.2.12 by INADA Naoki
Make "export-pot" hidden command.
76
           'msgid %s\n' % _normalize(s) +
5830.2.3 by INADA Naoki
bzrgettext extracts help message of command option too.
77
           'msgstr ""\n')
5830.2.1 by INADA Naoki
Add update-pot command to Makefile and tools/bzrgettext script that
78
5830.2.12 by INADA Naoki
Make "export-pot" hidden command.
79
def _poentry_per_paragraph(outf, path, lineno, msgid):
5830.2.18 by INADA Naoki
Add some tests for export_pot module.
80
    # TODO: How to split long help?
5830.2.2 by INADA Naoki
Split command's docstring per paragraph.
81
    paragraphs = msgid.split('\n\n')
82
    for p in paragraphs:
5875.3.4 by INADA Naoki
Don't translate :Usage: sections.
83
        if p.splitlines()[0] == ':Usage:':
84
            # :Usage: has special meaning in help topics.
85
            # This is usage example of command and should not be translated.
86
            continue
5830.2.12 by INADA Naoki
Make "export-pot" hidden command.
87
        _poentry(outf, path, lineno, p)
5830.2.2 by INADA Naoki
Split command's docstring per paragraph.
88
        lineno += p.count('\n') + 2
5830.2.1 by INADA Naoki
Add update-pot command to Makefile and tools/bzrgettext script that
89
5830.2.19 by INADA Naoki
Implement ast based offset detection.
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
5830.2.21 by INADA Naoki
Fix lineno of command help in pot
103
        offsets[node.s] = node.lineno - node.s.count('\n')
5830.2.19 by INADA Naoki
Implement ast based offset detection.
104
105
    _LAST_CACHED_SRC = src
106
    _LAST_CACHE = offsets.copy()
107
    return offsets
5830.2.1 by INADA Naoki
Add update-pot command to Makefile and tools/bzrgettext script that
108
5830.2.17 by INADA Naoki
Split command specific options and standard options.
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'
5830.2.19 by INADA Naoki
Implement ast based offset detection.
114
    offsets = _offsets_of_literal(src)
5830.2.17 by INADA Naoki
Split command specific options and standard options.
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):
5830.2.19 by INADA Naoki
Implement ast based offset detection.
121
            lineno = offsets.get(opt.title, 9999)
122
            if lineno == 9999:
123
                note("%r is not found in bzrlib/option.py" % opt.title)
5830.2.17 by INADA Naoki
Split command specific options and standard options.
124
            _poentry(outf, path, lineno, opt.title,
125
                     'title of %r option' % name)
126
        if getattr(opt, 'help', None):
5830.2.19 by INADA Naoki
Implement ast based offset detection.
127
            lineno = offsets.get(opt.help, 9999)
128
            if lineno == 9999:
129
                note("%r is not found in bzrlib/option.py" % opt.help)
5830.2.17 by INADA Naoki
Split command specific options and standard options.
130
            _poentry(outf, path, lineno, opt.help,
131
                     'help of %r option' % name)
5830.2.1 by INADA Naoki
Add update-pot command to Makefile and tools/bzrgettext script that
132
5830.2.12 by INADA Naoki
Make "export-pot" hidden command.
133
def _command_options(outf, path, cmd):
5830.2.17 by INADA Naoki
Split command specific options and standard options.
134
    src, default_lineno = inspect.findsource(cmd.__class__)
5830.2.19 by INADA Naoki
Implement ast based offset detection.
135
    offsets = _offsets_of_literal(''.join(src))
5830.2.17 by INADA Naoki
Split command specific options and standard options.
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):
5830.2.19 by INADA Naoki
Implement ast based offset detection.
143
            lineno = offsets.get(opt.title, default_lineno)
5830.2.17 by INADA Naoki
Split command specific options and standard options.
144
            _poentry(outf, path, lineno, opt.title,
145
                     'title of %r option of %r command' % (name, cmd.name()))
146
        if getattr(opt, 'help', None):
5830.2.19 by INADA Naoki
Implement ast based offset detection.
147
            lineno = offsets.get(opt.help, default_lineno)
5830.2.17 by INADA Naoki
Split command specific options and standard options.
148
            _poentry(outf, path, lineno, opt.help,
149
                     'help of %r option of %r command' % (name, cmd.name()))
5830.2.3 by INADA Naoki
bzrgettext extracts help message of command option too.
150
5830.2.1 by INADA Naoki
Add update-pot command to Makefile and tools/bzrgettext script that
151
5830.2.12 by INADA Naoki
Make "export-pot" hidden command.
152
def _write_command_help(outf, cmd_name, cmd):
153
    path = inspect.getfile(cmd.__class__)
154
    if path.endswith('.pyc'):
155
        path = path[:-1]
156
    path = os.path.relpath(path)
5830.2.21 by INADA Naoki
Fix lineno of command help in pot
157
    src, lineno = inspect.findsource(cmd.__class__)
158
    offsets = _offsets_of_literal(''.join(src))
159
    lineno = offsets[cmd.__doc__]
5830.2.12 by INADA Naoki
Make "export-pot" hidden command.
160
    doc = inspect.getdoc(cmd)
161
162
    _poentry_per_paragraph(outf, path, lineno, doc)
163
    _command_options(outf, path, cmd)
164
165
def _command_helps(outf):
5830.2.1 by INADA Naoki
Add update-pot command to Makefile and tools/bzrgettext script that
166
    """Extract docstrings from path.
167
168
    This respects the Bazaar cmdtable/table convention and will
169
    only extract docstrings from functions mentioned in these tables.
170
    """
5830.2.12 by INADA Naoki
Make "export-pot" hidden command.
171
    from glob import glob
172
173
    # builtin commands
174
    for cmd_name in _mod_commands.builtin_command_names():
5830.2.16 by INADA Naoki
Skip hidden commands to focus important commands.
175
        command = _mod_commands.get_cmd_object(cmd_name, False)
176
        if command.hidden:
177
            continue
5830.2.15 by INADA Naoki
Add debug trace.
178
        note("Exporting messages from builtin command: %s", cmd_name)
5830.2.12 by INADA Naoki
Make "export-pot" hidden command.
179
        _write_command_help(outf, cmd_name, command)
180
5830.2.14 by INADA Naoki
Cleanup import
181
    plugin_path = plugin.get_core_plugin_path()
5830.2.12 by INADA Naoki
Make "export-pot" hidden command.
182
    core_plugins = glob(plugin_path + '/*/__init__.py')
183
    core_plugins = [os.path.basename(os.path.dirname(p))
184
                        for p in core_plugins]
185
    # core plugins
186
    for cmd_name in _mod_commands.plugin_command_names():
187
        command = _mod_commands.get_cmd_object(cmd_name, False)
5830.2.16 by INADA Naoki
Skip hidden commands to focus important commands.
188
        if command.hidden:
189
            continue
5830.2.12 by INADA Naoki
Make "export-pot" hidden command.
190
        if command.plugin_name() not in core_plugins:
191
            # skip non-core plugins
192
            # TODO: Support extracting from third party plugins.
193
            continue
5830.2.15 by INADA Naoki
Add debug trace.
194
        note("Exporting messages from plugin command: %s in %s",
195
             cmd_name, command.plugin_name())
5830.2.12 by INADA Naoki
Make "export-pot" hidden command.
196
        _write_command_help(outf, cmd_name, command)
197
198
199
def _error_messages(outf):
5830.2.1 by INADA Naoki
Add update-pot command to Makefile and tools/bzrgettext script that
200
    """Extract fmt string from bzrlib.errors."""
5830.2.22 by INADA Naoki
Fix line no of error formats in bzr.pot
201
    path = errors.__file__
202
    if path.endswith('.pyc'):
203
        path = path[:-1]
204
    offsets = _offsets_of_literal(open(path).read())
205
5830.2.1 by INADA Naoki
Add update-pot command to Makefile and tools/bzrgettext script that
206
    base_klass = errors.BzrError
207
    for name in dir(errors):
208
        klass = getattr(errors, name)
209
        if not inspect.isclass(klass):
210
            continue
211
        if not issubclass(klass, base_klass):
212
            continue
213
        if klass is base_klass:
214
            continue
215
        if klass.internal_error:
216
            continue
217
        fmt = getattr(klass, "_fmt", None)
218
        if fmt:
5830.2.15 by INADA Naoki
Add debug trace.
219
            note("Exporting message from error: %s", name)
220
            _poentry(outf, 'bzrlib/errors.py',
5830.2.22 by INADA Naoki
Fix line no of error formats in bzr.pot
221
                     offsets.get(fmt, 9999), fmt)
5830.2.1 by INADA Naoki
Add update-pot command to Makefile and tools/bzrgettext script that
222
5830.2.12 by INADA Naoki
Make "export-pot" hidden command.
223
def _help_topics(outf):
5830.2.14 by INADA Naoki
Cleanup import
224
    topic_registry = help_topics.topic_registry
5830.2.9 by INADA Naoki
Export from help_topics that is directly registered into
225
    for key in topic_registry.keys():
226
        doc = topic_registry.get(key)
227
        if isinstance(doc, str):
5830.2.12 by INADA Naoki
Make "export-pot" hidden command.
228
            _poentry_per_paragraph(
229
                    outf,
5830.2.11 by INADA Naoki
Change dummy file path for help topics.
230
                    'dummy/help_topics/'+key+'/detail.txt',
231
                    1, doc)
5830.2.9 by INADA Naoki
Export from help_topics that is directly registered into
232
233
        summary = topic_registry.get_summary(key)
234
        if summary is not None:
5830.2.12 by INADA Naoki
Make "export-pot" hidden command.
235
            _poentry(outf, 'dummy/help_topics/'+key+'/summary.txt',
236
                     1, summary)
237
238
def export_pot(outf):
239
    global _FOUND_MSGID
240
    _FOUND_MSGID = set()
5830.2.17 by INADA Naoki
Split command specific options and standard options.
241
    _standard_options(outf)
5830.2.12 by INADA Naoki
Make "export-pot" hidden command.
242
    _command_helps(outf)
243
    _error_messages(outf)
5830.2.13 by INADA Naoki
Disable exporting help topics.
244
    # disable exporting help topics until we decide  how to translate it.
245
    #_help_topics(outf)