/
tools.py
269 lines (199 loc) · 7.48 KB
/
tools.py
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import re
from dennis.minisix import textclass
class _MockBlessedThing(textclass):
def __call__(self, s):
return s
class FauxTerminal(object):
def __getattr__(self, attr, default=None):
return _MockBlessedThing()
try:
from blessings import Terminal
except ImportError:
Terminal = FauxTerminal
class Format(object):
"""Variable format base class"""
name = ''
desc = ''
regexp = ''
identifier = None
@classmethod
def extract_variable_name(cls, text):
raise NotImplemented
# http://www.gnu.org/software/gettext/manual/html_node/python_002dformat.html#python_002dformat
# https://docs.python.org/2/library/string.html#format-string-syntax
class PythonBraceFormat(Format):
name = 'python-brace-format'
desc = 'Python brace format (e.g. "{0}", "{foo}")'
regexp = (
# {}, {0}, {foo}, {foo:bar}, {foo:bar baz}
r'(?:\{[^\}]*?\})'
)
identifier = re.compile(r'\{([^!:\}]*)')
@classmethod
def extract_variable_name(cls, text):
identifier = cls.identifier.match(text)
if identifier:
return identifier.group(1)
# http://www.gnu.org/software/gettext/manual/html_node/python_002dformat.html#python_002dformat
# http://docs.python.org/2/library/stdtypes.html#string-formatting-operations
class PythonFormat(Format):
name = 'python-format'
desc = 'Python percent format (e.g. "%s", "%(foo)s")'
regexp = (
# %s and %(foo)s
# Note: This doesn't support %E or %F because of problems
# with false positives and urlencoding. Theoretically those
# aren't getting used in gettext contexts anyhow.
r'(?:%(?:[(]\S+?[)])?[#0+-]?[\.\d\*]*[hlL]?[diouxefGgcrs])'
)
identifier = re.compile(
r'%'
r'(?:' + r'\((\S+?)\)' + r')?'
r'[#0+-]?[\.\d\*]*[hlL]?[diouxefGgcrs]'
)
@classmethod
def extract_variable_name(cls, text):
identifier = cls.identifier.match(text)
if identifier:
return identifier.group(1) or ''
def get_available_formats():
return dict(
(thing.name, thing)
for name, thing in globals().items()
if (name.endswith('Format')
and issubclass(thing, Format)
and thing.name)
)
class UnknownFormat(Exception):
pass
class VariableTokenizer(object):
def __init__(self, formats=None):
"""
:arg formats: List of variable formats
If None, creates a VariableTokenizer that tokenizes on all
formats of variables. Otherwise just recognizes the listed
formats.
"""
all_formats = get_available_formats()
if formats is None:
formats = all_formats.keys()
formats = [fmt for fmt in formats if fmt]
# If they don't want variable tokenizing at all
if not formats:
self.formats = []
self.vars_re = None
else:
# Convert names to classes
self.formats = []
for fmt in formats:
try:
self.formats.append(all_formats[fmt])
except KeyError:
raise UnknownFormat(
'{0} is not a known variable format'.format(fmt))
# Generate variable regexp
self.vars_re = re.compile(
r'(' +
'|'.join([vt.regexp for vt in self.formats]) +
r')'
)
def contains(self, fmt):
"""Does this tokenizer contain specified variable format?"""
return fmt in [tok.name for tok in self.formats]
def tokenize(self, text):
"""Breaks s into strings and Python variables
This preserves whitespace.
:arg text: the string to tokenize
:returns: list of tokens---every even one is a Python variable
"""
if not self.vars_re:
return [text]
return [token for token in self.vars_re.split(text) if token]
def extract_tokens(self, text, unique=True):
"""Returns the set of variable in the text"""
if not self.vars_re:
return set()
try:
tokens = self.vars_re.findall(text)
if unique:
tokens = set(tokens)
return tokens
except TypeError:
print('TYPEERROR: {0}'.format(repr(text)))
def is_token(self, text):
"""Is this text a variable?"""
if not self.vars_re:
return False
return self.vars_re.match(text) is not None
def extract_variable_name(self, text):
for fmt in self.formats:
if re.compile(fmt.regexp).match(text):
return fmt.extract_variable_name(text)
def all_subclasses(cls):
subc = cls.__subclasses__()
for d in list(subc):
subc.extend(all_subclasses(d))
return subc
# Matches:
# "dennis-ignore: *" to skip all the rules
# "dennis-ignore: E201,..." to ignore specific rules
DENNIS_NOTE_RE = re.compile(r'dennis-ignore:\s+(\*|[EW0-9,]+)')
def parse_dennis_note(text):
"""Parses a dennis note and returns list of rules to skip"""
if not text:
return []
match = DENNIS_NOTE_RE.search(text)
if not match:
return []
match = match.group(1).strip()
if match == '*':
return '*'
return [item for item in match.split(',') if item]
def parse_pofile(fn_or_string):
"""Parses a po file and attaches original poentry blocks
When polib parses a pofile, it captures the line number of the
start of the block, but doesn't capture the original string for
the block. When you call str()/unicode() on the poentry, it
"reassembles" the block with textwrapped lines, so it returns
something substantially different than the original block. This is
problematic if we want to print out the block with the line
numbers--one for each line.
So this wrapper captures the line numbers and original text for
each block and attaches that to the parsed poentries in an
attribute named "original" thus allowing us to print the original
text with line numbers.
"""
from polib import _is_file, detect_encoding, io, pofile
# This parses the pofile
parsed_pofile = pofile(fn_or_string)
# Now we need to build a linenumber -> block hash so that we can
# accurately print out what was in the pofile because polib will
# reassembled what it parsed, but it's not the same.
if _is_file(fn_or_string):
enc = detect_encoding(fn_or_string, 'pofile')
fp = io.open(fn_or_string, 'rt', encoding=enc)
else:
fp = fn_or_string.splitlines(True)
fp = list(fp)
entries = list(parsed_pofile)
for i, poentry in enumerate(entries):
# Grab the lines that make up the poentry.
# Note: linenum is 1-based, so we convert it to 0-based.
try:
lines = fp[poentry.linenum-1:entries[i+1].linenum-1]
except IndexError:
lines = fp[poentry.linenum-1:]
# Nix blank lines at the end.
while lines and not lines[-1].strip():
lines.pop()
# Join them and voila!
poentry.original = textclass('').join(lines)
return parsed_pofile
def withlines(linenum, poentry_text):
"""Returns text with line numbers"""
start = linenum
new_text = []
lines_with_nums = zip(range(start, start+100), poentry_text.splitlines())
for line_no, line in lines_with_nums:
new_text.append(textclass(line_no) + textclass(':') + textclass(line))
return textclass('\n').join(new_text)