misc
This commit is contained in:
parent
557fd6e697
commit
9e0af0b509
39
proxy.py
39
proxy.py
@ -1,6 +1,10 @@
|
|||||||
from typing import List, Protocol, Tuple, Union
|
from typing import List, Protocol, Tuple, Union
|
||||||
|
|
||||||
def MakeAttrs(css:str|Tuple[str, ...]|None, id:str|None, attributes: dict[str, str | None]) -> str:
|
render_depth:int = 0
|
||||||
|
|
||||||
|
def MakeAttrs(css:str|Tuple[str, ...]|None, id:str|None, attributes: dict[str, str | None]|None) -> str:
|
||||||
|
if not attributes:
|
||||||
|
attributes = {}
|
||||||
if css:
|
if css:
|
||||||
attributes['class'] = " ".join(css) if isinstance(css, (list, tuple)) else css
|
attributes['class'] = " ".join(css) if isinstance(css, (list, tuple)) else css
|
||||||
if id:
|
if id:
|
||||||
@ -16,10 +20,15 @@ class ComplexProxyLeaf(dict):
|
|||||||
attrs:str = ""
|
attrs:str = ""
|
||||||
def __init__(self, name:str):
|
def __init__(self, name:str):
|
||||||
self.name = name
|
self.name = name
|
||||||
def __call__(self, css:str|None = None, id:str|None = None):
|
|
||||||
clone = ComplexProxyLeaf(self.name)
|
def props(self, css:str|Tuple[str, ...]|None, id:str|None, attributes: dict[str, str | None]|None):
|
||||||
clone.attrs = MakeAttrs(css, id, {})
|
clone = clone = self.__class__(self.name)
|
||||||
|
clone.attrs = MakeAttrs(css, id, attributes)
|
||||||
return clone
|
return clone
|
||||||
|
|
||||||
|
def __call__(self, css:str|None = None, id:str|None = None):
|
||||||
|
return self.props(css, id, {})
|
||||||
|
|
||||||
def __repr__(self) -> str:
|
def __repr__(self) -> str:
|
||||||
return f'<{self.name} {self.attrs}/>'
|
return f'<{self.name} {self.attrs}/>'
|
||||||
|
|
||||||
@ -28,34 +37,36 @@ class ComplexProxyBranch(ComplexProxyLeaf):
|
|||||||
attrs:str = ""
|
attrs:str = ""
|
||||||
def __init__(self, name:str):
|
def __init__(self, name:str):
|
||||||
self.name = name
|
self.name = name
|
||||||
|
|
||||||
def __call__(self, css:str|None = None, id:str|None = None):
|
def __call__(self, css:str|None = None, id:str|None = None):
|
||||||
clone = ComplexProxyBranch(self.name)
|
return self.props(css, id, None)
|
||||||
clone.attrs = MakeAttrs(css, id, {})
|
|
||||||
return clone
|
|
||||||
def __repr__(self) -> str:
|
def __repr__(self) -> str:
|
||||||
print("dump", self.attrs)
|
return f'<{self.name} {self.attrs}></{self.name}>'
|
||||||
copy = self.attrs
|
|
||||||
self.attrs = ""
|
|
||||||
return f'<{self.name} {copy}></{self.name}>'
|
|
||||||
def __getitem__(self, key:Union[str, 'ComplexProxyBranch', ComplexProxyLeaf, Tuple[Union[str, 'ComplexProxyBranch', ComplexProxyLeaf], ...]]) -> str:
|
def __getitem__(self, key:Union[str, 'ComplexProxyBranch', ComplexProxyLeaf, Tuple[Union[str, 'ComplexProxyBranch', ComplexProxyLeaf], ...]]) -> str:
|
||||||
|
|
||||||
|
global render_depth
|
||||||
|
render_depth = render_depth + 1
|
||||||
if isinstance(key, tuple):
|
if isinstance(key, tuple):
|
||||||
children = "".join(str(k) for k in key)
|
children = f'\n'.join(str(k) for k in key)
|
||||||
else:
|
else:
|
||||||
children = key
|
children = key
|
||||||
print("child", self.attrs)
|
|
||||||
return f'<{self.name} {self.attrs}>{children}</{self.name}>'
|
|
||||||
|
|
||||||
|
render_depth = render_depth - 1
|
||||||
|
|
||||||
|
return f'<{self.name} {self.attrs}>\n{children}\n</{self.name}>'
|
||||||
|
|
||||||
IMG = ComplexProxyLeaf("img")
|
IMG = ComplexProxyLeaf("img")
|
||||||
BR = ComplexProxyLeaf("br")
|
BR = ComplexProxyLeaf("br")
|
||||||
A = ComplexProxyBranch("a")
|
A = ComplexProxyBranch("a")
|
||||||
|
|
||||||
print(
|
print(
|
||||||
A
|
A
|
||||||
[
|
[
|
||||||
"click here",
|
"click here",
|
||||||
BR,
|
BR,
|
||||||
|
IMG(id="header.png"),
|
||||||
A(css="test"),
|
A(css="test"),
|
||||||
|
A["other?"],
|
||||||
A(css="www.site.com")
|
A(css="www.site.com")
|
||||||
[
|
[
|
||||||
IMG(id="image.png")
|
IMG(id="image.png")
|
||||||
|
Loading…
Reference in New Issue
Block a user