|
| 1 | +from __future__ import annotations |
| 2 | + |
1 | 3 | from typing import Union, Optional
|
2 | 4 |
|
3 | 5 | from pynescript.ast.types import AST
|
4 | 6 |
|
5 | 7 |
|
6 |
| -def _dump_value_impl(value, indent: int = 0, depth: int = 0): |
7 |
| - indent_step = " " * indent |
| 8 | +def iter_fields(node: AST): |
| 9 | + return node.iter_fields() |
| 10 | + |
| 11 | + |
| 12 | +def _dump_value_impl(value, indent: str = "", depth: int = 0): |
8 | 13 | if isinstance(value, AST):
|
9 | 14 | return _dump_impl(value, indent=indent, depth=depth)
|
10 | 15 | elif isinstance(value, list) and len(value) > 0:
|
11 | 16 | lines = []
|
12 | 17 | lines.append("[")
|
13 | 18 | for subvalue in value:
|
14 | 19 | lines.append(
|
15 |
| - f"{indent_step * (depth + 1)}{_dump_value_impl(subvalue, indent=indent, depth=depth + 1)}," |
| 20 | + f"{indent * (depth + 1)}{_dump_value_impl(subvalue, indent=indent, depth=depth + 1)}," |
16 | 21 | )
|
17 |
| - lines.append(f"{indent_step * depth}]") |
| 22 | + lines.append(f"{indent * depth}]") |
18 | 23 | return "\n".join(lines)
|
19 | 24 | else:
|
20 | 25 | return f"{value!r}"
|
21 | 26 |
|
22 | 27 |
|
23 |
| -def _dump_impl(node: AST, indent: int = 0, depth: int = 0): |
| 28 | +def _dump_impl(node: AST, indent: str = "", depth: int = 0): |
24 | 29 | class_name = node.__class__.__name__
|
25 |
| - class_params_except = [ |
26 |
| - "loc", |
27 |
| - "end_loc", |
28 |
| - "lineno", |
29 |
| - "col_offset", |
30 |
| - "end_lineno", |
31 |
| - "end_col_offset", |
32 |
| - ] |
33 |
| - class_params = { |
34 |
| - name: value |
35 |
| - for name, value in node.__dict__.items() |
36 |
| - if name not in class_params_except |
37 |
| - } |
38 |
| - if indent > 0 and len(class_params) > 0: |
39 |
| - indent_step = " " * indent |
| 30 | + class_params = dict(iter_fields(node)) |
| 31 | + if indent and len(class_params) > 0: |
40 | 32 | lines = []
|
41 | 33 | lines.append(f"{class_name}(")
|
42 | 34 | for name, value in class_params.items():
|
43 | 35 | lines.append(
|
44 |
| - f"{indent_step * (depth + 1)}{name}={_dump_value_impl(value, indent=indent, depth=depth + 1)}," |
| 36 | + f"{indent * (depth + 1)}{name}={_dump_value_impl(value, indent=indent, depth=depth + 1)}," |
45 | 37 | )
|
46 |
| - lines.append(f"{indent_step * depth})") |
| 38 | + lines.append(f"{indent * depth})") |
47 | 39 | return "\n".join(lines)
|
48 | 40 | else:
|
49 | 41 | class_params = [f"{name}={value!r}" for name, value in class_params.items()]
|
50 | 42 | class_params = ", ".join(class_params)
|
51 | 43 | return f"{class_name}({class_params})"
|
52 | 44 |
|
53 | 45 |
|
54 |
| -def dump(node: AST, indent: Optional[int] = None) -> str: |
| 46 | +def dump(node: AST, indent: Optional[Union[int, str]] = None) -> str: |
55 | 47 | if indent is None:
|
56 |
| - indent = 2 |
57 |
| - |
| 48 | + indent = 0 |
| 49 | + if isinstance(indent, int): |
| 50 | + indent = " " * indent |
58 | 51 | return _dump_impl(node, indent=indent)
|
59 | 52 |
|
60 | 53 |
|
| 54 | +def unparse(node: AST) -> str: |
| 55 | + from pynescript.ast.node_visitors import Unparser |
| 56 | + |
| 57 | + unparser = Unparser() |
| 58 | + return unparser.visit(node) |
| 59 | + |
| 60 | + |
61 | 61 | def literal_eval(node_or_string: Union[str, AST]):
|
62 | 62 | raise NotImplementedError()
|
0 commit comments