/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: 2018-05-06 11:48:54 UTC
  • mto: This revision was merged to the branch mainline in revision 6960.
  • Revision ID: jelmer@jelmer.uk-20180506114854-h4qd9ojaqy8wxjsd
Move .mailmap to root.

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):
152
150
        else:
153
151
            comment = "# %s\n" % comment
154
152
        mutter("Exporting msg %r at line %d in %r", s[:20], lineno, path)
155
 
        line = (
 
153
        self.outf.write(
156
154
            "#: {path}:{lineno}\n"
157
155
            "{comment}"
158
156
            "msgid {msg}\n"
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)
165
160
 
166
161
    def poentry_in_context(self, context, string, comment=None):
167
162
        context = context.from_string(string)
190
185
 
191
186
def _write_option(exporter, context, opt, note):
192
187
    if getattr(opt, 'hidden', False):
193
 
        return
 
188
        return   
194
189
    optname = opt.name
195
190
    if getattr(opt, 'title', None):
196
191
        exporter.poentry_in_context(context, opt.title,
197
 
                                    "title of {name!r} {what}".format(name=optname, what=note))
 
192
            "title of {name!r} {what}".format(name=optname, what=note))
198
193
    for name, _, _, helptxt in opt.iter_switches():
199
194
        if name != optname:
200
195
            if opt.is_hidden(name):
202
197
            name = "=".join([optname, name])
203
198
        if helptxt:
204
199
            exporter.poentry_in_context(context, helptxt,
205
 
                                        "help of {name!r} {what}".format(name=name, what=note))
 
200
                "help of {name!r} {what}".format(name=name, what=note))
206
201
 
207
202
 
208
203
def _standard_options(exporter):
234
229
            return True
235
230
 
236
231
    exporter.poentry_per_paragraph(dcontext.path, dcontext.lineno, doc,
237
 
                                   exclude_usage)
 
232
        exclude_usage)
238
233
    _command_options(exporter, context, cmd)
239
234
 
240
235
 
260
255
    if plugin_name is not None and plugin_name not in plugins:
261
256
        raise errors.BzrError(gettext('Plugin %s is not loaded' % plugin_name))
262
257
    core_plugins = set(
263
 
        name for name in plugins
264
 
        if plugins[name].path().startswith(breezy.__path__[0]))
 
258
            name for name in plugins
 
259
            if plugins[name].path().startswith(breezy.__path__[0]))
265
260
    # plugins
266
261
    for cmd_name in _mod_commands.plugin_command_names():
267
262
        command = _mod_commands.get_cmd_object(cmd_name, False)
276
271
            # TODO: Support extracting from third party plugins.
277
272
            continue
278
273
        note(gettext("Exporting messages from plugin command: {0} in {1}").format(
279
 
             cmd_name, command.plugin_name()))
 
274
             cmd_name, command.plugin_name() ))
280
275
        _write_command_help(exporter, command)
281
276
 
282
277
 
306
301
        doc = topic_registry.get(key)
307
302
        if isinstance(doc, str):
308
303
            exporter.poentry_per_paragraph(
309
 
                'dummy/help_topics/' + key + '/detail.txt',
310
 
                1, doc)
311
 
        elif callable(doc):  # help topics from files
 
304
                    'dummy/help_topics/'+key+'/detail.txt',
 
305
                    1, doc)
 
306
        elif callable(doc): # help topics from files
312
307
            exporter.poentry_per_paragraph(
313
 
                'en/help_topics/' + key + '.txt',
314
 
                1, doc(key))
 
308
                    'en/help_topics/'+key+'.txt',
 
309
                    1, doc(key))
315
310
        summary = topic_registry.get_summary(key)
316
311
        if summary is not None:
317
 
            exporter.poentry('dummy/help_topics/' + key + '/summary.txt',
318
 
                             1, summary)
 
312
            exporter.poentry('dummy/help_topics/'+key+'/summary.txt',
 
313
                     1, summary)
319
314
 
320
315
 
321
316
def export_pot(outf, plugin=None, include_duplicates=False):