Inspired by: https://github.com/YoloSwagTeam/ast2json/blob/master/ast2json/ast2json.py
(node)
| 64 | |
| 65 | |
| 66 | def ast2json(node): |
| 67 | """ |
| 68 | Inspired by: |
| 69 | https://github.com/YoloSwagTeam/ast2json/blob/master/ast2json/ast2json.py |
| 70 | """ |
| 71 | assert isinstance(node, ast.AST) |
| 72 | data = OrderedDict() |
| 73 | data["_type"] = node.__class__.__name__ |
| 74 | |
| 75 | # try: |
| 76 | # data['_doc_string'] = ast.get_docstring(node) |
| 77 | # except TypeError: |
| 78 | # pass |
| 79 | |
| 80 | for attr, target in node.__dict__.items(): |
| 81 | if attr.startswith("_"): |
| 82 | continue |
| 83 | data[attr] = get_value(target) |
| 84 | return data |
| 85 | |
| 86 | |
| 87 | def get_value(attr): |