2016-09-11 20:23:29 +02:00
|
|
|
import re
|
2017-01-06 15:11:15 +01:00
|
|
|
from collections import defaultdict
|
2021-12-02 14:30:52 +01:00
|
|
|
from typing import Dict, List
|
2016-09-11 20:23:29 +02:00
|
|
|
|
2022-11-17 09:30:48 +01:00
|
|
|
from .template_parser import FormattedError, Token, tokenize
|
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,
|
2021-02-12 08:20:45 +01:00
|
|
|
*("." + s for s in classes),
|
|
|
|
*("#" + s for s in ids),
|
2020-09-02 06:59:07 +02:00
|
|
|
]
|
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:
|
2021-02-12 08:20:45 +01:00
|
|
|
s += "." + ".".join(self.classes)
|
2016-09-11 20:23:29 +02:00
|
|
|
if self.ids:
|
2021-02-12 08:20:45 +01:00
|
|
|
s += "#" + "#".join(self.ids)
|
2016-09-11 20:23:29 +02:00
|
|
|
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 = []
|
2021-02-12 08:20:45 +01:00
|
|
|
s = ""
|
2017-01-06 15:11:15 +01:00
|
|
|
|
|
|
|
for ch in element:
|
2021-02-12 08:20:45 +01:00
|
|
|
if ch == "{":
|
2017-01-06 15:11:15 +01:00
|
|
|
outside_braces = False
|
2021-02-12 08:20:45 +01:00
|
|
|
if ch == "}":
|
2017-01-06 15:11:15 +01:00
|
|
|
outside_braces = True
|
2021-02-12 08:20:45 +01:00
|
|
|
if ch == " " and outside_braces:
|
2023-01-18 03:30:35 +01:00
|
|
|
if s != "":
|
2017-01-06 15:11:15 +01:00
|
|
|
lst.append(s)
|
2021-02-12 08:20:45 +01:00
|
|
|
s = ""
|
2017-01-06 15:11:15 +01:00
|
|
|
else:
|
|
|
|
s += ch
|
2023-01-18 03:30:35 +01:00
|
|
|
if s != "":
|
2017-01-06 15:11:15 +01:00
|
|
|
lst.append(s)
|
|
|
|
|
|
|
|
return lst
|
|
|
|
|
|
|
|
|
2023-02-02 04:35:24 +01: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)
|
2022-11-17 09:30:48 +01:00
|
|
|
except FormattedError as e:
|
2021-02-12 08:19:30 +01:00
|
|
|
raise Exception(
|
2021-02-12 08:20:45 +01:00
|
|
|
f"""
|
2020-06-13 08:57:35 +02:00
|
|
|
fn: {fn}
|
2021-02-12 08:20:45 +01:00
|
|
|
{e}"""
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
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
|