From 6cf522966e812a88dababc704af2dfdda7c8c59a Mon Sep 17 00:00:00 2001 From: Ian Fijolek Date: Thu, 27 Jan 2022 19:50:31 -0800 Subject: [PATCH] Linter updates to py version --- browser_ruler.py | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/browser_ruler.py b/browser_ruler.py index 2aad935..b659f5b 100755 --- a/browser_ruler.py +++ b/browser_ruler.py @@ -11,11 +11,14 @@ from urllib.parse import urlparse class Intent(NamedTuple): + """Intent contains a url requested to be opened""" url: str + # referrer isn't possible to be captured at this time referrer: str class BrowserRule(NamedTuple): + """BrowserRule is matches a url and launches it in a browser""" match_func: Callable[[Intent], bool] command: str args: List[str] = [] @@ -26,9 +29,11 @@ class BrowserRule(NamedTuple): def launch(self, intent: Intent) -> None: """Launch the command""" - Popen(["nohup", self.command, *self.args, intent.url], shell=False) + with Popen(["nohup", self.command, *self.args, intent.url], shell=False): + pass def maybe_launch(self, intent: Intent) -> bool: + """Launch URL in a browser if it matches the rules""" if self.matched(intent): self.launch(intent) return True @@ -43,10 +48,7 @@ MatchFunc = Callable[[Intent], bool] def match_regex(rules: List[str]) -> MatchFunc: """Returns a function to handle urls and match them using regex""" def match_func(intent: Intent) -> bool: - for rule in rules: - if match(rule, intent.url): - return True - return False + return any(match(rule, intent.url) for rule in rules) return match_func @@ -55,10 +57,10 @@ def match_host_regex(rules: List[str]) -> MatchFunc: """Returns a function to handle urls and match them using regex""" def match_func(intent: Intent) -> bool: url = urlparse(intent.url) - for rule in rules: - if match(rule, url.hostname): - return True - return False + if not url.hostname: + return False + + return any(match(rule, url.hostname) for rule in rules) return match_func @@ -76,18 +78,21 @@ def match_hostname(hostnames: List[str]) -> MatchFunc: def any_match(rules: Iterable[MatchFunc]) -> MatchFunc: + """Returns a functino that will check against any match rules + + Equivalent to using `any(...)`""" def match_func(intent: Intent) -> bool: return any(r(intent) for r in rules) return match_func -def default(x: Any) -> bool: +def default(_: Any) -> bool: """Always returns True""" return True -def noop(x: Any) -> bool: +def noop(_: Any) -> bool: """Always returns False""" return False @@ -183,11 +188,13 @@ browser_rules = [ def open_url(url: str, referrer=None) -> bool: + """Open a given url based on rule set""" intent = Intent(url, referrer) return any(rule.maybe_launch(intent) for rule in browser_rules) -def main(urls: List[str], referrer=None): +def main(urls: List[str]): + """Recieves list of urls and opens them each with provided rule sets""" failed_urls: List[str] = [] for url in urls: if not open_url(url):