Skip to content

Commit

Permalink
Refactoring Code
Browse files Browse the repository at this point in the history
## [Setup.py](setup.py)

- Use `with` when opening file to ensure closure

## [Util.py](mako/util.py)

- Remove unnecessary else after guard condition
- Replace if statement with if expression
- Remove unnecessary else after guard condition
- Replace unneeded comprehension with generator
- Inline variable that is immediately returned

## [Template.py](mako/template.py)

- Replace if statement with if expression, Simplify if expression by using or
- Replace list(), dict() or set() with comprehension
- Swap if/else branches, Merge else clause's nested if statement into elif

## [Runtime.py](mako/runtime.py)

- Remove unnecessary else after guard condition

## [PyParser.py](mako/pyparser.py)

- Replace yield inside for loop with yield from
- Lift code into else after jump in control flow, Merge else clause's nested if statement into elif
- Replace if statement with if expression

## [Pygen.py](mako/pygen.py)

- Merge nested if conditions
- Simplify conditional into return statement (removes comment)
- Replace if statement with if expression, Simplify boolean if expression

## [Parsetree.py](mako/parsetree.py)

- Replace unneeded comprehension with generator
- Merge else clause's nested if statement into elif
- Replace unneeded comprehension with generator

## [Lookup.py](mako/lookup.py)

- Swap if/else branches, Merge else clause's nested if statement into elif
- Swap if/else branches, Remove unnecessary else after guard condition

## [Lexer.py](mako/lexer.py)

- Replace if statement with if expression
- Merge nested if conditions
- Swap if/else branches, Remove unnecessary else after guard condition, Merge else clause's nested if statement into elif
- Swap if/else branches, Remove unnecessary else after guard condition

## [Exceptions.py](mako/exceptions.py)

- Replace if statement with if expression, Use `with` when opening file to ensure closure

## [Ast.py](mako/ast.py)

- Replace multiple comparisons of same variable with `in` operator

## [Pygmentplugin.py](mako/ext/pygmentplugin.py)

- Replace if statement with if expression

Closes: #335
Pull-request: #335
Pull-request-sha: c6243b1

Change-Id: I9093e2f5ca4bb59aa12536b1a0bdf2d58514aa40
  • Loading branch information
CaselIT committed Oct 25, 2021
1 parent cb94b67 commit c47d172
Show file tree
Hide file tree
Showing 15 changed files with 187 additions and 227 deletions.
1 change: 0 additions & 1 deletion .github/workflows/run-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ jobs:
- "windows-latest"
- "macos-latest"
python-version:
- "3.6"
- "3.7"
- "3.8"
- "3.9"
Expand Down
2 changes: 1 addition & 1 deletion mako/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def __init__(self, code, **exception_kwargs):
code = code + "pass"
elif keyword == "try":
code = code + "pass\nexcept:pass"
elif keyword == "elif" or keyword == "else":
elif keyword in ["elif", "else"]:
code = "if False:pass\n" + code + "pass"
elif keyword == "except":
code = "try:pass\n" + code + "pass"
Expand Down
13 changes: 6 additions & 7 deletions mako/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,13 +225,12 @@ def _init(self, trcback):
if new_trcback:
try:
# A normal .py file (not a Template)
fp = open(new_trcback[-1][0], "rb")
encoding = util.parse_encoding(fp)
if not encoding:
encoding = "utf-8"
fp.seek(0)
self.source = fp.read()
fp.close()
with open(new_trcback[-1][0], "rb") as fp:
encoding = util.parse_encoding(fp)
if not encoding:
encoding = "utf-8"
fp.seek(0)
self.source = fp.read()
if encoding:
self.source = self.source.decode(encoding)
except IOError:
Expand Down
3 changes: 1 addition & 2 deletions mako/ext/babelplugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,4 @@ def extract(fileobj, keywords, comment_tags, options):
:rtype: ``iterator``
"""
extractor = BabelMakoExtractor(keywords, comment_tags, options)
for message in extractor(fileobj):
yield message
yield from extractor(fileobj)
6 changes: 2 additions & 4 deletions mako/ext/extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ def process_file(self, fileobj):
template_node = lexer.Lexer(
fileobj.read(), input_encoding=self.config["encoding"]
).parse()
for extracted in self.extract_nodes(template_node.get_children()):
yield extracted
yield from self.extract_nodes(template_node.get_children())

def extract_nodes(self, nodes):
translator_comments = []
Expand Down Expand Up @@ -118,8 +117,7 @@ def extract_nodes(self, nodes):
in_translator_comments = False

if child_nodes:
for extracted in self.extract_nodes(child_nodes):
yield extracted
yield from self.extract_nodes(child_nodes)

@staticmethod
def _split_comment(lineno, comment):
Expand Down
14 changes: 5 additions & 9 deletions mako/ext/linguaplugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# This module is part of Mako and is released under
# the MIT License: http://www.opensource.org/licenses/mit-license.php

import contextlib
import io

from lingua.extractors import Extractor
Expand All @@ -25,16 +26,11 @@ def __call__(self, filename, options, fileobj=None):
self.filename = filename
self.python_extractor = get_extractor("x.py")
if fileobj is None:
fileobj = open(filename, "r")
must_close = True
ctx = open(filename, "r")
else:
must_close = False
try:
for message in self.process_file(fileobj):
yield message
finally:
if must_close:
fileobj.close()
ctx = contextlib.nullcontext(fileobj)
with ctx as file_:
yield from self.process_file(file_)

def process_python(self, code, code_lineno, translator_strings):
source = code.getvalue().strip()
Expand Down
192 changes: 90 additions & 102 deletions mako/lexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,7 @@ def match(self, regexp, flags=None):
try:
reg = _regexp_cache[(regexp, flags)]
except KeyError:
if flags:
reg = re.compile(regexp, flags)
else:
reg = re.compile(regexp)
reg = re.compile(regexp, flags) if flags else re.compile(regexp)
_regexp_cache[(regexp, flags)] = reg

return self.match_reg(reg)
Expand All @@ -75,21 +72,14 @@ def match_reg(self, reg):
match = reg.match(self.text, self.match_position)
if match:
(start, end) = match.span()
if end == start:
self.match_position = end + 1
else:
self.match_position = end
self.match_position = end + 1 if end == start else end
self.matched_lineno = self.lineno
lines = re.findall(r"\n", self.text[mp : self.match_position])
cp = mp - 1
while cp >= 0 and cp < self.textlength and self.text[cp] != "\n":
cp -= 1
self.matched_charpos = mp - cp
self.lineno += len(lines)
# print "MATCHED:", match.group(0), "LINE START:",
# self.matched_lineno, "LINE END:", self.lineno
# print "MATCH:", regexp, "\n", self.text[mp : mp + 15], \
# (match and "TRUE" or "FALSE")
return match

def parse_until_text(self, watch_nesting, *text):
Expand Down Expand Up @@ -149,12 +139,15 @@ def append_node(self, nodecls, *args, **kwargs):
if self.control_line:
control_frame = self.control_line[-1]
control_frame.nodes.append(node)
if not (
isinstance(node, parsetree.ControlLine)
and control_frame.is_ternary(node.keyword)
if (
not (
isinstance(node, parsetree.ControlLine)
and control_frame.is_ternary(node.keyword)
)
and self.ternary_stack
and self.ternary_stack[-1]
):
if self.ternary_stack and self.ternary_stack[-1]:
self.ternary_stack[-1][-1].nodes.append(node)
self.ternary_stack[-1][-1].nodes.append(node)
if isinstance(node, parsetree.Tag):
if len(self.tag):
node.parent = self.tag[-1]
Expand Down Expand Up @@ -207,11 +200,7 @@ def decode_raw_stream(self, text, decode_raw, known_encoding, filename):
)
else:
m = self._coding_re.match(text.decode("utf-8", "ignore"))
if m:
parsed_encoding = m.group(1)
else:
parsed_encoding = known_encoding or "utf-8"

parsed_encoding = m.group(1) if m else known_encoding or "utf-8"
if decode_raw:
try:
text = text.decode(parsed_encoding)
Expand Down Expand Up @@ -301,35 +290,34 @@ def match_tag_start(self):
re.I | re.S | re.X,
)

if match:
keyword, attr, isend = match.groups()
self.keyword = keyword
attributes = {}
if attr:
for att in re.findall(
r"\s*(\w+)\s*=\s*(?:'([^']*)'|\"([^\"]*)\")", attr
):
key, val1, val2 = att
text = val1 or val2
text = text.replace("\r\n", "\n")
attributes[key] = text
self.append_node(parsetree.Tag, keyword, attributes)
if isend:
self.tag.pop()
else:
if keyword == "text":
match = self.match(r"(.*?)(?=\</%text>)", re.S)
if not match:
raise exceptions.SyntaxException(
"Unclosed tag: <%%%s>" % self.tag[-1].keyword,
**self.exception_kwargs,
)
self.append_node(parsetree.Text, match.group(1))
return self.match_tag_end()
return True
else:
if not match:
return False

keyword, attr, isend = match.groups()
self.keyword = keyword
attributes = {}
if attr:
for att in re.findall(
r"\s*(\w+)\s*=\s*(?:'([^']*)'|\"([^\"]*)\")", attr
):
key, val1, val2 = att
text = val1 or val2
text = text.replace("\r\n", "\n")
attributes[key] = text
self.append_node(parsetree.Tag, keyword, attributes)
if isend:
self.tag.pop()
elif keyword == "text":
match = self.match(r"(.*?)(?=\</%text>)", re.S)
if not match:
raise exceptions.SyntaxException(
"Unclosed tag: <%%%s>" % self.tag[-1].keyword,
**self.exception_kwargs
)
self.append_node(parsetree.Text, match.group(1))
return self.match_tag_end()
return True

def match_tag_end(self):
match = self.match(r"\</%[\t ]*(.+?)[\t ]*>")
if match:
Expand All @@ -352,15 +340,15 @@ def match_tag_end(self):

def match_end(self):
match = self.match(r"\Z", re.S)
if match:
string = match.group()
if string:
return string
else:
return True
else:
if not match:
return False

string = match.group()
if string:
return string
else:
return True

def match_text(self):
match = self.match(
r"""
Expand Down Expand Up @@ -411,63 +399,63 @@ def match_python_block(self):

def match_expression(self):
match = self.match(r"\${")
if match:
line, pos = self.matched_lineno, self.matched_charpos
text, end = self.parse_until_text(True, r"\|", r"}")
if end == "|":
escapes, end = self.parse_until_text(True, r"}")
else:
escapes = ""
text = text.replace("\r\n", "\n")
self.append_node(
parsetree.Expression,
text,
escapes.strip(),
lineno=line,
pos=pos,
)
return True
else:
if not match:
return False

line, pos = self.matched_lineno, self.matched_charpos
text, end = self.parse_until_text(True, r"\|", r"}")
if end == "|":
escapes, end = self.parse_until_text(True, r"}")
else:
escapes = ""
text = text.replace("\r\n", "\n")
self.append_node(
parsetree.Expression,
text,
escapes.strip(),
lineno=line,
pos=pos,
)
return True

def match_control_line(self):
match = self.match(
r"(?<=^)[\t ]*(%(?!%)|##)[\t ]*((?:(?:\\r?\n)|[^\r\n])*)"
r"(?:\r?\n|\Z)",
re.M,
)
if match:
operator = match.group(1)
text = match.group(2)
if operator == "%":
m2 = re.match(r"(end)?(\w+)\s*(.*)", text)
if not m2:
if not match:
return False

operator = match.group(1)
text = match.group(2)
if operator == "%":
m2 = re.match(r"(end)?(\w+)\s*(.*)", text)
if not m2:
raise exceptions.SyntaxException(
"Invalid control line: '%s'" % text,
**self.exception_kwargs
)
isend, keyword = m2.group(1, 2)
isend = isend is not None

if isend:
if not len(self.control_line):
raise exceptions.SyntaxException(
"Invalid control line: '%s'" % text,
**self.exception_kwargs,
"No starting keyword '%s' for '%s'"
% (keyword, text),
**self.exception_kwargs
)
isend, keyword = m2.group(1, 2)
isend = isend is not None

if isend:
if not len(self.control_line):
raise exceptions.SyntaxException(
"No starting keyword '%s' for '%s'"
% (keyword, text),
**self.exception_kwargs,
)
elif self.control_line[-1].keyword != keyword:
raise exceptions.SyntaxException(
"Keyword '%s' doesn't match keyword '%s'"
% (text, self.control_line[-1].keyword),
**self.exception_kwargs,
)
self.append_node(parsetree.ControlLine, keyword, isend, text)
else:
self.append_node(parsetree.Comment, text)
return True
elif self.control_line[-1].keyword != keyword:
raise exceptions.SyntaxException(
"Keyword '%s' doesn't match keyword '%s'"
% (text, self.control_line[-1].keyword),
**self.exception_kwargs
)
self.append_node(parsetree.ControlLine, keyword, isend, text)
else:
return False
self.append_node(parsetree.Comment, text)
return True

def match_comment(self):
"""matches the multiline version of a comment"""
Expand Down
22 changes: 10 additions & 12 deletions mako/lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,15 +262,14 @@ def adjust_uri(self, uri, relativeto):
if key in self._uri_cache:
return self._uri_cache[key]

if uri[0] != "/":
if relativeto is not None:
v = self._uri_cache[key] = posixpath.join(
posixpath.dirname(relativeto), uri
)
else:
v = self._uri_cache[key] = "/" + uri
else:
if uri[0] == "/":
v = self._uri_cache[key] = uri
elif relativeto is not None:
v = self._uri_cache[key] = posixpath.join(
posixpath.dirname(relativeto), uri
)
else:
v = self._uri_cache[key] = "/" + uri
return v

def filename_to_uri(self, filename):
Expand Down Expand Up @@ -334,11 +333,10 @@ def _check(self, uri, template):

try:
template_stat = os.stat(template.filename)
if template.module._modified_time < template_stat[stat.ST_MTIME]:
self._collection.pop(uri, None)
return self._load(template.filename, uri)
else:
if template.module._modified_time >= template_stat[stat.ST_MTIME]:
return template
self._collection.pop(uri, None)
return self._load(template.filename, uri)
except OSError:
self._collection.pop(uri, None)
raise exceptions.TemplateLookupException(
Expand Down
Loading

0 comments on commit c47d172

Please sign in to comment.