68 lines
1.9 KiB
Python
68 lines
1.9 KiB
Python
from dataclasses import dataclass
|
|
from jinja2 import Environment, BaseLoader, Template, StrictUndefined
|
|
from js import CARD_XPATHS_ENTRIES, CARD_TEMPLATE
|
|
from typing import Optional
|
|
import jinja2
|
|
|
|
@dataclass
|
|
class Entry:
|
|
node : Optional[str]
|
|
xpath : str
|
|
content : Optional[str]
|
|
attributes : dict
|
|
|
|
# breaks because firefox and chrome normalizes html tags inside py-script
|
|
CARD_XPATHS_ENTRIES_JSON = r"""
|
|
##JSON:://*##
|
|
"""
|
|
#import json
|
|
#CARD_XPATHS_ENTRIES = json.loads(CARD_XPATHS_ENTRIES_JSON)
|
|
|
|
# time to abuse the JsProxy objects!
|
|
XPATH_MAP = {}
|
|
for entry in CARD_XPATHS_ENTRIES:
|
|
node = entry.node if hasattr(entry, "node") else entry["node"]
|
|
xpath = entry.xpath if hasattr(entry, "xpath") else entry["xpath"]
|
|
content = entry.content if hasattr(entry, "content") else entry["content"]
|
|
attributes = entry.attributes if hasattr(entry, "attributes") else entry["attributes"]
|
|
|
|
if node is None and not content:
|
|
assert "text()" in xpath
|
|
continue
|
|
|
|
XPATH_MAP[xpath] = Entry(node, xpath, content, attributes)
|
|
|
|
def get_all(xpath) -> Entry:
|
|
xpath = f"/ability_card/{xpath}"
|
|
for key, entry in XPATH_MAP.items():
|
|
if key.startswith(xpath):
|
|
yield entry
|
|
|
|
def get(xpath) -> Entry:
|
|
xpath = f"/ability_card/{xpath}"
|
|
if xpath in XPATH_MAP:
|
|
return XPATH_MAP[xpath]
|
|
assert len(list(get_all(xpath))) == 0
|
|
|
|
#print(*sorted( f"{k} - {v.node} - {len(v.content)}" for k, v in XPATH_MAP.items() ), sep="\n")
|
|
|
|
print(
|
|
Environment(
|
|
trim_blocks = True,
|
|
lstrip_blocks = True,
|
|
loader = BaseLoader,
|
|
autoescape = True,
|
|
undefined = StrictUndefined,
|
|
)
|
|
.from_string(CARD_TEMPLATE)
|
|
.render(
|
|
get = get,
|
|
get_all = get_all,
|
|
**{
|
|
v.node: v.content
|
|
for k, v in XPATH_MAP.items()
|
|
if v.node is not None and k == f"/ability_card/{v.node}"
|
|
}
|
|
)
|
|
)
|