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
22This module only handles breezy objects that use strings not directly wrapped
23by a gettext() call. To generate a complete translation template file, this
24output needs to be combined with that of xgettext or a similar command for
25extracting those strings, as is done in the bzr Makefile. Sorting the output
26is also left to that stage of the process.
27"""
28
29import inspect
30import os
31import sys
32
33import breezy
34from . import (
35    commands as _mod_commands,
36    errors,
37    help_topics,
38    option,
39    plugin as _mod_plugin,
40    )
41from .trace import (
42    mutter,
43    note,
44    )
45from .i18n import gettext
46
47
48def _escape(s):
49    s = (s.replace('\\', '\\\\')
50         .replace('\n', '\\n')
51         .replace('\r', '\\r')
52         .replace('\t', '\\t')
53         .replace('"', '\\"')
54         )
55    return s
56
57
58def _normalize(s):
59    # This converts the various Python string types into a format that
60    # is appropriate for .po files, namely much closer to C style.
61    lines = s.split('\n')
62    if len(lines) == 1:
63        s = '"' + _escape(s) + '"'
64    else:
65        if not lines[-1]:
66            del lines[-1]
67            lines[-1] = lines[-1] + '\n'
68        lineterm = '\\n"\n"'
69        s = '""\n"' + lineterm.join(map(_escape, lines)) + '"'
70    return s
71
72
73def _parse_source(source_text, filename='<unknown>'):
74    """Get object to lineno mappings from given source_text"""
75    import ast
76    cls_to_lineno = {}
77    str_to_lineno = {}
78    for node in ast.walk(ast.parse(source_text, filename)):
79        # TODO: worry about duplicates?
80        if isinstance(node, ast.ClassDef):
81            # TODO: worry about nesting?
82            cls_to_lineno[node.name] = node.lineno
83        elif isinstance(node, ast.Str):
84            # Python AST gives location of string literal as the line the
85            # string terminates on. It's more useful to have the line the
86            # string begins on. Unfortunately, counting back newlines is
87            # only an approximation as the AST is ignorant of escaping.
88            str_to_lineno[node.s] = node.lineno - (0 if sys.version_info >= (3, 8) else node.s.count('\n'))
89    return cls_to_lineno, str_to_lineno
90
91
92class _ModuleContext(object):
93    """Record of the location within a source tree"""
94
95    def __init__(self, path, lineno=1, _source_info=None):
96        self.path = path
97        self.lineno = lineno
98        if _source_info is not None:
99            self._cls_to_lineno, self._str_to_lineno = _source_info
100
101    @classmethod
102    def from_module(cls, module):
103        """Get new context from module object and parse source for linenos"""
104        sourcepath = inspect.getsourcefile(module)
105        # TODO: fix this to do the right thing rather than rely on cwd
106        relpath = os.path.relpath(sourcepath)
107        return cls(relpath,
108                   _source_info=_parse_source("".join(inspect.findsource(module)[0]), module.__file__))
109
110    def from_class(self, cls):
111        """Get new context with same details but lineno of class in source"""
112        try:
113            lineno = self._cls_to_lineno[cls.__name__]
114        except (AttributeError, KeyError):
115            mutter("Definition of %r not found in %r", cls, self.path)
116            return self
117        return self.__class__(self.path, lineno,
118                              (self._cls_to_lineno, self._str_to_lineno))
119
120    def from_string(self, string):
121        """Get new context with same details but lineno of string in source"""
122        try:
123            lineno = self._str_to_lineno[string]
124        except (AttributeError, KeyError):
125            mutter("String %r not found in %r", string[:20], self.path)
126            return self
127        return self.__class__(self.path, lineno,
128                              (self._cls_to_lineno, self._str_to_lineno))
129
130
131class _PotExporter(object):
132    """Write message details to output stream in .pot file format"""
133
134    def __init__(self, outf, include_duplicates=False):
135        self.outf = outf
136        if include_duplicates:
137            self._msgids = None
138        else:
139            self._msgids = set()
140        self._module_contexts = {}
141
142    def poentry(self, path, lineno, s, comment=None):
143        if self._msgids is not None:
144            if s in self._msgids:
145                return
146            self._msgids.add(s)
147        if comment is None:
148            comment = ''
149        else:
150            comment = "# %s\n" % comment
151        mutter("Exporting msg %r at line %d in %r", s[:20], lineno, path)
152        line = (
153            "#: {path}:{lineno}\n"
154            "{comment}"
155            "msgid {msg}\n"
156            "msgstr \"\"\n"
157            "\n".format(
158                path=path, lineno=lineno, comment=comment, msg=_normalize(s)))
159        self.outf.write(line)
160
161    def poentry_in_context(self, context, string, comment=None):
162        context = context.from_string(string)
163        self.poentry(context.path, context.lineno, string, comment)
164
165    def poentry_per_paragraph(self, path, lineno, msgid, include=None):
166        # TODO: How to split long help?
167        paragraphs = msgid.split('\n\n')
168        if include is not None:
169            paragraphs = filter(include, paragraphs)
170        for p in paragraphs:
171            self.poentry(path, lineno, p)
172            lineno += p.count('\n') + 2
173
174    def get_context(self, obj):
175        module = inspect.getmodule(obj)
176        try:
177            context = self._module_contexts[module.__name__]
178        except KeyError:
179            context = _ModuleContext.from_module(module)
180            self._module_contexts[module.__name__] = context
181        if inspect.isclass(obj):
182            context = context.from_class(obj)
183        return context
184
185
186def _write_option(exporter, context, opt, note):
187    if getattr(opt, 'hidden', False):
188        return
189    optname = opt.name
190    if getattr(opt, 'title', None):
191        exporter.poentry_in_context(context, opt.title,
192                                    "title of {name!r} {what}".format(name=optname, what=note))
193    for name, _, _, helptxt in opt.iter_switches():
194        if name != optname:
195            if opt.is_hidden(name):
196                continue
197            name = "=".join([optname, name])
198        if helptxt:
199            exporter.poentry_in_context(context, helptxt,
200                                        "help of {name!r} {what}".format(name=name, what=note))
201
202
203def _standard_options(exporter):
204    OPTIONS = option.Option.OPTIONS
205    context = exporter.get_context(option)
206    for name in sorted(OPTIONS):
207        opt = OPTIONS[name]
208        _write_option(exporter, context.from_string(name), opt, "option")
209
210
211def _command_options(exporter, context, cmd):
212    note = "option of {0!r} command".format(cmd.name())
213    for opt in cmd.takes_options:
214        # String values in Command option lists are for global options
215        if not isinstance(opt, str):
216            _write_option(exporter, context, opt, note)
217
218
219def _write_command_help(exporter, cmd):
220    context = exporter.get_context(cmd.__class__)
221    rawdoc = cmd.__doc__
222    dcontext = context.from_string(rawdoc)
223    doc = inspect.cleandoc(rawdoc)
224
225    def exclude_usage(p):
226        # ':Usage:' has special meaning in help topics.
227        # This is usage example of command and should not be translated.
228        if p.splitlines()[0] != ':Usage:':
229            return True
230
231    exporter.poentry_per_paragraph(dcontext.path, dcontext.lineno, doc,
232                                   exclude_usage)
233    _command_options(exporter, context, cmd)
234
235
236def _command_helps(exporter, plugin_name=None):
237    """Extract docstrings from path.
238
239    This respects the Bazaar cmdtable/table convention and will
240    only extract docstrings from functions mentioned in these tables.
241    """
242
243    # builtin commands
244    for cmd_name in _mod_commands.builtin_command_names():
245        command = _mod_commands.get_cmd_object(cmd_name, False)
246        if command.hidden:
247            continue
248        if plugin_name is not None:
249            # only export builtins if we are not exporting plugin commands
250            continue
251        note(gettext("Exporting messages from builtin command: %s"), cmd_name)
252        _write_command_help(exporter, command)
253
254    plugins = _mod_plugin.plugins()
255    if plugin_name is not None and plugin_name not in plugins:
256        raise errors.BzrError(gettext('Plugin %s is not loaded' % plugin_name))
257    core_plugins = set(
258        name for name in plugins
259        if plugins[name].path().startswith(breezy.__path__[0]))
260    # plugins
261    for cmd_name in _mod_commands.plugin_command_names():
262        command = _mod_commands.get_cmd_object(cmd_name, False)
263        if command.hidden:
264            continue
265        if plugin_name is not None and command.plugin_name() != plugin_name:
266            # if we are exporting plugin commands, skip plugins we have not
267            # specified.
268            continue
269        if plugin_name is None and command.plugin_name() not in core_plugins:
270            # skip non-core plugins
271            # TODO: Support extracting from third party plugins.
272            continue
273        note(gettext("Exporting messages from plugin command: {0} in {1}").format(
274             cmd_name, command.plugin_name()))
275        _write_command_help(exporter, command)
276
277
278def _error_messages(exporter):
279    """Extract fmt string from breezy.errors."""
280    context = exporter.get_context(errors)
281    base_klass = errors.BzrError
282    for name in dir(errors):
283        klass = getattr(errors, name)
284        if not inspect.isclass(klass):
285            continue
286        if not issubclass(klass, base_klass):
287            continue
288        if klass is base_klass:
289            continue
290        if klass.internal_error:
291            continue
292        fmt = getattr(klass, "_fmt", None)
293        if fmt:
294            note(gettext("Exporting message from error: %s"), name)
295            exporter.poentry_in_context(context, fmt)
296
297
298def _help_topics(exporter):
299    topic_registry = help_topics.topic_registry
300    for key in topic_registry.keys():
301        doc = topic_registry.get(key)
302        if isinstance(doc, str):
303            exporter.poentry_per_paragraph(
304                'dummy/help_topics/' + key + '/detail.txt',
305                1, doc)
306        elif callable(doc):  # help topics from files
307            exporter.poentry_per_paragraph(
308                'en/help_topics/' + key + '.txt',
309                1, doc(key))
310        summary = topic_registry.get_summary(key)
311        if summary is not None:
312            exporter.poentry('dummy/help_topics/' + key + '/summary.txt',
313                             1, summary)
314
315
316def export_pot(outf, plugin=None, include_duplicates=False):
317    exporter = _PotExporter(outf, include_duplicates)
318    if plugin is None:
319        _standard_options(exporter)
320        _command_helps(exporter)
321        _error_messages(exporter)
322        _help_topics(exporter)
323    else:
324        _command_helps(exporter, plugin)
325