summary refs log tree commit diff stats
path: root/ganarchy/config.py
diff options
context:
space:
mode:
authorSoniEx2 <endermoneymod@gmail.com>2020-02-26 18:00:52 -0300
committerSoniEx2 <endermoneymod@gmail.com>2020-02-26 18:12:19 -0300
commit1de7b738142ce1213d374ddfd5a86973feaacacc (patch)
tree5486841b8436278ee0390f4c7ba39fdc9a5062fd /ganarchy/config.py
parenta89d3537757cc74f3e6652565487eab36537b4ed (diff)
Create an URIPredicate
Diffstat (limited to 'ganarchy/config.py')
-rw-r--r--ganarchy/config.py23
1 files changed, 22 insertions, 1 deletions
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})