Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 53 additions & 18 deletions catalogs/e2catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,55 @@ def code_expectations(roots):
{k: sorted(v) for k, v in sorted(fonts.items(), key=lambda i: i[0].lower())})


def apply_skin_attributes(func):
"""Attribute names an applySkin() method compares its (attrib, value) pairs with."""
attrs = set()
names = {"attrib"}
for loop in ast.walk(func):
if isinstance(loop, ast.For) and isinstance(loop.target, ast.Tuple) and loop.target.elts \
and isinstance(loop.target.elts[0], ast.Name):
names.add(loop.target.elts[0].id)
for sub in ast.walk(func):
if isinstance(sub, ast.Compare) and isinstance(sub.left, ast.Name) and sub.left.id in names:
for op, comp in zip(sub.ops, sub.comparators):
if isinstance(op, ast.Eq) and isinstance(comp, ast.Constant) and isinstance(comp.value, str):
attrs.add(comp.value)
elif isinstance(op, ast.In) and isinstance(comp, (ast.Tuple, ast.List, ast.Set)):
attrs.update(e.value for e in comp.elts if isinstance(e, ast.Constant)
and isinstance(e.value, str))
elif isinstance(sub, ast.Match) and isinstance(sub.subject, ast.Name) and sub.subject.id in names:
for case in sub.cases:
attrs |= {p.value.value for p in ast.walk(case.pattern)
if isinstance(p, ast.MatchValue) and isinstance(p.value, ast.Constant)
and isinstance(p.value.value, str)}
# Dispatch style: one nested function per attribute, def EntryBackgroundColor(value): ...
for sub in ast.walk(func):
if isinstance(sub, ast.FunctionDef) and sub is not func and len(sub.args.args) == 1 \
and not sub.name.startswith("_"):
attrs.add(sub.name)
return {a for a in attrs if VALUE_RE.match(a)}


def widget_attributes(roots):
"""Skin attributes read by Python widgets (applySkin in components, addons, plugins),
on top of the ones skin.py handles. Converters are skipped: they take arguments, not attributes."""
found = set()
for root, _label in roots:
for dirpath, dirnames, filenames in os.walk(root):
dirnames[:] = [d for d in dirnames if not d.startswith(".") and d != "Converter"]
for fn in filenames:
if not fn.endswith(".py"):
continue
try:
tree = ast.parse(open(os.path.join(dirpath, fn), encoding="utf-8", errors="replace").read())
except (SyntaxError, ValueError):
continue
for node in ast.walk(tree):
if isinstance(node, ast.FunctionDef) and node.name == "applySkin":
found |= apply_skin_attributes(node)
return sorted(found, key=str.lower)


def renderer_info(path):
"""GUI widget, base class and the extra skin attributes a renderer reads in applySkin."""
info = {}
Expand All @@ -223,24 +272,7 @@ def renderer_info(path):
if isinstance(item, ast.Assign) and any(isinstance(t, ast.Name) and t.id == "GUI_WIDGET" for t in item.targets):
info["guiWidget"] = attr_name(item.value)
if isinstance(item, ast.FunctionDef) and item.name == "applySkin":
names = {"attrib"}
for loop in ast.walk(item):
if isinstance(loop, ast.For) and isinstance(loop.target, ast.Tuple) and loop.target.elts \
and isinstance(loop.target.elts[0], ast.Name):
names.add(loop.target.elts[0].id)
for sub in ast.walk(item):
if isinstance(sub, ast.Compare) and isinstance(sub.left, ast.Name) and sub.left.id in names:
for op, comp in zip(sub.ops, sub.comparators):
if isinstance(op, ast.Eq) and isinstance(comp, ast.Constant) and isinstance(comp.value, str):
attrs.add(comp.value)
elif isinstance(op, ast.In) and isinstance(comp, (ast.Tuple, ast.List, ast.Set)):
attrs.update(e.value for e in comp.elts if isinstance(e, ast.Constant)
and isinstance(e.value, str))
elif isinstance(sub, ast.Match) and isinstance(sub.subject, ast.Name) and sub.subject.id in names:
for case in sub.cases:
attrs |= {p.value.value for p in ast.walk(case.pattern)
if isinstance(p, ast.MatchValue) and isinstance(p.value, ast.Constant)
and isinstance(p.value.value, str)}
attrs |= apply_skin_attributes(item)
if "guiWidget" in info or attrs or "Renderer" in bases:
info["class"] = cls.name
info["bases"] = bases
Expand Down Expand Up @@ -536,6 +568,7 @@ def build(root, name, plugin_dirs):
s["hasDefaultSkin"] = any(n in covered for n in s["skinNames"])

colours = parse_colors(root, os.path.join(py, "skin.py"), screens, parameters)
components["widgetAttributes"] = widget_attributes(roots)

return {
"image": name,
Expand Down Expand Up @@ -600,6 +633,8 @@ def to_markdown(cat):
f"- Color parameters: {', '.join(cat['colors'].get('colorParameters', []))}",
f"- Screens with color buttons: {cat['colors'].get('screensWithColorButtons')}, widget types: {cat['colors'].get('colorButtonWidgetTypes')}",
"",
f"## Attributes read by Python widgets ({len(cat['components'].get('widgetAttributes', []))})", "",
", ".join(f"`{a}`" for a in cat["components"].get("widgetAttributes", [])), "",
"## Renderer", "",
"| Renderer | GUI widget | Own attributes |", "|---|---|---|",
*[f"| {k} | {v.get('guiWidget', '')} | {', '.join(v.get('skinAttributes', []))} |" for k, v in cat["components"]["renderers"].items()],
Expand Down
Loading