1
# Copyright (C) 2011 Canonical Ltd
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.
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.
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
17
# The normalize function is taken from pygettext which is distributed
18
# with Python under the Python License, which is GPL compatible.
20
"""Extract docstrings from Bazaar commands.
27
commands as _mod_commands,
32
from bzrlib.trace import (
39
s = (s.replace('\\', '\\\\')
48
# This converts the various Python string types into a format that
49
# is appropriate for .po files, namely much closer to C style.
52
s = '"' + _escape(s) + '"'
56
lines[-1] = lines[-1] + '\n'
57
lines = map(_escape, lines)
59
s = '""\n"' + lineterm.join(lines) + '"'
63
_FOUND_MSGID = None # set by entry function.
65
def _poentry(outf, path, lineno, s, comment=None):
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) +
76
'msgid %s\n' % _normalize(s) +
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')
85
_poentry(outf, path, lineno, p)
86
lineno += p.count('\n') + 2
88
_LAST_CACHE = _LAST_CACHED_SRC = None
90
def _offsets_of_literal(src):
91
global _LAST_CACHE, _LAST_CACHED_SRC
92
if src == _LAST_CACHED_SRC:
93
return _LAST_CACHE.copy()
98
for node in ast.walk(root):
99
if not isinstance(node, ast.Str):
101
offsets[node.s] = node.lineno - node.s.count('\n')
103
_LAST_CACHED_SRC = src
104
_LAST_CACHE = offsets.copy()
107
def _standard_options(outf):
108
from bzrlib.option import Option
109
src = inspect.findsource(Option)[0]
111
path = 'bzrlib/option.py'
112
offsets = _offsets_of_literal(src)
114
for name in sorted(Option.OPTIONS.keys()):
115
opt = Option.OPTIONS[name]
116
if getattr(opt, 'hidden', False):
118
if getattr(opt, 'title', None):
119
lineno = offsets.get(opt.title, 9999)
121
note("%r is not found in bzrlib/option.py" % opt.title)
122
_poentry(outf, path, lineno, opt.title,
123
'title of %r option' % name)
124
if getattr(opt, 'help', None):
125
lineno = offsets.get(opt.help, 9999)
127
note("%r is not found in bzrlib/option.py" % opt.help)
128
_poentry(outf, path, lineno, opt.help,
129
'help of %r option' % name)
131
def _command_options(outf, path, cmd):
132
src, default_lineno = inspect.findsource(cmd.__class__)
133
offsets = _offsets_of_literal(''.join(src))
134
for opt in cmd.takes_options:
135
if isinstance(opt, str):
137
if getattr(opt, 'hidden', False):
140
if getattr(opt, 'title', None):
141
lineno = offsets.get(opt.title, default_lineno)
142
_poentry(outf, path, lineno, opt.title,
143
'title of %r option of %r command' % (name, cmd.name()))
144
if getattr(opt, 'help', None):
145
lineno = offsets.get(opt.help, default_lineno)
146
_poentry(outf, path, lineno, opt.help,
147
'help of %r option of %r command' % (name, cmd.name()))
150
def _write_command_help(outf, cmd):
151
path = inspect.getfile(cmd.__class__)
152
if path.endswith('.pyc'):
154
path = os.path.relpath(path)
155
src, lineno = inspect.findsource(cmd.__class__)
156
offsets = _offsets_of_literal(''.join(src))
157
lineno = offsets[cmd.__doc__]
158
doc = inspect.getdoc(cmd)
161
# ':Usage:' has special meaning in help topics.
162
# This is usage example of command and should not be translated.
163
if p.splitlines()[0] == ':Usage:':
166
_poentry_per_paragraph(outf, path, lineno, doc, filter)
167
_command_options(outf, path, cmd)
170
def _command_helps(outf):
171
"""Extract docstrings from path.
173
This respects the Bazaar cmdtable/table convention and will
174
only extract docstrings from functions mentioned in these tables.
176
from glob import glob
179
for cmd_name in _mod_commands.builtin_command_names():
180
command = _mod_commands.get_cmd_object(cmd_name, False)
183
note("Exporting messages from builtin command: %s", cmd_name)
184
_write_command_help(outf, command)
186
plugin_path = plugin.get_core_plugin_path()
187
core_plugins = glob(plugin_path + '/*/__init__.py')
188
core_plugins = [os.path.basename(os.path.dirname(p))
189
for p in core_plugins]
191
for cmd_name in _mod_commands.plugin_command_names():
192
command = _mod_commands.get_cmd_object(cmd_name, False)
195
if command.plugin_name() not in core_plugins:
196
# skip non-core plugins
197
# TODO: Support extracting from third party plugins.
199
note("Exporting messages from plugin command: %s in %s",
200
cmd_name, command.plugin_name())
201
_write_command_help(outf, command)
204
def _error_messages(outf):
205
"""Extract fmt string from bzrlib.errors."""
206
path = errors.__file__
207
if path.endswith('.pyc'):
209
offsets = _offsets_of_literal(open(path).read())
211
base_klass = errors.BzrError
212
for name in dir(errors):
213
klass = getattr(errors, name)
214
if not inspect.isclass(klass):
216
if not issubclass(klass, base_klass):
218
if klass is base_klass:
220
if klass.internal_error:
222
fmt = getattr(klass, "_fmt", None)
224
note("Exporting message from error: %s", name)
225
_poentry(outf, 'bzrlib/errors.py',
226
offsets.get(fmt, 9999), fmt)
228
def _help_topics(outf):
229
topic_registry = help_topics.topic_registry
230
for key in topic_registry.keys():
231
doc = topic_registry.get(key)
232
if isinstance(doc, str):
233
_poentry_per_paragraph(
235
'dummy/help_topics/'+key+'/detail.txt',
238
summary = topic_registry.get_summary(key)
239
if summary is not None:
240
_poentry(outf, 'dummy/help_topics/'+key+'/summary.txt',
243
def export_pot(outf):
246
_standard_options(outf)
248
_error_messages(outf)
249
# disable exporting help topics until we decide how to translate it.