/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

Merge test-run support.

Show diffs side-by-side

added added

removed removed

Lines of Context:
30
30
 
31
31
import inspect
32
32
import os
33
 
import sys
34
33
 
35
34
import breezy
36
35
from . import (
39
38
    help_topics,
40
39
    option,
41
40
    plugin as _mod_plugin,
 
41
    help,
42
42
    )
43
 
from .sixish import PY3
44
43
from .trace import (
45
44
    mutter,
46
45
    note,
50
49
 
51
50
def _escape(s):
52
51
    s = (s.replace('\\', '\\\\')
53
 
         .replace('\n', '\\n')
54
 
         .replace('\r', '\\r')
55
 
         .replace('\t', '\\t')
56
 
         .replace('"', '\\"')
57
 
         )
 
52
        .replace('\n', '\\n')
 
53
        .replace('\r', '\\r')
 
54
        .replace('\t', '\\t')
 
55
        .replace('"', '\\"')
 
56
        )
58
57
    return s
59
58
 
60
 
 
61
59
def _normalize(s):
62
60
    # This converts the various Python string types into a format that
63
61
    # is appropriate for .po files, namely much closer to C style.
73
71
    return s
74
72
 
75
73
 
76
 
def _parse_source(source_text, filename='<unknown>'):
 
74
def _parse_source(source_text):
77
75
    """Get object to lineno mappings from given source_text"""
78
76
    import ast
79
77
    cls_to_lineno = {}
80
78
    str_to_lineno = {}
81
 
    for node in ast.walk(ast.parse(source_text, filename)):
 
79
    for node in ast.walk(ast.parse(source_text)):
82
80
        # TODO: worry about duplicates?
83
81
        if isinstance(node, ast.ClassDef):
84
82
            # TODO: worry about nesting?
88
86
            # string terminates on. It's more useful to have the line the
89
87
            # string begins on. Unfortunately, counting back newlines is
90
88
            # only an approximation as the AST is ignorant of escaping.
91
 
            str_to_lineno[node.s] = node.lineno - (0 if sys.version_info >= (3, 8) else node.s.count('\n'))
 
89
            str_to_lineno[node.s] = node.lineno - node.s.count('\n')
92
90
    return cls_to_lineno, str_to_lineno
93
91
 
94
92
 
108
106
        # TODO: fix this to do the right thing rather than rely on cwd
109
107
        relpath = os.path.relpath(sourcepath)
110
108
        return cls(relpath,
111
 
                   _source_info=_parse_source("".join(inspect.findsource(module)[0]), module.__file__))
 
109
            _source_info=_parse_source("".join(inspect.findsource(module)[0])))
112
110
 
113
111
    def from_class(self, cls):
114
112
        """Get new context with same details but lineno of class in source"""
118
116
            mutter("Definition of %r not found in %r", cls, self.path)
119
117
            return self
120
118
        return self.__class__(self.path, lineno,
121
 
                              (self._cls_to_lineno, self._str_to_lineno))
 
119
            (self._cls_to_lineno, self._str_to_lineno))
122
120
 
123
121
    def from_string(self, string):
124
122
        """Get new context with same details but lineno of string in source"""
128
126
            mutter("String %r not found in %r", string[:20], self.path)
129
127
            return self
130
128
        return self.__class__(self.path, lineno,
131
 
                              (self._cls_to_lineno, self._str_to_lineno))
 
129
            (self._cls_to_lineno, self._str_to_lineno))
132
130
 
133
131
 
134
132
class _PotExporter(object):
159
157
            "msgstr \"\"\n"
160
158
            "\n".format(
161
159
                path=path, lineno=lineno, comment=comment, msg=_normalize(s)))
162
 
        if not PY3:
163
 
            line = line.decode('utf-8')
164
 
        self.outf.write(line)
 
160
        self.outf.write(line.encode('utf-8'))
165
161
 
166
162
    def poentry_in_context(self, context, string, comment=None):
167
163
        context = context.from_string(string)
190
186
 
191
187
def _write_option(exporter, context, opt, note):
192
188
    if getattr(opt, 'hidden', False):
193
 
        return
 
189
        return   
194
190
    optname = opt.name
195
191
    if getattr(opt, 'title', None):
196
192
        exporter.poentry_in_context(context, opt.title,
197
 
                                    "title of {name!r} {what}".format(name=optname, what=note))
 
193
            "title of {name!r} {what}".format(name=optname, what=note))
198
194
    for name, _, _, helptxt in opt.iter_switches():
199
195
        if name != optname:
200
196
            if opt.is_hidden(name):
202
198
            name = "=".join([optname, name])
203
199
        if helptxt:
204
200
            exporter.poentry_in_context(context, helptxt,
205
 
                                        "help of {name!r} {what}".format(name=name, what=note))
 
201
                "help of {name!r} {what}".format(name=name, what=note))
206
202
 
207
203
 
208
204
def _standard_options(exporter):
234
230
            return True
235
231
 
236
232
    exporter.poentry_per_paragraph(dcontext.path, dcontext.lineno, doc,
237
 
                                   exclude_usage)
 
233
        exclude_usage)
238
234
    _command_options(exporter, context, cmd)
239
235
 
240
236
 
260
256
    if plugin_name is not None and plugin_name not in plugins:
261
257
        raise errors.BzrError(gettext('Plugin %s is not loaded' % plugin_name))
262
258
    core_plugins = set(
263
 
        name for name in plugins
264
 
        if plugins[name].path().startswith(breezy.__path__[0]))
 
259
            name for name in plugins
 
260
            if plugins[name].path().startswith(breezy.__path__[0]))
265
261
    # plugins
266
262
    for cmd_name in _mod_commands.plugin_command_names():
267
263
        command = _mod_commands.get_cmd_object(cmd_name, False)
276
272
            # TODO: Support extracting from third party plugins.
277
273
            continue
278
274
        note(gettext("Exporting messages from plugin command: {0} in {1}").format(
279
 
             cmd_name, command.plugin_name()))
 
275
             cmd_name, command.plugin_name() ))
280
276
        _write_command_help(exporter, command)
281
277
 
282
278
 
306
302
        doc = topic_registry.get(key)
307
303
        if isinstance(doc, str):
308
304
            exporter.poentry_per_paragraph(
309
 
                'dummy/help_topics/' + key + '/detail.txt',
310
 
                1, doc)
311
 
        elif callable(doc):  # help topics from files
 
305
                    'dummy/help_topics/'+key+'/detail.txt',
 
306
                    1, doc)
 
307
        elif callable(doc): # help topics from files
312
308
            exporter.poentry_per_paragraph(
313
 
                'en/help_topics/' + key + '.txt',
314
 
                1, doc(key))
 
309
                    'en/help_topics/'+key+'.txt',
 
310
                    1, doc(key))
315
311
        summary = topic_registry.get_summary(key)
316
312
        if summary is not None:
317
 
            exporter.poentry('dummy/help_topics/' + key + '/summary.txt',
318
 
                             1, summary)
 
313
            exporter.poentry('dummy/help_topics/'+key+'/summary.txt',
 
314
                     1, summary)
319
315
 
320
316
 
321
317
def export_pot(outf, plugin=None, include_duplicates=False):