update readme on build system
[lunaix-os.git] / lunaix-os / scripts / build-tools / lib / utils.py
index 0ca46957e1bf9e3b72142d4c72952371808d730e..6dd1ace18baf5700259c289982274b460708e57b 100644 (file)
@@ -1,5 +1,6 @@
 import os, ast
 from pathlib import Path
+from typing import Any, List
 
 def join_path(stem, path):
     if os.path.isabs(path):
@@ -8,10 +9,16 @@ def join_path(stem, path):
 
 
 class Schema:
-    class Empty:
+    class Any:
         def __init__(self):
             pass
 
+        def __str__(self):
+            return "Any"
+        
+        def __repr__(self):
+            return self.__str__()
+
     class Union:
         def __init__(self, *args):
             self.union = [*args]
@@ -19,6 +26,20 @@ class Schema:
         def __str__(self):
             strs = [Schema.get_type_str(t) for t in self.union]
             return f"{' | '.join(strs)}"
+        
+        def __repr__(self):
+            return self.__str__()
+        
+    class List:
+        def __init__(self, el_type):
+            self.el_type = el_type
+        
+        def __str__(self):
+            strs = Schema.get_type_str(self.el_type)
+            return f"*{strs}"
+        
+        def __repr__(self):
+            return self.__str__()
 
     def __init__(self, type, **kwargs):
         self.__type = type
@@ -34,6 +55,16 @@ class Schema:
             
         return True
     
+    def __match_list_member(self, actual, expect):
+        if not isinstance(actual, List):
+            return False
+        
+        for a in actual:
+            if not self.__match(a, expect.el_type):
+                return False
+            
+        return True
+    
     def __match_union(self, actual, union):
         for s in union.union:
             if self.__match(actual, s):
@@ -41,9 +72,15 @@ class Schema:
         return False
 
     def __match(self, val, scheme):
+        if scheme is Any:
+            return True
+        
         if isinstance(scheme, Schema):
             return scheme.match(val)
         
+        if isinstance(scheme, Schema.List):
+            return self.__match_list_member(val, scheme)
+        
         if isinstance(scheme, list) and isinstance(val, list):
             return self.__match_list(val, scheme)
         
@@ -61,8 +98,6 @@ class Schema:
 
         for field, t in self.__fields.items():
             if not hasattr(instance, field):
-                if isinstance(t, Schema.Empty):
-                    continue
                 return False
             
             field_val = getattr(instance, field)