Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring Code #335

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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:
yezz123 marked this conversation as resolved.
Show resolved Hide resolved
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)
CaselIT marked this conversation as resolved.
Show resolved Hide resolved
CaselIT marked this conversation as resolved.
Show resolved Hide resolved
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
3 changes: 1 addition & 2 deletions mako/ext/linguaplugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ def __call__(self, filename, options, fileobj=None):
else:
must_close = False
try:
for message in self.process_file(fileobj):
yield message
yield from self.process_file(fileobj)
finally:
if must_close:
fileobj.close()
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)
CaselIT marked this conversation as resolved.
Show resolved Hide resolved
except OSError:
self._collection.pop(uri, None)
raise exceptions.TemplateLookupException(
Expand Down