2016-08-02 03:00:12 +02:00
|
|
|
from collections import defaultdict
|
2017-03-03 19:01:52 +01:00
|
|
|
from typing import Dict, List, Set
|
2016-08-02 03:00:12 +02:00
|
|
|
|
2016-09-11 20:23:29 +02:00
|
|
|
from .html_branches import html_branches, HtmlTreeBranch
|
2016-08-02 03:00:12 +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 show_all_branches(fns: List[str]) -> None:
|
2016-08-02 03:00:12 +02:00
|
|
|
for fn in fns:
|
|
|
|
print(fn)
|
2020-04-09 21:51:58 +02:00
|
|
|
with open(fn) as f:
|
2019-07-14 21:37:08 +02:00
|
|
|
text = f.read()
|
2016-09-11 20:23:29 +02:00
|
|
|
branches = html_branches(text, fn=fn)
|
2016-08-02 03:00:12 +02:00
|
|
|
for branch in branches:
|
|
|
|
print(branch.text())
|
|
|
|
print('---')
|
|
|
|
|
2017-11-05 11:57:15 +01:00
|
|
|
class Grepper:
|
2016-08-02 03:00:12 +02:00
|
|
|
'''
|
|
|
|
A Grepper object is optimized to do repeated
|
|
|
|
searches of words that can be found in our
|
|
|
|
HtmlTreeBranch objects.
|
|
|
|
'''
|
|
|
|
|
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, fns: List[str]) -> None:
|
2017-05-07 16:55:13 +02:00
|
|
|
all_branches = [] # type: List[HtmlTreeBranch]
|
2016-08-02 03:00:12 +02:00
|
|
|
|
|
|
|
for fn in fns:
|
2020-04-09 21:51:58 +02:00
|
|
|
with open(fn) as f:
|
2019-07-14 21:37:08 +02:00
|
|
|
text = f.read()
|
2016-09-11 20:23:29 +02:00
|
|
|
branches = html_branches(text, fn=fn)
|
2016-08-02 03:00:12 +02:00
|
|
|
all_branches += branches
|
|
|
|
|
2017-05-07 16:55:13 +02:00
|
|
|
self.word_dict = defaultdict(set) # type: Dict[str, Set[HtmlTreeBranch]]
|
2016-08-02 03:00:12 +02:00
|
|
|
for b in all_branches:
|
|
|
|
for word in b.words:
|
|
|
|
self.word_dict[word].add(b)
|
|
|
|
|
|
|
|
self.all_branches = set(all_branches)
|
|
|
|
|
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 grep(self, word_set: Set[str]) -> None:
|
2016-08-02 03:00:12 +02:00
|
|
|
|
2017-05-07 16:55:13 +02:00
|
|
|
words = list(word_set) # type: List[str]
|
2016-08-02 03:00:12 +02:00
|
|
|
|
|
|
|
if len(words) == 0:
|
|
|
|
matches = self.all_branches
|
|
|
|
else:
|
|
|
|
matches = self.word_dict[words[0]]
|
|
|
|
for i in range(1, len(words)):
|
|
|
|
matches = matches & self.word_dict[words[i]]
|
|
|
|
|
|
|
|
branches = list(matches)
|
|
|
|
branches.sort(key=lambda branch: (branch.fn, branch.line))
|
|
|
|
for branch in branches:
|
|
|
|
print('%s %d' % (branch.fn, branch.line))
|
|
|
|
print(branch.staircase_text())
|
|
|
|
print('')
|
|
|
|
|
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 grep(fns: List[str], words: Set[str]) -> None:
|
2016-08-02 03:00:12 +02:00
|
|
|
grepper = Grepper(fns)
|
|
|
|
grepper.grep(words)
|