/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 breezy/export_pot.py

  • Committer: Jelmer Vernooij
  • Date: 2017-06-08 23:30:31 UTC
  • mto: This revision was merged to the branch mainline in revision 6690.
  • Revision ID: jelmer@jelmer.uk-20170608233031-3qavls2o7a1pqllj
Update imports.

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
is also left to that stage of the process.
27
27
"""
28
28
 
 
29
from __future__ import absolute_import
 
30
 
29
31
import inspect
30
32
import os
31
 
import sys
32
33
 
33
34
import breezy
34
35
from . import (
36
37
    errors,
37
38
    help_topics,
38
39
    option,
39
 
    plugin as _mod_plugin,
 
40
    help,
40
41
    )
41
42
from .trace import (
42
43
    mutter,
47
48
 
48
49
def _escape(s):
49
50
    s = (s.replace('\\', '\\\\')
50
 
         .replace('\n', '\\n')
51
 
         .replace('\r', '\\r')
52
 
         .replace('\t', '\\t')
53
 
         .replace('"', '\\"')
54
 
         )
 
51
        .replace('\n', '\\n')
 
52
        .replace('\r', '\\r')
 
53
        .replace('\t', '\\t')
 
54
        .replace('"', '\\"')
 
55
        )
55
56
    return s
56
57
 
57
 
 
58
58
def _normalize(s):
59
59
    # This converts the various Python string types into a format that
60
60
    # is appropriate for .po files, namely much closer to C style.
70
70
    return s
71
71
 
72
72
 
73
 
def _parse_source(source_text, filename='<unknown>'):
 
73
def _parse_source(source_text):
74
74
    """Get object to lineno mappings from given source_text"""
75
75
    import ast
76
76
    cls_to_lineno = {}
77
77
    str_to_lineno = {}
78
 
    for node in ast.walk(ast.parse(source_text, filename)):
 
78
    for node in ast.walk(ast.parse(source_text)):
79
79
        # TODO: worry about duplicates?
80
80
        if isinstance(node, ast.ClassDef):
81
81
            # TODO: worry about nesting?
85
85
            # string terminates on. It's more useful to have the line the
86
86
            # string begins on. Unfortunately, counting back newlines is
87
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'))
 
88
            str_to_lineno[node.s] = node.lineno - node.s.count('\n')
89
89
    return cls_to_lineno, str_to_lineno
90
90
 
91
91
 
105
105
        # TODO: fix this to do the right thing rather than rely on cwd
106
106
        relpath = os.path.relpath(sourcepath)
107
107
        return cls(relpath,
108
 
                   _source_info=_parse_source("".join(inspect.findsource(module)[0]), module.__file__))
 
108
            _source_info=_parse_source("".join(inspect.findsource(module)[0])))
109
109
 
110
110
    def from_class(self, cls):
111
111
        """Get new context with same details but lineno of class in source"""
115
115
            mutter("Definition of %r not found in %r", cls, self.path)
116
116
            return self
117
117
        return self.__class__(self.path, lineno,
118
 
                              (self._cls_to_lineno, self._str_to_lineno))
 
118
            (self._cls_to_lineno, self._str_to_lineno))
119
119
 
120
120
    def from_string(self, string):
121
121
        """Get new context with same details but lineno of string in source"""
125
125
            mutter("String %r not found in %r", string[:20], self.path)
126
126
            return self
127
127
        return self.__class__(self.path, lineno,
128
 
                              (self._cls_to_lineno, self._str_to_lineno))
 
128
            (self._cls_to_lineno, self._str_to_lineno))
129
129
 
130
130
 
131
131
class _PotExporter(object):
149
149
        else:
150
150
            comment = "# %s\n" % comment
151
151
        mutter("Exporting msg %r at line %d in %r", s[:20], lineno, path)
152
 
        line = (
 
152
        self.outf.write(
153
153
            "#: {path}:{lineno}\n"
154
154
            "{comment}"
155
155
            "msgid {msg}\n"
156
156
            "msgstr \"\"\n"
157
157
            "\n".format(
158
158
                path=path, lineno=lineno, comment=comment, msg=_normalize(s)))
159
 
        self.outf.write(line)
160
159
 
161
160
    def poentry_in_context(self, context, string, comment=None):
162
161
        context = context.from_string(string)
185
184
 
186
185
def _write_option(exporter, context, opt, note):
187
186
    if getattr(opt, 'hidden', False):
188
 
        return
 
187
        return   
189
188
    optname = opt.name
190
189
    if getattr(opt, 'title', None):
191
190
        exporter.poentry_in_context(context, opt.title,
192
 
                                    "title of {name!r} {what}".format(name=optname, what=note))
 
191
            "title of {name!r} {what}".format(name=optname, what=note))
193
192
    for name, _, _, helptxt in opt.iter_switches():
194
193
        if name != optname:
195
194
            if opt.is_hidden(name):
197
196
            name = "=".join([optname, name])
198
197
        if helptxt:
199
198
            exporter.poentry_in_context(context, helptxt,
200
 
                                        "help of {name!r} {what}".format(name=name, what=note))
 
199
                "help of {name!r} {what}".format(name=name, what=note))
201
200
 
202
201
 
203
202
def _standard_options(exporter):
229
228
            return True
230
229
 
231
230
    exporter.poentry_per_paragraph(dcontext.path, dcontext.lineno, doc,
232
 
                                   exclude_usage)
 
231
        exclude_usage)
233
232
    _command_options(exporter, context, cmd)
234
233
 
235
234
 
251
250
        note(gettext("Exporting messages from builtin command: %s"), cmd_name)
252
251
        _write_command_help(exporter, command)
253
252
 
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
 
253
    plugins = breezy.global_state.plugins
 
254
    core_plugins = set(name for name in plugins
259
255
        if plugins[name].path().startswith(breezy.__path__[0]))
260
256
    # plugins
261
257
    for cmd_name in _mod_commands.plugin_command_names():
263
259
        if command.hidden:
264
260
            continue
265
261
        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.
 
262
            # if we are exporting plugin commands, skip plugins we have not specified.
268
263
            continue
269
264
        if plugin_name is None and command.plugin_name() not in core_plugins:
270
265
            # skip non-core plugins
271
266
            # TODO: Support extracting from third party plugins.
272
267
            continue
273
268
        note(gettext("Exporting messages from plugin command: {0} in {1}").format(
274
 
             cmd_name, command.plugin_name()))
 
269
             cmd_name, command.plugin_name() ))
275
270
        _write_command_help(exporter, command)
276
271
 
277
272
 
301
296
        doc = topic_registry.get(key)
302
297
        if isinstance(doc, str):
303
298
            exporter.poentry_per_paragraph(
304
 
                'dummy/help_topics/' + key + '/detail.txt',
305
 
                1, doc)
306
 
        elif callable(doc):  # help topics from files
 
299
                    'dummy/help_topics/'+key+'/detail.txt',
 
300
                    1, doc)
 
301
        elif callable(doc): # help topics from files
307
302
            exporter.poentry_per_paragraph(
308
 
                'en/help_topics/' + key + '.txt',
309
 
                1, doc(key))
 
303
                    'en/help_topics/'+key+'.txt',
 
304
                    1, doc(key))
310
305
        summary = topic_registry.get_summary(key)
311
306
        if summary is not None:
312
 
            exporter.poentry('dummy/help_topics/' + key + '/summary.txt',
313
 
                             1, summary)
 
307
            exporter.poentry('dummy/help_topics/'+key+'/summary.txt',
 
308
                     1, summary)
314
309
 
315
310
 
316
311
def export_pot(outf, plugin=None, include_duplicates=False):