74
def _parse_source(source_text):
75
"""Get object to lineno mappings from given source_text"""
65
_FOUND_MSGID = None # set by entry function.
67
def _poentry(outf, path, lineno, s, comment=None):
74
comment = "# %s\n" % comment
75
mutter("Exporting msg %r at line %d in %r", s[:20], lineno, path)
76
print >>outf, ('#: %s:%d\n' % (path, lineno) +
78
'msgid %s\n' % _normalize(s) +
81
def _poentry_per_paragraph(outf, path, lineno, msgid, filter=lambda x: False):
82
# TODO: How to split long help?
83
paragraphs = msgid.split('\n\n')
87
_poentry(outf, path, lineno, p)
88
lineno += p.count('\n') + 2
90
_LAST_CACHE = _LAST_CACHED_SRC = None
92
def _offsets_of_literal(src):
93
global _LAST_CACHE, _LAST_CACHED_SRC
94
if src == _LAST_CACHED_SRC:
95
return _LAST_CACHE.copy()
79
for node in ast.walk(ast.parse(source_text)):
80
# TODO: worry about duplicates?
81
if isinstance(node, ast.ClassDef):
82
# TODO: worry about nesting?
83
cls_to_lineno[node.name] = node.lineno
84
elif isinstance(node, ast.Str):
85
# Python AST gives location of string literal as the line the
86
# string terminates on. It's more useful to have the line the
87
# string begins on. Unfortunately, counting back newlines is
88
# only an approximation as the AST is ignorant of escaping.
89
str_to_lineno[node.s] = node.lineno - node.s.count('\n')
90
return cls_to_lineno, str_to_lineno
93
class _ModuleContext(object):
94
"""Record of the location within a source tree"""
96
def __init__(self, path, lineno=1, _source_info=None):
99
if _source_info is not None:
100
self._cls_to_lineno, self._str_to_lineno = _source_info
103
def from_module(cls, module):
104
"""Get new context from module object and parse source for linenos"""
105
sourcepath = inspect.getsourcefile(module)
106
# TODO: fix this to do the right thing rather than rely on cwd
107
relpath = os.path.relpath(sourcepath)
109
_source_info=_parse_source("".join(inspect.findsource(module)[0])))
111
def from_class(self, cls):
112
"""Get new context with same details but lineno of class in source"""
114
lineno = self._cls_to_lineno[cls.__name__]
115
except (AttributeError, KeyError):
116
mutter("Definition of %r not found in %r", cls, self.path)
118
return self.__class__(self.path, lineno,
119
(self._cls_to_lineno, self._str_to_lineno))
121
def from_string(self, string):
122
"""Get new context with same details but lineno of string in source"""
124
lineno = self._str_to_lineno[string]
125
except (AttributeError, KeyError):
126
mutter("String %r not found in %r", string[:20], self.path)
128
return self.__class__(self.path, lineno,
129
(self._cls_to_lineno, self._str_to_lineno))
132
class _PotExporter(object):
133
"""Write message details to output stream in .pot file format"""
135
def __init__(self, outf, include_duplicates=False):
137
if include_duplicates:
141
self._module_contexts = {}
143
def poentry(self, path, lineno, s, comment=None):
144
if self._msgids is not None:
145
if s in self._msgids:
151
comment = "# %s\n" % comment
152
mutter("Exporting msg %r at line %d in %r", s[:20], lineno, path)
154
"#: {path}:{lineno}\n"
159
path=path, lineno=lineno, comment=comment, msg=_normalize(s)))
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)
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)
171
self.poentry(path, lineno, p)
172
lineno += p.count('\n') + 2
174
def get_context(self, obj):
175
module = inspect.getmodule(obj)
177
context = self._module_contexts[module.__name__]
179
context = _ModuleContext.from_module(module)
180
self._module_contexts[module.__name__] = context
181
if inspect.isclass(obj):
182
context = context.from_class(obj)
186
def _write_option(exporter, context, opt, note):
187
if getattr(opt, 'hidden', False):
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():
195
if opt.is_hidden(name):
197
name = "=".join([optname, name])
199
exporter.poentry_in_context(context, helptxt,
200
"help of {name!r} {what}".format(name=name, what=note))
203
def _standard_options(exporter):
204
OPTIONS = option.Option.OPTIONS
205
context = exporter.get_context(option)
206
for name in sorted(OPTIONS.keys()):
208
_write_option(exporter, context.from_string(name), opt, "option")
211
def _command_options(exporter, context, cmd):
212
note = "option of {0!r} command".format(cmd.name())
100
for node in ast.walk(root):
101
if not isinstance(node, ast.Str):
103
offsets[node.s] = node.lineno - node.s.count('\n')
105
_LAST_CACHED_SRC = src
106
_LAST_CACHE = offsets.copy()
109
def _standard_options(outf):
110
from bzrlib.option import Option
111
src = inspect.findsource(Option)[0]
113
path = 'bzrlib/option.py'
114
offsets = _offsets_of_literal(src)
116
for name in sorted(Option.OPTIONS.keys()):
117
opt = Option.OPTIONS[name]
118
if getattr(opt, 'hidden', False):
120
if getattr(opt, 'title', None):
121
lineno = offsets.get(opt.title, 9999)
123
note(gettext("%r is not found in bzrlib/option.py") % opt.title)
124
_poentry(outf, path, lineno, opt.title,
125
'title of %r option' % name)
126
if getattr(opt, 'help', None):
127
lineno = offsets.get(opt.help, 9999)
129
note(gettext("%r is not found in bzrlib/option.py") % opt.help)
130
_poentry(outf, path, lineno, opt.help,
131
'help of %r option' % name)
133
def _command_options(outf, path, cmd):
134
src, default_lineno = inspect.findsource(cmd.__class__)
135
offsets = _offsets_of_literal(''.join(src))
213
136
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)
219
def _write_command_help(exporter, cmd):
220
context = exporter.get_context(cmd.__class__)
222
dcontext = context.from_string(rawdoc)
223
doc = inspect.cleandoc(rawdoc)
225
def exclude_usage(p):
137
if isinstance(opt, str):
139
if getattr(opt, 'hidden', False):
142
if getattr(opt, 'title', None):
143
lineno = offsets.get(opt.title, default_lineno)
144
_poentry(outf, path, lineno, opt.title,
145
'title of %r option of %r command' % (name, cmd.name()))
146
if getattr(opt, 'help', None):
147
lineno = offsets.get(opt.help, default_lineno)
148
_poentry(outf, path, lineno, opt.help,
149
'help of %r option of %r command' % (name, cmd.name()))
152
def _write_command_help(outf, cmd):
153
path = inspect.getfile(cmd.__class__)
154
if path.endswith('.pyc'):
156
path = os.path.relpath(path)
157
src, lineno = inspect.findsource(cmd.__class__)
158
offsets = _offsets_of_literal(''.join(src))
159
lineno = offsets[cmd.__doc__]
160
doc = inspect.getdoc(cmd)
226
163
# ':Usage:' has special meaning in help topics.
227
164
# This is usage example of command and should not be translated.
228
if p.splitlines()[0] != ':Usage:':
165
if p.splitlines()[0] == ':Usage:':
231
exporter.poentry_per_paragraph(dcontext.path, dcontext.lineno, doc,
233
_command_options(exporter, context, cmd)
236
def _command_helps(exporter, plugin_name=None):
168
_poentry_per_paragraph(outf, path, lineno, doc, filter)
169
_command_options(outf, path, cmd)
172
def _command_helps(outf, plugin_name=None):
237
173
"""Extract docstrings from path.
239
175
This respects the Bazaar cmdtable/table convention and will