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()
72
def _parse_source(source_text):
73
"""Get object to lineno mappings from given source_text"""
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))
77
for node in ast.walk(ast.parse(source_text)):
78
# TODO: worry about duplicates?
79
if isinstance(node, ast.ClassDef):
80
# TODO: worry about nesting?
81
cls_to_lineno[node.name] = node.lineno
82
elif isinstance(node, ast.Str):
83
# Python AST gives location of string literal as the line the
84
# string terminates on. It's more useful to have the line the
85
# string begins on. Unfortunately, counting back newlines is
86
# only an approximation as the AST is ignorant of escaping.
87
str_to_lineno[node.s] = node.lineno - node.s.count('\n')
88
return cls_to_lineno, str_to_lineno
91
class _ModuleContext(object):
92
"""Record of the location within a source tree"""
94
def __init__(self, path, lineno=1, _source_info=None):
97
if _source_info is not None:
98
self._cls_to_lineno, self._str_to_lineno = _source_info
101
def from_module(cls, module):
102
"""Get new context from module object and parse source for linenos"""
103
sourcepath = inspect.getsourcefile(module)
104
# TODO: fix this to do the right thing rather than rely on cwd
105
relpath = os.path.relpath(sourcepath)
107
_source_info=_parse_source("".join(inspect.findsource(module)[0])))
109
def from_class(self, cls):
110
"""Get new context with same details but lineno of class in source"""
112
lineno = self._cls_to_lineno[cls.__name__]
113
except (AttributeError, KeyError):
114
mutter("Definition of %r not found in %r", cls, self.path)
116
return self.__class__(self.path, lineno,
117
(self._cls_to_lineno, self._str_to_lineno))
119
def from_string(self, string):
120
"""Get new context with same details but lineno of string in source"""
122
lineno = self._str_to_lineno[string]
123
except (AttributeError, KeyError):
124
mutter("String %r not found in %r", string[:20], self.path)
126
return self.__class__(self.path, lineno,
127
(self._cls_to_lineno, self._str_to_lineno))
130
class _PotExporter(object):
131
"""Write message details to output stream in .pot file format"""
133
def __init__(self, outf, include_duplicates=False):
135
if include_duplicates:
139
self._module_contexts = {}
141
def poentry(self, path, lineno, s, comment=None):
142
if self._msgids is not None:
143
if s in self._msgids:
149
comment = "# %s\n" % comment
150
mutter("Exporting msg %r at line %d in %r", s[:20], lineno, path)
152
"#: {path}:{lineno}\n"
157
path=path, lineno=lineno, comment=comment, msg=_normalize(s)))
159
def poentry_in_context(self, context, string, comment=None):
160
context = context.from_string(string)
161
self.poentry(context.path, context.lineno, string, comment)
163
def poentry_per_paragraph(self, path, lineno, msgid, include=None):
164
# TODO: How to split long help?
165
paragraphs = msgid.split('\n\n')
166
if include is not None:
167
paragraphs = filter(include, paragraphs)
169
self.poentry(path, lineno, p)
170
lineno += p.count('\n') + 2
172
def get_context(self, obj):
173
module = inspect.getmodule(obj)
175
context = self._module_contexts[module.__name__]
177
context = _ModuleContext.from_module(module)
178
self._module_contexts[module.__name__] = context
179
if inspect.isclass(obj):
180
context = context.from_class(obj)
184
def _write_option(exporter, context, opt, note):
185
if getattr(opt, 'hidden', False):
188
if getattr(opt, 'title', None):
189
exporter.poentry_in_context(context, opt.title,
190
"title of {name!r} {what}".format(name=optname, what=note))
191
for name, _, _, helptxt in opt.iter_switches():
193
if opt.is_hidden(name):
195
name = "=".join([optname, name])
197
exporter.poentry_in_context(context, helptxt,
198
"help of {name!r} {what}".format(name=name, what=note))
201
def _standard_options(exporter):
202
OPTIONS = option.Option.OPTIONS
203
context = exporter.get_context(option)
204
for name in sorted(OPTIONS.keys()):
206
_write_option(exporter, context.from_string(name), opt, "option")
209
def _command_options(exporter, context, cmd):
210
note = "option of {0!r} command".format(cmd.name())
134
211
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)
212
# String values in Command option lists are for global options
213
if not isinstance(opt, str):
214
_write_option(exporter, context, opt, note)
217
def _write_command_help(exporter, cmd):
218
context = exporter.get_context(cmd.__class__)
220
dcontext = context.from_string(rawdoc)
221
doc = inspect.cleandoc(rawdoc)
223
def exclude_usage(p):
161
224
# ':Usage:' has special meaning in help topics.
162
225
# This is usage example of command and should not be translated.
163
if p.splitlines()[0] == ':Usage:':
226
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):
229
exporter.poentry_per_paragraph(dcontext.path, dcontext.lineno, doc,
231
_command_options(exporter, context, cmd)
234
def _command_helps(exporter, plugin_name=None):
171
235
"""Extract docstrings from path.
173
237
This respects the Bazaar cmdtable/table convention and will