print_tree.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. from .api import Api, PropertyType, NODE_TYPE_NAMES, CallArgType, Expr
  2. def print_tree(api, node_path="/", depth=None):
  3. print(node_path)
  4. print_node_info(api, node_path, depth, indent=1)
  5. def print_node_info(api, parent_path, depth, indent):
  6. if indent - 1 == depth:
  7. return
  8. ws = " "*4*indent
  9. for (child_name, child_id, child_type) in api.get_children(parent_path):
  10. child_type = NODE_TYPE_NAMES.get(child_type, child_type)
  11. desc = f"{ws}{child_name}:{child_id}/"
  12. desc += " "*(50 - len(desc))
  13. desc += f"[{child_type}]"
  14. print(desc)
  15. if parent_path == "/":
  16. child_path = "/" + child_name
  17. else:
  18. child_path = parent_path + "/" + child_name
  19. print_node_info(api, child_path, depth, indent+1)
  20. for prop in api.get_properties(parent_path):
  21. prop_val = api.get_property_value(parent_path, prop.name)
  22. def fmt_str(pv):
  23. return pv if isinstance(pv, Expr) else f"\"{pv}\""
  24. def fmt_f32(pv):
  25. return pv if isinstance(pv, Expr) else f"{pv:.2f}"
  26. if prop.type == PropertyType.STR:
  27. prop_val = "[" + ", ".join(fmt_str(pv) for pv in prop_val) + "]"
  28. elif prop.type == PropertyType.FLOAT32:
  29. prop_val = "[" + ", ".join(fmt_f32(pv) for pv in prop_val) + "]"
  30. if len(prop_val) == 1:
  31. prop_val = prop_val[0]
  32. prop_val = f" = {prop_val}"
  33. prop_type = PropertyType.to_str(prop.type)
  34. print(f"{ws}{prop.name}: {prop_type}{prop_val}")
  35. if prop.depends:
  36. print(f"{ws} depends: {prop.depends}")
  37. for sig in api.get_signals(parent_path):
  38. print(f"{ws}~{sig}")
  39. for slot_name, slot_id in api.get_slots(parent_path, sig):
  40. print(f"{ws}- '{slot_name}' ({slot_id})")
  41. for method_name in api.get_methods(parent_path):
  42. args, results = api.get_method(parent_path, method_name)
  43. args = [f"{name}: " + CallArgType.to_str(typ) for (name, _, typ) in args]
  44. results = [f"{name}: " + CallArgType.to_str(typ) for (name, _, typ) in (results or [])]
  45. method_str = f"{method_name}(" + ", ".join(args) + ") -> (" + ", ".join(results) + ")"
  46. print(f"{ws}{method_str}")