Linter updates to py version

This commit is contained in:
IamTheFij 2022-01-27 19:50:31 -08:00
parent 1e9b9d9685
commit 6cf522966e

View File

@ -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):