2016-09-11 20:23:29 +02:00
|
|
|
import re
|
2017-01-06 15:11:15 +01:00
|
|
|
from collections import defaultdict
|
2020-06-13 03:34:01 +02:00
|
|
|
from typing import Dict, List, Optional, Sequence, Set
|
2016-09-11 20:23:29 +02:00
|
|
|
|
2020-06-11 00:54:34 +02:00
|
|
|
from .template_parser import FormattedException, Token, tokenize
|
2016-09-11 20:23:29 +02:00
|
|
|
|
|
|
|
|
|
|
|
class HtmlBranchesException(Exception):
|
|
|
|
# TODO: Have callers pass in line numbers.
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2017-11-05 11:57:15 +01:00
|
|
|
class HtmlTreeBranch:
|
2016-09-11 20:23:29 +02:00
|
|
|
"""
|
|
|
|
For <p><div id='yo'>bla<span class='bar'></span></div></p>, store a
|
|
|
|
representation of the tags all the way down to the leaf, which would
|
|
|
|
conceptually be something like "p div(#yo) span(.bar)".
|
|
|
|
"""
|
|
|
|
|
python: Convert function type annotations to Python 3 style.
Generated by com2ann (slightly patched to avoid also converting
assignment type annotations, which require Python 3.6), followed by
some manual whitespace adjustment, and six fixes for runtime issues:
- def __init__(self, token: Token, parent: Optional[Node]) -> None:
+ def __init__(self, token: Token, parent: "Optional[Node]") -> None:
-def main(options: argparse.Namespace) -> NoReturn:
+def main(options: argparse.Namespace) -> "NoReturn":
-def fetch_request(url: str, callback: Any, **kwargs: Any) -> Generator[Callable[..., Any], Any, None]:
+def fetch_request(url: str, callback: Any, **kwargs: Any) -> "Generator[Callable[..., Any], Any, None]":
-def assert_server_running(server: subprocess.Popen[bytes], log_file: Optional[str]) -> None:
+def assert_server_running(server: "subprocess.Popen[bytes]", log_file: Optional[str]) -> None:
-def server_is_up(server: subprocess.Popen[bytes], log_file: Optional[str]) -> bool:
+def server_is_up(server: "subprocess.Popen[bytes]", log_file: Optional[str]) -> bool:
- method_kwarg_pairs: List[FuncKwargPair],
+ method_kwarg_pairs: "List[FuncKwargPair]",
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-19 03:48:37 +02:00
|
|
|
def __init__(self, tags: List['TagInfo'], fn: Optional[str]) -> None:
|
2016-09-11 20:23:29 +02:00
|
|
|
self.tags = tags
|
|
|
|
self.fn = fn
|
|
|
|
self.line = tags[-1].token.line
|
|
|
|
|
2020-04-22 01:09:50 +02:00
|
|
|
self.words: Set[str] = set()
|
2016-09-11 20:23:29 +02:00
|
|
|
for tag in tags:
|
|
|
|
for word in tag.words:
|
|
|
|
self.words.add(word)
|
|
|
|
|
python: Convert function type annotations to Python 3 style.
Generated by com2ann (slightly patched to avoid also converting
assignment type annotations, which require Python 3.6), followed by
some manual whitespace adjustment, and six fixes for runtime issues:
- def __init__(self, token: Token, parent: Optional[Node]) -> None:
+ def __init__(self, token: Token, parent: "Optional[Node]") -> None:
-def main(options: argparse.Namespace) -> NoReturn:
+def main(options: argparse.Namespace) -> "NoReturn":
-def fetch_request(url: str, callback: Any, **kwargs: Any) -> Generator[Callable[..., Any], Any, None]:
+def fetch_request(url: str, callback: Any, **kwargs: Any) -> "Generator[Callable[..., Any], Any, None]":
-def assert_server_running(server: subprocess.Popen[bytes], log_file: Optional[str]) -> None:
+def assert_server_running(server: "subprocess.Popen[bytes]", log_file: Optional[str]) -> None:
-def server_is_up(server: subprocess.Popen[bytes], log_file: Optional[str]) -> bool:
+def server_is_up(server: "subprocess.Popen[bytes]", log_file: Optional[str]) -> bool:
- method_kwarg_pairs: List[FuncKwargPair],
+ method_kwarg_pairs: "List[FuncKwargPair]",
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-19 03:48:37 +02:00
|
|
|
def staircase_text(self) -> str:
|
2016-09-11 20:23:29 +02:00
|
|
|
"""
|
|
|
|
produces representation of a node in staircase-like format:
|
|
|
|
|
|
|
|
html
|
|
|
|
body.main-section
|
|
|
|
p#intro
|
|
|
|
|
|
|
|
"""
|
|
|
|
res = '\n'
|
|
|
|
indent = ' ' * 4
|
|
|
|
for t in self.tags:
|
|
|
|
res += indent + t.text() + '\n'
|
|
|
|
indent += ' ' * 4
|
|
|
|
return res
|
|
|
|
|
python: Convert function type annotations to Python 3 style.
Generated by com2ann (slightly patched to avoid also converting
assignment type annotations, which require Python 3.6), followed by
some manual whitespace adjustment, and six fixes for runtime issues:
- def __init__(self, token: Token, parent: Optional[Node]) -> None:
+ def __init__(self, token: Token, parent: "Optional[Node]") -> None:
-def main(options: argparse.Namespace) -> NoReturn:
+def main(options: argparse.Namespace) -> "NoReturn":
-def fetch_request(url: str, callback: Any, **kwargs: Any) -> Generator[Callable[..., Any], Any, None]:
+def fetch_request(url: str, callback: Any, **kwargs: Any) -> "Generator[Callable[..., Any], Any, None]":
-def assert_server_running(server: subprocess.Popen[bytes], log_file: Optional[str]) -> None:
+def assert_server_running(server: "subprocess.Popen[bytes]", log_file: Optional[str]) -> None:
-def server_is_up(server: subprocess.Popen[bytes], log_file: Optional[str]) -> bool:
+def server_is_up(server: "subprocess.Popen[bytes]", log_file: Optional[str]) -> bool:
- method_kwarg_pairs: List[FuncKwargPair],
+ method_kwarg_pairs: "List[FuncKwargPair]",
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-19 03:48:37 +02:00
|
|
|
def text(self) -> str:
|
2016-09-11 20:23:29 +02:00
|
|
|
"""
|
|
|
|
produces one-line representation of branch:
|
|
|
|
|
|
|
|
html body.main-section p#intro
|
|
|
|
"""
|
|
|
|
return ' '.join(t.text() for t in self.tags)
|
|
|
|
|
|
|
|
|
2017-11-05 11:57:15 +01:00
|
|
|
class Node:
|
2020-07-05 02:59:37 +02:00
|
|
|
def __init__(self, token: Optional[Token], parent: "Optional[Node]") -> None:
|
python: Convert function type annotations to Python 3 style.
Generated by com2ann (slightly patched to avoid also converting
assignment type annotations, which require Python 3.6), followed by
some manual whitespace adjustment, and six fixes for runtime issues:
- def __init__(self, token: Token, parent: Optional[Node]) -> None:
+ def __init__(self, token: Token, parent: "Optional[Node]") -> None:
-def main(options: argparse.Namespace) -> NoReturn:
+def main(options: argparse.Namespace) -> "NoReturn":
-def fetch_request(url: str, callback: Any, **kwargs: Any) -> Generator[Callable[..., Any], Any, None]:
+def fetch_request(url: str, callback: Any, **kwargs: Any) -> "Generator[Callable[..., Any], Any, None]":
-def assert_server_running(server: subprocess.Popen[bytes], log_file: Optional[str]) -> None:
+def assert_server_running(server: "subprocess.Popen[bytes]", log_file: Optional[str]) -> None:
-def server_is_up(server: subprocess.Popen[bytes], log_file: Optional[str]) -> bool:
+def server_is_up(server: "subprocess.Popen[bytes]", log_file: Optional[str]) -> bool:
- method_kwarg_pairs: List[FuncKwargPair],
+ method_kwarg_pairs: "List[FuncKwargPair]",
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-19 03:48:37 +02:00
|
|
|
# FIXME parent parameter is not used!
|
2016-09-11 20:23:29 +02:00
|
|
|
self.token = token
|
2020-04-22 01:09:50 +02:00
|
|
|
self.children: List[Node] = []
|
|
|
|
self.parent: Optional[Node] = None
|
2016-09-11 20:23:29 +02:00
|
|
|
|
|
|
|
|
2017-11-05 11:57:15 +01:00
|
|
|
class TagInfo:
|
python: Convert function type annotations to Python 3 style.
Generated by com2ann (slightly patched to avoid also converting
assignment type annotations, which require Python 3.6), followed by
some manual whitespace adjustment, and six fixes for runtime issues:
- def __init__(self, token: Token, parent: Optional[Node]) -> None:
+ def __init__(self, token: Token, parent: "Optional[Node]") -> None:
-def main(options: argparse.Namespace) -> NoReturn:
+def main(options: argparse.Namespace) -> "NoReturn":
-def fetch_request(url: str, callback: Any, **kwargs: Any) -> Generator[Callable[..., Any], Any, None]:
+def fetch_request(url: str, callback: Any, **kwargs: Any) -> "Generator[Callable[..., Any], Any, None]":
-def assert_server_running(server: subprocess.Popen[bytes], log_file: Optional[str]) -> None:
+def assert_server_running(server: "subprocess.Popen[bytes]", log_file: Optional[str]) -> None:
-def server_is_up(server: subprocess.Popen[bytes], log_file: Optional[str]) -> bool:
+def server_is_up(server: "subprocess.Popen[bytes]", log_file: Optional[str]) -> bool:
- method_kwarg_pairs: List[FuncKwargPair],
+ method_kwarg_pairs: "List[FuncKwargPair]",
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-19 03:48:37 +02:00
|
|
|
def __init__(self, tag: str, classes: List[str], ids: List[str], token: Token) -> None:
|
2016-09-11 20:23:29 +02:00
|
|
|
self.tag = tag
|
|
|
|
self.classes = classes
|
|
|
|
self.ids = ids
|
|
|
|
self.token = token
|
2020-09-02 06:59:07 +02:00
|
|
|
self.words = [
|
|
|
|
self.tag,
|
|
|
|
*('.' + s for s in classes),
|
|
|
|
*('#' + s for s in ids),
|
|
|
|
]
|
2016-09-11 20:23:29 +02:00
|
|
|
|
python: Convert function type annotations to Python 3 style.
Generated by com2ann (slightly patched to avoid also converting
assignment type annotations, which require Python 3.6), followed by
some manual whitespace adjustment, and six fixes for runtime issues:
- def __init__(self, token: Token, parent: Optional[Node]) -> None:
+ def __init__(self, token: Token, parent: "Optional[Node]") -> None:
-def main(options: argparse.Namespace) -> NoReturn:
+def main(options: argparse.Namespace) -> "NoReturn":
-def fetch_request(url: str, callback: Any, **kwargs: Any) -> Generator[Callable[..., Any], Any, None]:
+def fetch_request(url: str, callback: Any, **kwargs: Any) -> "Generator[Callable[..., Any], Any, None]":
-def assert_server_running(server: subprocess.Popen[bytes], log_file: Optional[str]) -> None:
+def assert_server_running(server: "subprocess.Popen[bytes]", log_file: Optional[str]) -> None:
-def server_is_up(server: subprocess.Popen[bytes], log_file: Optional[str]) -> bool:
+def server_is_up(server: "subprocess.Popen[bytes]", log_file: Optional[str]) -> bool:
- method_kwarg_pairs: List[FuncKwargPair],
+ method_kwarg_pairs: "List[FuncKwargPair]",
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-19 03:48:37 +02:00
|
|
|
def text(self) -> str:
|
2016-09-11 20:23:29 +02:00
|
|
|
s = self.tag
|
|
|
|
if self.classes:
|
|
|
|
s += '.' + '.'.join(self.classes)
|
|
|
|
if self.ids:
|
|
|
|
s += '#' + '#'.join(self.ids)
|
|
|
|
return s
|
|
|
|
|
|
|
|
|
python: Convert function type annotations to Python 3 style.
Generated by com2ann (slightly patched to avoid also converting
assignment type annotations, which require Python 3.6), followed by
some manual whitespace adjustment, and six fixes for runtime issues:
- def __init__(self, token: Token, parent: Optional[Node]) -> None:
+ def __init__(self, token: Token, parent: "Optional[Node]") -> None:
-def main(options: argparse.Namespace) -> NoReturn:
+def main(options: argparse.Namespace) -> "NoReturn":
-def fetch_request(url: str, callback: Any, **kwargs: Any) -> Generator[Callable[..., Any], Any, None]:
+def fetch_request(url: str, callback: Any, **kwargs: Any) -> "Generator[Callable[..., Any], Any, None]":
-def assert_server_running(server: subprocess.Popen[bytes], log_file: Optional[str]) -> None:
+def assert_server_running(server: "subprocess.Popen[bytes]", log_file: Optional[str]) -> None:
-def server_is_up(server: subprocess.Popen[bytes], log_file: Optional[str]) -> bool:
+def server_is_up(server: "subprocess.Popen[bytes]", log_file: Optional[str]) -> bool:
- method_kwarg_pairs: List[FuncKwargPair],
+ method_kwarg_pairs: "List[FuncKwargPair]",
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-19 03:48:37 +02:00
|
|
|
def get_tag_info(token: Token) -> TagInfo:
|
2016-09-11 20:23:29 +02:00
|
|
|
s = token.s
|
|
|
|
tag = token.tag
|
2020-04-22 01:09:50 +02:00
|
|
|
classes: List[str] = []
|
|
|
|
ids: List[str] = []
|
2016-09-11 20:23:29 +02:00
|
|
|
|
|
|
|
searches = [
|
|
|
|
(classes, ' class="(.*?)"'),
|
|
|
|
(classes, " class='(.*?)'"),
|
|
|
|
(ids, ' id="(.*?)"'),
|
|
|
|
(ids, " id='(.*?)'"),
|
|
|
|
]
|
|
|
|
|
|
|
|
for lst, regex in searches:
|
|
|
|
m = re.search(regex, s)
|
|
|
|
if m:
|
|
|
|
for g in m.groups():
|
2017-01-06 15:11:15 +01:00
|
|
|
lst += split_for_id_and_class(g)
|
2016-09-11 20:23:29 +02:00
|
|
|
|
|
|
|
return TagInfo(tag=tag, classes=classes, ids=ids, token=token)
|
|
|
|
|
|
|
|
|
python: Convert function type annotations to Python 3 style.
Generated by com2ann (slightly patched to avoid also converting
assignment type annotations, which require Python 3.6), followed by
some manual whitespace adjustment, and six fixes for runtime issues:
- def __init__(self, token: Token, parent: Optional[Node]) -> None:
+ def __init__(self, token: Token, parent: "Optional[Node]") -> None:
-def main(options: argparse.Namespace) -> NoReturn:
+def main(options: argparse.Namespace) -> "NoReturn":
-def fetch_request(url: str, callback: Any, **kwargs: Any) -> Generator[Callable[..., Any], Any, None]:
+def fetch_request(url: str, callback: Any, **kwargs: Any) -> "Generator[Callable[..., Any], Any, None]":
-def assert_server_running(server: subprocess.Popen[bytes], log_file: Optional[str]) -> None:
+def assert_server_running(server: "subprocess.Popen[bytes]", log_file: Optional[str]) -> None:
-def server_is_up(server: subprocess.Popen[bytes], log_file: Optional[str]) -> bool:
+def server_is_up(server: "subprocess.Popen[bytes]", log_file: Optional[str]) -> bool:
- method_kwarg_pairs: List[FuncKwargPair],
+ method_kwarg_pairs: "List[FuncKwargPair]",
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-19 03:48:37 +02:00
|
|
|
def split_for_id_and_class(element: str) -> List[str]:
|
2017-01-06 15:11:15 +01:00
|
|
|
# Here we split a given string which is expected to contain id or class
|
|
|
|
# attributes from HTML tags. This also takes care of template variables
|
|
|
|
# in string during splitting process. For eg. 'red black {{ a|b|c }}'
|
|
|
|
# is split as ['red', 'black', '{{ a|b|c }}']
|
2020-04-22 01:09:50 +02:00
|
|
|
outside_braces: bool = True
|
2017-01-06 15:11:15 +01:00
|
|
|
lst = []
|
|
|
|
s = ''
|
|
|
|
|
|
|
|
for ch in element:
|
|
|
|
if ch == '{':
|
|
|
|
outside_braces = False
|
|
|
|
if ch == '}':
|
|
|
|
outside_braces = True
|
|
|
|
if ch == ' ' and outside_braces:
|
|
|
|
if not s == '':
|
|
|
|
lst.append(s)
|
|
|
|
s = ''
|
|
|
|
else:
|
|
|
|
s += ch
|
|
|
|
if not s == '':
|
|
|
|
lst.append(s)
|
|
|
|
|
|
|
|
return lst
|
|
|
|
|
|
|
|
|
python: Convert function type annotations to Python 3 style.
Generated by com2ann (slightly patched to avoid also converting
assignment type annotations, which require Python 3.6), followed by
some manual whitespace adjustment, and six fixes for runtime issues:
- def __init__(self, token: Token, parent: Optional[Node]) -> None:
+ def __init__(self, token: Token, parent: "Optional[Node]") -> None:
-def main(options: argparse.Namespace) -> NoReturn:
+def main(options: argparse.Namespace) -> "NoReturn":
-def fetch_request(url: str, callback: Any, **kwargs: Any) -> Generator[Callable[..., Any], Any, None]:
+def fetch_request(url: str, callback: Any, **kwargs: Any) -> "Generator[Callable[..., Any], Any, None]":
-def assert_server_running(server: subprocess.Popen[bytes], log_file: Optional[str]) -> None:
+def assert_server_running(server: "subprocess.Popen[bytes]", log_file: Optional[str]) -> None:
-def server_is_up(server: subprocess.Popen[bytes], log_file: Optional[str]) -> bool:
+def server_is_up(server: "subprocess.Popen[bytes]", log_file: Optional[str]) -> bool:
- method_kwarg_pairs: List[FuncKwargPair],
+ method_kwarg_pairs: "List[FuncKwargPair]",
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-19 03:48:37 +02:00
|
|
|
def html_branches(text: str, fn: Optional[str] = None) -> List[HtmlTreeBranch]:
|
2020-04-18 11:48:00 +02:00
|
|
|
tree = html_tag_tree(text, fn)
|
2020-04-22 01:09:50 +02:00
|
|
|
branches: List[HtmlTreeBranch] = []
|
2016-09-11 20:23:29 +02:00
|
|
|
|
2020-06-13 03:34:01 +02:00
|
|
|
def walk(node: Node, tag_info_list: Sequence[TagInfo] = []) -> None:
|
2020-07-05 02:59:37 +02:00
|
|
|
assert node.token is not None
|
2016-09-11 20:23:29 +02:00
|
|
|
info = get_tag_info(node.token)
|
2020-06-13 03:34:01 +02:00
|
|
|
tag_info_list = [*tag_info_list, info]
|
2016-09-11 20:23:29 +02:00
|
|
|
|
|
|
|
if node.children:
|
|
|
|
for child in node.children:
|
|
|
|
walk(node=child, tag_info_list=tag_info_list)
|
|
|
|
else:
|
|
|
|
tree_branch = HtmlTreeBranch(tags=tag_info_list, fn=fn)
|
|
|
|
branches.append(tree_branch)
|
|
|
|
|
|
|
|
for node in tree.children:
|
2020-06-13 03:34:01 +02:00
|
|
|
walk(node, [])
|
2016-09-11 20:23:29 +02:00
|
|
|
|
|
|
|
return branches
|
|
|
|
|
|
|
|
|
2020-04-18 11:48:00 +02:00
|
|
|
def html_tag_tree(text: str, fn: Optional[str]=None) -> Node:
|
2016-09-11 20:23:29 +02:00
|
|
|
tokens = tokenize(text)
|
|
|
|
top_level = Node(token=None, parent=None)
|
|
|
|
stack = [top_level]
|
|
|
|
|
|
|
|
for token in tokens:
|
2016-09-11 22:36:55 +02:00
|
|
|
# Add tokens to the Node tree first (conditionally).
|
2016-09-11 20:23:29 +02:00
|
|
|
if token.kind in ('html_start', 'html_singleton'):
|
2016-09-11 22:36:55 +02:00
|
|
|
parent = stack[-1]
|
2016-11-28 23:29:01 +01:00
|
|
|
node = Node(token=token, parent=parent)
|
2016-09-11 22:36:55 +02:00
|
|
|
parent.children.append(node)
|
|
|
|
|
|
|
|
# Then update the stack to have the next node that
|
|
|
|
# we will be appending to at the top.
|
|
|
|
if token.kind == 'html_start':
|
|
|
|
stack.append(node)
|
2016-09-11 20:23:29 +02:00
|
|
|
elif token.kind == 'html_end':
|
|
|
|
stack.pop()
|
|
|
|
|
|
|
|
return top_level
|
2017-01-06 15:11:15 +01:00
|
|
|
|
|
|
|
|
python: Convert function type annotations to Python 3 style.
Generated by com2ann (slightly patched to avoid also converting
assignment type annotations, which require Python 3.6), followed by
some manual whitespace adjustment, and six fixes for runtime issues:
- def __init__(self, token: Token, parent: Optional[Node]) -> None:
+ def __init__(self, token: Token, parent: "Optional[Node]") -> None:
-def main(options: argparse.Namespace) -> NoReturn:
+def main(options: argparse.Namespace) -> "NoReturn":
-def fetch_request(url: str, callback: Any, **kwargs: Any) -> Generator[Callable[..., Any], Any, None]:
+def fetch_request(url: str, callback: Any, **kwargs: Any) -> "Generator[Callable[..., Any], Any, None]":
-def assert_server_running(server: subprocess.Popen[bytes], log_file: Optional[str]) -> None:
+def assert_server_running(server: "subprocess.Popen[bytes]", log_file: Optional[str]) -> None:
-def server_is_up(server: subprocess.Popen[bytes], log_file: Optional[str]) -> bool:
+def server_is_up(server: "subprocess.Popen[bytes]", log_file: Optional[str]) -> bool:
- method_kwarg_pairs: List[FuncKwargPair],
+ method_kwarg_pairs: "List[FuncKwargPair]",
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-19 03:48:37 +02:00
|
|
|
def build_id_dict(templates: List[str]) -> (Dict[str, List[str]]):
|
2020-04-22 01:09:50 +02:00
|
|
|
template_id_dict: (Dict[str, List[str]]) = defaultdict(list)
|
2017-01-06 15:11:15 +01:00
|
|
|
|
|
|
|
for fn in templates:
|
2020-04-09 21:51:58 +02:00
|
|
|
with open(fn) as f:
|
2019-07-14 21:37:08 +02:00
|
|
|
text = f.read()
|
2020-04-18 11:48:00 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
list_tags = tokenize(text)
|
|
|
|
except FormattedException as e:
|
2020-06-13 08:57:35 +02:00
|
|
|
raise Exception(f'''
|
|
|
|
fn: {fn}
|
|
|
|
{e}''')
|
2017-01-06 15:11:15 +01:00
|
|
|
|
|
|
|
for tag in list_tags:
|
|
|
|
info = get_tag_info(tag)
|
|
|
|
|
|
|
|
for ids in info.ids:
|
|
|
|
template_id_dict[ids].append("Line " + str(info.token.line) + ":" + fn)
|
|
|
|
|
|
|
|
return template_id_dict
|