Skip to content

Commit a6c37d6

Browse files
CaselITGerrit Code Review
authored andcommitted
Merge "Refactoring Code" into main
2 parents eacf04b + c47d172 commit a6c37d6

15 files changed

Lines changed: 187 additions & 227 deletions

File tree

.github/workflows/run-test.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ jobs:
2323
- "windows-latest"
2424
- "macos-latest"
2525
python-version:
26-
- "3.6"
2726
- "3.7"
2827
- "3.8"
2928
- "3.9"

mako/ast.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def __init__(self, code, **exception_kwargs):
9696
code = code + "pass"
9797
elif keyword == "try":
9898
code = code + "pass\nexcept:pass"
99-
elif keyword == "elif" or keyword == "else":
99+
elif keyword in ["elif", "else"]:
100100
code = "if False:pass\n" + code + "pass"
101101
elif keyword == "except":
102102
code = "try:pass\n" + code + "pass"

mako/exceptions.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -225,13 +225,12 @@ def _init(self, trcback):
225225
if new_trcback:
226226
try:
227227
# A normal .py file (not a Template)
228-
fp = open(new_trcback[-1][0], "rb")
229-
encoding = util.parse_encoding(fp)
230-
if not encoding:
231-
encoding = "utf-8"
232-
fp.seek(0)
233-
self.source = fp.read()
234-
fp.close()
228+
with open(new_trcback[-1][0], "rb") as fp:
229+
encoding = util.parse_encoding(fp)
230+
if not encoding:
231+
encoding = "utf-8"
232+
fp.seek(0)
233+
self.source = fp.read()
235234
if encoding:
236235
self.source = self.source.decode(encoding)
237236
except IOError:

mako/ext/babelplugin.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,4 @@ def extract(fileobj, keywords, comment_tags, options):
5454
:rtype: ``iterator``
5555
"""
5656
extractor = BabelMakoExtractor(keywords, comment_tags, options)
57-
for message in extractor(fileobj):
58-
yield message
57+
yield from extractor(fileobj)

mako/ext/extract.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ def process_file(self, fileobj):
1919
template_node = lexer.Lexer(
2020
fileobj.read(), input_encoding=self.config["encoding"]
2121
).parse()
22-
for extracted in self.extract_nodes(template_node.get_children()):
23-
yield extracted
22+
yield from self.extract_nodes(template_node.get_children())
2423

2524
def extract_nodes(self, nodes):
2625
translator_comments = []
@@ -118,8 +117,7 @@ def extract_nodes(self, nodes):
118117
in_translator_comments = False
119118

120119
if child_nodes:
121-
for extracted in self.extract_nodes(child_nodes):
122-
yield extracted
120+
yield from self.extract_nodes(child_nodes)
123121

124122
@staticmethod
125123
def _split_comment(lineno, comment):

mako/ext/linguaplugin.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# This module is part of Mako and is released under
55
# the MIT License: http://www.opensource.org/licenses/mit-license.php
66

7+
import contextlib
78
import io
89

910
from lingua.extractors import Extractor
@@ -25,16 +26,11 @@ def __call__(self, filename, options, fileobj=None):
2526
self.filename = filename
2627
self.python_extractor = get_extractor("x.py")
2728
if fileobj is None:
28-
fileobj = open(filename, "r")
29-
must_close = True
29+
ctx = open(filename, "r")
3030
else:
31-
must_close = False
32-
try:
33-
for message in self.process_file(fileobj):
34-
yield message
35-
finally:
36-
if must_close:
37-
fileobj.close()
31+
ctx = contextlib.nullcontext(fileobj)
32+
with ctx as file_:
33+
yield from self.process_file(file_)
3834

3935
def process_python(self, code, code_lineno, translator_strings):
4036
source = code.getvalue().strip()

mako/lexer.py

Lines changed: 90 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,7 @@ def match(self, regexp, flags=None):
5454
try:
5555
reg = _regexp_cache[(regexp, flags)]
5656
except KeyError:
57-
if flags:
58-
reg = re.compile(regexp, flags)
59-
else:
60-
reg = re.compile(regexp)
57+
reg = re.compile(regexp, flags) if flags else re.compile(regexp)
6158
_regexp_cache[(regexp, flags)] = reg
6259

6360
return self.match_reg(reg)
@@ -75,21 +72,14 @@ def match_reg(self, reg):
7572
match = reg.match(self.text, self.match_position)
7673
if match:
7774
(start, end) = match.span()
78-
if end == start:
79-
self.match_position = end + 1
80-
else:
81-
self.match_position = end
75+
self.match_position = end + 1 if end == start else end
8276
self.matched_lineno = self.lineno
8377
lines = re.findall(r"\n", self.text[mp : self.match_position])
8478
cp = mp - 1
8579
while cp >= 0 and cp < self.textlength and self.text[cp] != "\n":
8680
cp -= 1
8781
self.matched_charpos = mp - cp
8882
self.lineno += len(lines)
89-
# print "MATCHED:", match.group(0), "LINE START:",
90-
# self.matched_lineno, "LINE END:", self.lineno
91-
# print "MATCH:", regexp, "\n", self.text[mp : mp + 15], \
92-
# (match and "TRUE" or "FALSE")
9383
return match
9484

9585
def parse_until_text(self, watch_nesting, *text):
@@ -149,12 +139,15 @@ def append_node(self, nodecls, *args, **kwargs):
149139
if self.control_line:
150140
control_frame = self.control_line[-1]
151141
control_frame.nodes.append(node)
152-
if not (
153-
isinstance(node, parsetree.ControlLine)
154-
and control_frame.is_ternary(node.keyword)
142+
if (
143+
not (
144+
isinstance(node, parsetree.ControlLine)
145+
and control_frame.is_ternary(node.keyword)
146+
)
147+
and self.ternary_stack
148+
and self.ternary_stack[-1]
155149
):
156-
if self.ternary_stack and self.ternary_stack[-1]:
157-
self.ternary_stack[-1][-1].nodes.append(node)
150+
self.ternary_stack[-1][-1].nodes.append(node)
158151
if isinstance(node, parsetree.Tag):
159152
if len(self.tag):
160153
node.parent = self.tag[-1]
@@ -207,11 +200,7 @@ def decode_raw_stream(self, text, decode_raw, known_encoding, filename):
207200
)
208201
else:
209202
m = self._coding_re.match(text.decode("utf-8", "ignore"))
210-
if m:
211-
parsed_encoding = m.group(1)
212-
else:
213-
parsed_encoding = known_encoding or "utf-8"
214-
203+
parsed_encoding = m.group(1) if m else known_encoding or "utf-8"
215204
if decode_raw:
216205
try:
217206
text = text.decode(parsed_encoding)
@@ -301,35 +290,34 @@ def match_tag_start(self):
301290
re.I | re.S | re.X,
302291
)
303292

304-
if match:
305-
keyword, attr, isend = match.groups()
306-
self.keyword = keyword
307-
attributes = {}
308-
if attr:
309-
for att in re.findall(
310-
r"\s*(\w+)\s*=\s*(?:'([^']*)'|\"([^\"]*)\")", attr
311-
):
312-
key, val1, val2 = att
313-
text = val1 or val2
314-
text = text.replace("\r\n", "\n")
315-
attributes[key] = text
316-
self.append_node(parsetree.Tag, keyword, attributes)
317-
if isend:
318-
self.tag.pop()
319-
else:
320-
if keyword == "text":
321-
match = self.match(r"(.*?)(?=\</%text>)", re.S)
322-
if not match:
323-
raise exceptions.SyntaxException(
324-
"Unclosed tag: <%%%s>" % self.tag[-1].keyword,
325-
**self.exception_kwargs,
326-
)
327-
self.append_node(parsetree.Text, match.group(1))
328-
return self.match_tag_end()
329-
return True
330-
else:
293+
if not match:
331294
return False
332295

296+
keyword, attr, isend = match.groups()
297+
self.keyword = keyword
298+
attributes = {}
299+
if attr:
300+
for att in re.findall(
301+
r"\s*(\w+)\s*=\s*(?:'([^']*)'|\"([^\"]*)\")", attr
302+
):
303+
key, val1, val2 = att
304+
text = val1 or val2
305+
text = text.replace("\r\n", "\n")
306+
attributes[key] = text
307+
self.append_node(parsetree.Tag, keyword, attributes)
308+
if isend:
309+
self.tag.pop()
310+
elif keyword == "text":
311+
match = self.match(r"(.*?)(?=\</%text>)", re.S)
312+
if not match:
313+
raise exceptions.SyntaxException(
314+
"Unclosed tag: <%%%s>" % self.tag[-1].keyword,
315+
**self.exception_kwargs
316+
)
317+
self.append_node(parsetree.Text, match.group(1))
318+
return self.match_tag_end()
319+
return True
320+
333321
def match_tag_end(self):
334322
match = self.match(r"\</%[\t ]*(.+?)[\t ]*>")
335323
if match:
@@ -352,15 +340,15 @@ def match_tag_end(self):
352340

353341
def match_end(self):
354342
match = self.match(r"\Z", re.S)
355-
if match:
356-
string = match.group()
357-
if string:
358-
return string
359-
else:
360-
return True
361-
else:
343+
if not match:
362344
return False
363345

346+
string = match.group()
347+
if string:
348+
return string
349+
else:
350+
return True
351+
364352
def match_text(self):
365353
match = self.match(
366354
r"""
@@ -411,63 +399,63 @@ def match_python_block(self):
411399

412400
def match_expression(self):
413401
match = self.match(r"\${")
414-
if match:
415-
line, pos = self.matched_lineno, self.matched_charpos
416-
text, end = self.parse_until_text(True, r"\|", r"}")
417-
if end == "|":
418-
escapes, end = self.parse_until_text(True, r"}")
419-
else:
420-
escapes = ""
421-
text = text.replace("\r\n", "\n")
422-
self.append_node(
423-
parsetree.Expression,
424-
text,
425-
escapes.strip(),
426-
lineno=line,
427-
pos=pos,
428-
)
429-
return True
430-
else:
402+
if not match:
431403
return False
432404

405+
line, pos = self.matched_lineno, self.matched_charpos
406+
text, end = self.parse_until_text(True, r"\|", r"}")
407+
if end == "|":
408+
escapes, end = self.parse_until_text(True, r"}")
409+
else:
410+
escapes = ""
411+
text = text.replace("\r\n", "\n")
412+
self.append_node(
413+
parsetree.Expression,
414+
text,
415+
escapes.strip(),
416+
lineno=line,
417+
pos=pos,
418+
)
419+
return True
420+
433421
def match_control_line(self):
434422
match = self.match(
435423
r"(?<=^)[\t ]*(%(?!%)|##)[\t ]*((?:(?:\\r?\n)|[^\r\n])*)"
436424
r"(?:\r?\n|\Z)",
437425
re.M,
438426
)
439-
if match:
440-
operator = match.group(1)
441-
text = match.group(2)
442-
if operator == "%":
443-
m2 = re.match(r"(end)?(\w+)\s*(.*)", text)
444-
if not m2:
427+
if not match:
428+
return False
429+
430+
operator = match.group(1)
431+
text = match.group(2)
432+
if operator == "%":
433+
m2 = re.match(r"(end)?(\w+)\s*(.*)", text)
434+
if not m2:
435+
raise exceptions.SyntaxException(
436+
"Invalid control line: '%s'" % text,
437+
**self.exception_kwargs
438+
)
439+
isend, keyword = m2.group(1, 2)
440+
isend = isend is not None
441+
442+
if isend:
443+
if not len(self.control_line):
445444
raise exceptions.SyntaxException(
446-
"Invalid control line: '%s'" % text,
447-
**self.exception_kwargs,
445+
"No starting keyword '%s' for '%s'"
446+
% (keyword, text),
447+
**self.exception_kwargs
448448
)
449-
isend, keyword = m2.group(1, 2)
450-
isend = isend is not None
451-
452-
if isend:
453-
if not len(self.control_line):
454-
raise exceptions.SyntaxException(
455-
"No starting keyword '%s' for '%s'"
456-
% (keyword, text),
457-
**self.exception_kwargs,
458-
)
459-
elif self.control_line[-1].keyword != keyword:
460-
raise exceptions.SyntaxException(
461-
"Keyword '%s' doesn't match keyword '%s'"
462-
% (text, self.control_line[-1].keyword),
463-
**self.exception_kwargs,
464-
)
465-
self.append_node(parsetree.ControlLine, keyword, isend, text)
466-
else:
467-
self.append_node(parsetree.Comment, text)
468-
return True
449+
elif self.control_line[-1].keyword != keyword:
450+
raise exceptions.SyntaxException(
451+
"Keyword '%s' doesn't match keyword '%s'"
452+
% (text, self.control_line[-1].keyword),
453+
**self.exception_kwargs
454+
)
455+
self.append_node(parsetree.ControlLine, keyword, isend, text)
469456
else:
470-
return False
457+
self.append_node(parsetree.Comment, text)
458+
return True
471459

472460
def match_comment(self):
473461
"""matches the multiline version of a comment"""

mako/lookup.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -262,15 +262,14 @@ def adjust_uri(self, uri, relativeto):
262262
if key in self._uri_cache:
263263
return self._uri_cache[key]
264264

265-
if uri[0] != "/":
266-
if relativeto is not None:
267-
v = self._uri_cache[key] = posixpath.join(
268-
posixpath.dirname(relativeto), uri
269-
)
270-
else:
271-
v = self._uri_cache[key] = "/" + uri
272-
else:
265+
if uri[0] == "/":
273266
v = self._uri_cache[key] = uri
267+
elif relativeto is not None:
268+
v = self._uri_cache[key] = posixpath.join(
269+
posixpath.dirname(relativeto), uri
270+
)
271+
else:
272+
v = self._uri_cache[key] = "/" + uri
274273
return v
275274

276275
def filename_to_uri(self, filename):
@@ -334,11 +333,10 @@ def _check(self, uri, template):
334333

335334
try:
336335
template_stat = os.stat(template.filename)
337-
if template.module._modified_time < template_stat[stat.ST_MTIME]:
338-
self._collection.pop(uri, None)
339-
return self._load(template.filename, uri)
340-
else:
336+
if template.module._modified_time >= template_stat[stat.ST_MTIME]:
341337
return template
338+
self._collection.pop(uri, None)
339+
return self._load(template.filename, uri)
342340
except OSError:
343341
self._collection.pop(uri, None)
344342
raise exceptions.TemplateLookupException(

0 commit comments

Comments
 (0)