From 6951679dddea8dc908430292e0a4b59fb4b4a62f Mon Sep 17 00:00:00 2001 From: ken-morel Date: Fri, 27 Sep 2024 04:16:32 +0100 Subject: [PATCH] starting evaluate literal refactor --- src/taktk/constants.py | 2 ++ src/taktk/template.py | 58 ++++++++++++++++++++++-------------------- 2 files changed, 32 insertions(+), 28 deletions(-) create mode 100644 src/taktk/constants.py diff --git a/src/taktk/constants.py b/src/taktk/constants.py new file mode 100644 index 0000000..96e87ef --- /dev/null +++ b/src/taktk/constants.py @@ -0,0 +1,2 @@ +from tkinter.constants import * +from . import Nil diff --git a/src/taktk/template.py b/src/taktk/template.py index 291d9fe..d39413d 100644 --- a/src/taktk/template.py +++ b/src/taktk/template.py @@ -38,12 +38,6 @@ class TagType(enum.Enum): META = enum.auto() -class ArgType(enum.Enum): - INFERRED = enum.auto() - RAW = enum.enum() - EVALUATED = enum.enum() - - SPACE = frozenset(" ") VARNAME = frozenset(string.ascii_letters + string.digits + "_") COMPONENT_NAME = VARNAME | frozenset(".") @@ -420,10 +414,10 @@ def parse(self) -> "Template.Item": def evaluate_literal( string: str, namespace: Optional[writeable.Namespace] = None -) -> Any: +) -> tuple[bool, Any]: """Evaluate a litteral from string and optional namespace.""" - from .media import get_media - import tkinter.constants + from . import media + from . import constants string_set = set(string) if len(string) > 1: @@ -432,47 +426,55 @@ def evaluate_literal( b, e = string, None else: raise ValueError("empty literal string") - if hasattr(tkinter.constants, string): - return getattr(tkinter.constants, string) + if hasattr(constants, string): + return (False, getattr(constants, string)) elif string == "None": - return None + return (False, None) elif string == "True": - return True + return (False, True) elif string == "False": - return False + return (False, False) elif b == "<" and e == ">": - return get_media(string[1:-1]) - elif len(string_set - INT) == 0 and string.isnumeric(): - return int(string) + return (False, media.get_media(string[1:-1])) + elif len(string_set - INT) == 0: + return (False, int(string)) elif len(string_set - DECIMAL) == 0: return Decimal(string) - elif len(string) > 2 and b == "{" and e == "}": + elif b == "{" and e == "}": if namespace is None: raise ValueError( "Unallowed writeable.Writeable in none namespaced context", string, ) code = string[1:-1] - if len(code) >= 2 and code[0] == "$": - return writeable.Writeable.from_name(namespace, code[1:]) - if len(code) >= 2 and code[0] == "{" and code[-1] == "}": - code = code[1:-1] + return (False, eval(code, {}, namespace)) + elif b == "$": + if len(string) < 2: + raise Exception(f"Wrong subscription {string!r}") + typ = string[1] + if typ == "{": # Custum writeable + code = string[2:-1] if "||" in code: get, set_ = code.split("||") else: get, set_ = code, "" - return writeable.Writeable.from_get_set(namespace, get, set_) + return ( + True, + writeable.Writeable.from_get_set(namespace, get, set_), + ) + elif typ == "(": + return (True, eval(string[2:1])) else: - return eval(code, {}, namespace) + return (True, writeable.Writeable.from_name(namespace, code[1:])) elif b in STRING_QUOTES: if e == b: - return string[1:-1] + return (False, string[1:-1]) else: raise ValueError("Unterminated string:", string) elif string[0] == string[-1] == "/": - return Path(os.path.expandvars(string[1:-1])) - elif string[0] == "[" and string[-1] == "]": - return dictionary.Translation(string[1:-1]) + return (False, Path(os.path.expandvars(string[1:-1]))) + elif b == "@": + return (False, dictionary.Translation(string[1:])) elif ":" in string and len(string_set - (DECIMAL | SLICE)) == 0: if len(d := (string_set - SLICE)) > 0: raise ValueError("wrong slice", string, d)