X-Git-Url: https://scm.lunaixsky.com/lunaix-os.git/blobdiff_plain/d15268ba6eadf89a38087995ff407f50418485fb..5b211a176c805745d48517eff99c4dd651f2da75:/lunaix-os/scripts/build-tools/lib/utils.py?ds=sidebyside diff --git a/lunaix-os/scripts/build-tools/lib/utils.py b/lunaix-os/scripts/build-tools/lib/utils.py index 0ca4695..6dd1ace 100644 --- a/lunaix-os/scripts/build-tools/lib/utils.py +++ b/lunaix-os/scripts/build-tools/lib/utils.py @@ -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)