From 1de7b738142ce1213d374ddfd5a86973feaacacc Mon Sep 17 00:00:00 2001 From: SoniEx2 Date: Wed, 26 Feb 2020 18:00:52 -0300 Subject: Create an URIPredicate --- ganarchy/config.py | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) (limited to 'ganarchy') diff --git a/ganarchy/config.py b/ganarchy/config.py index f1a0cd6..f753698 100644 --- a/ganarchy/config.py +++ b/ganarchy/config.py @@ -21,13 +21,34 @@ import abdl import qtoml from enum import Enum +from urllib.parse import urlparse + +class URIPredicate(abdl.predicates.Predicate): + def __init__(self, ports=range(1,65536), schemes=('http', 'https')): + self.ports = ports + self.schemes = schemes + + def accept(self, obj): + try: + u = urlparse(obj) + if not u: + return False + # also raises for invalid ports, see https://docs.python.org/3/library/urllib.parse.html#urllib.parse.urlparse + # "Reading the port attribute will raise a ValueError if an invalid port is specified in the URL. [...]" + if u.port is not None and u.port not in self.ports: + return False + if u.scheme not in self.schemes: + return False + except ValueError: + return False + return True # sanitize = skip invalid entries # validate = error on invalid entries CONFIG_REPOS_SANITIZE = abdl.compile("""->'projects'?:?$dict ->commit/[0-9a-fA-F]{40}|[0-9a-fA-F]{64}/?:?$dict ->url[:?$uri]:?$dict - ->branch:?$dict(->'active'?:?$bool)""", {'bool': bool, 'dict': dict, 'uri': object})#URIValidator}) + ->branch:?$dict(->'active'?:?$bool)""", {'bool': bool, 'dict': dict, 'uri': URIPredicate()}) CONFIG_REPOS = abdl.compile("->'projects'->commit->url->branch", {'dict': dict}) CONFIG_TITLE_SANITIZE = abdl.compile("""->title'title'?:?$str""", {'str': str}) -- cgit 1.4.1