1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
#!/usr/bin/env python
#
# bzrgettext - extract docstrings for Bazaar commands
#
# Copyright 2009 Matt Mackall <mpm@selenic.com> and others
# Copyright 2011 Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
# This script is copied from mercurial/i18n/hggettext and modified
# for Bazaar.
# The normalize function is taken from pygettext which is distributed
# with Python under the Python License, which is GPL compatible.
"""Extract docstrings from Bazaar commands.
"""
import os, sys, inspect
def escape(s):
s = (s.replace('\\', '\\\\')
.replace('\n', '\\n')
.replace('\r', '\\r')
.replace('\t', '\\t')
.replace('"', '\\"')
)
return s
def normalize(s):
# This converts the various Python string types into a format that
# is appropriate for .po files, namely much closer to C style.
lines = s.split('\n')
if len(lines) == 1:
s = '"' + escape(s) + '"'
else:
if not lines[-1]:
del lines[-1]
lines[-1] = lines[-1] + '\n'
lines = map(escape, lines)
lineterm = '\\n"\n"'
s = '""\n"' + lineterm.join(lines) + '"'
return s
def poentry(path, lineno, s):
return ('#: %s:%d\n' % (path, lineno) +
'msgid %s\n' % normalize(s) +
'msgstr ""\n')
def offset(src, doc, name, default):
"""Compute offset or issue a warning on stdout."""
# Backslashes in doc appear doubled in src.
end = src.find(doc.replace('\\', '\\\\'))
if end == -1:
# This can happen if the docstring contains unnecessary escape
# sequences such as \" in a triple-quoted string. The problem
# is that \" is turned into " and so doc wont appear in src.
sys.stderr.write("warning: unknown offset in %s, assuming %d lines\n"
% (name, default))
return default
else:
return src.count('\n', 0, end)
def importpath(path):
"""Import a path like foo/bar/baz.py and return the baz module."""
if path.endswith('.py'):
path = path[:-3]
if path.endswith('/__init__'):
path = path[:-9]
path = path.replace('/', '.')
mod = __import__(path)
for comp in path.split('.')[1:]:
mod = getattr(mod, comp)
return mod
def docstrings(path):
"""Extract docstrings from path.
This respects the Bazaar cmdtable/table convention and will
only extract docstrings from functions mentioned in these tables.
"""
from bzrlib.commands import Command as cmd_klass
mod = importpath(path)
for name in dir(mod):
if not name.startswith('cmd_'):
continue
obj = getattr(mod, name)
try:
doc = obj.__doc__
if doc:
doc = inspect.cleandoc(doc)
else:
continue
except AttributeError:
continue
if (inspect.isclass(obj) and issubclass(obj, cmd_klass)
and not obj is cmd_klass):
print poentry(path, inspect.findsource(obj)[1], doc)
def bzrerrors():
"""Extract fmt string from bzrlib.errors."""
from bzrlib import errors
base_klass = errors.BzrError
for name in dir(errors):
klass = getattr(errors, name)
if not inspect.isclass(klass):
continue
if not issubclass(klass, base_klass):
continue
if klass is base_klass:
continue
if klass.internal_error:
continue
fmt = getattr(klass, "_fmt", None)
if fmt:
print poentry('bzrlib/erros.py',
inspect.findsource(klass)[1], fmt)
def rawtext(path):
src = open(path).read()
print poentry(path, 1, src)
if __name__ == "__main__":
# It is very important that we import the Bazaar modules from
# the source tree where bzrgettext is executed. Otherwise we might
# accidentally import and extract strings from a Bazaar
# installation mentioned in PYTHONPATH.
sys.path.insert(0, os.getcwd())
import bzrlib.lazy_import
for path in sys.argv[1:]:
if path.endswith('.txt'):
rawtext(path)
else:
docstrings(path)
bzrerrors()
|