aboutsummaryrefslogtreecommitdiffstats
path: root/sources/pyside-tools/qtpy2cpp_lib/nodedump.py
diff options
context:
space:
mode:
authorFriedemann Kleint <Friedemann.Kleint@qt.io>2022-09-02 09:43:41 +0200
committerFriedemann Kleint <Friedemann.Kleint@qt.io>2022-09-02 12:16:45 +0200
commit065766883f5e34d20ca88084cd747813a84ed2d3 (patch)
tree2bca2c34d113f0198564155ac21789466d4b8b96 /sources/pyside-tools/qtpy2cpp_lib/nodedump.py
parent4cfa700d5965922b5156517a64790369deac354f (diff)
Move qtpy2cpp to sources/pyside-tools
Preparing the entry point. Task-number: PYSIDE-1945 Change-Id: I4a2fbe6d35b4f97bf0ab7cfc2085b86a40bc2558 Reviewed-by: Christian Tismer <tismer@stackless.com>
Diffstat (limited to 'sources/pyside-tools/qtpy2cpp_lib/nodedump.py')
-rw-r--r--sources/pyside-tools/qtpy2cpp_lib/nodedump.py50
1 files changed, 50 insertions, 0 deletions
diff --git a/sources/pyside-tools/qtpy2cpp_lib/nodedump.py b/sources/pyside-tools/qtpy2cpp_lib/nodedump.py
new file mode 100644
index 000000000..de62e9700
--- /dev/null
+++ b/sources/pyside-tools/qtpy2cpp_lib/nodedump.py
@@ -0,0 +1,50 @@
+# Copyright (C) 2022 The Qt Company Ltd.
+# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
+
+"""Helper to dump AST nodes for debugging"""
+
+
+import ast
+
+
+def to_string(node):
+ """Helper to retrieve a string from the (Lists of )Name/Attribute
+ aggregated into some nodes"""
+ if isinstance(node, ast.Name):
+ return node.id
+ if isinstance(node, ast.Attribute):
+ return node.attr
+ return ''
+
+
+def debug_format_node(node):
+ """Format AST node for debugging"""
+ if isinstance(node, ast.alias):
+ return f'alias("{node.name}")'
+ if isinstance(node, ast.arg):
+ return f'arg({node.arg})'
+ if isinstance(node, ast.Attribute):
+ if isinstance(node.value, ast.Name):
+ nested_name = debug_format_node(node.value)
+ return f'Attribute("{node.attr}", {nested_name})'
+ return f'Attribute("{node.attr}")'
+ if isinstance(node, ast.Call):
+ return 'Call({}({}))'.format(to_string(node.func), len(node.args))
+ if isinstance(node, ast.ClassDef):
+ base_names = [to_string(base) for base in node.bases]
+ bases = ': ' + ','.join(base_names) if base_names else ''
+ return f'ClassDef({node.name}{bases})'
+ if isinstance(node, ast.ImportFrom):
+ return f'ImportFrom("{node.module}")'
+ if isinstance(node, ast.FunctionDef):
+ arg_names = [a.arg for a in node.args.args]
+ return 'FunctionDef({}({}))'.format(node.name, ', '.join(arg_names))
+ if isinstance(node, ast.Name):
+ return 'Name("{}", Ctx={})'.format(node.id, type(node.ctx).__name__)
+ if isinstance(node, ast.NameConstant):
+ return f'NameConstant({node.value})'
+ if isinstance(node, ast.Num):
+ return f'Num({node.n})'
+ if isinstance(node, ast.Str):
+ return f'Str("{node.s}")'
+ return type(node).__name__