tree.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. # 打印 Ardot 节点树的紧凑结构:缩进 + 类型/名称 + 文本 + 颜色 + 几何
  2. import json
  3. import sys
  4. path = sys.argv[1]
  5. ids = sys.argv[2].split(',') if len(sys.argv) > 2 else None
  6. maxdepth = int(sys.argv[3]) if len(sys.argv) > 3 else 8
  7. def hexcolor(fills):
  8. if not fills:
  9. return ''
  10. f = fills[0]
  11. if f.get('type') != 'SOLID':
  12. return f.get('type', '')
  13. c = f.get('color', {})
  14. op = f.get('opacity', 1)
  15. s = '#%02X%02X%02X' % (round(c.get('r', 0) * 255), round(c.get('g', 0) * 255), round(c.get('b', 0) * 255))
  16. if op is not None and op < 1:
  17. s += '@%s' % round(op, 2)
  18. return s
  19. for line in open(path, encoding='utf-8').read().splitlines():
  20. if not line.strip().startswith('{'):
  21. continue
  22. try:
  23. node = json.loads(line)
  24. except Exception:
  25. continue
  26. if 'id' not in node or 'children' not in node:
  27. continue
  28. if ids and node['id'] not in ids:
  29. continue
  30. def walk(n, depth):
  31. if depth > maxdepth:
  32. return
  33. t = n.get('type', '')
  34. name = n.get('name', '')
  35. parts = ['%s%s %s' % (' ' * depth, t, name)]
  36. ch = n.get('characters')
  37. if ch:
  38. parts.append('"%s"' % str(ch).replace('\n', '\\n')[:80])
  39. if n.get('fontSize'):
  40. parts.append('@%s' % n['fontSize'])
  41. col = hexcolor(n.get('fills'))
  42. if col:
  43. parts.append(col)
  44. geo = []
  45. for k in ('x', 'y', 'width', 'height'):
  46. if k in n:
  47. v = n[k]
  48. geo.append('%s=%s' % (k, round(v) if isinstance(v, (int, float)) else v))
  49. if geo:
  50. parts.append(' '.join(geo))
  51. if n.get('layout'):
  52. parts.append('layout=%s' % n['layout'])
  53. if n.get('cornerRadius'):
  54. parts.append('r=%s' % n['cornerRadius'])
  55. if n.get('textAlignHorizontal') and n.get('textAlignHorizontal') != 'LEFT':
  56. parts.append('align=%s' % n['textAlignHorizontal'])
  57. print(' '.join(parts))
  58. for c in n.get('children', []) or []:
  59. if isinstance(c, dict):
  60. walk(c, depth + 1)
  61. walk(node, 0)
  62. print('=' * 70)