aboutsummaryrefslogtreecommitdiffstats
path: root/sources/pyside-tools
diff options
context:
space:
mode:
authorFriedemann Kleint <Friedemann.Kleint@qt.io>2023-03-22 14:48:11 +0100
committerFriedemann Kleint <Friedemann.Kleint@qt.io>2023-03-23 12:05:53 +0000
commit1864626470fd615b6012a3b7190c3adec90978e6 (patch)
tree2bcc5fa5f146ecc81e7b1403f374074ab05bcc7e /sources/pyside-tools
parent225aa423812013e28605934093c7943c809e3ac4 (diff)
metaobjectdump.py: Refactor/Fix handling of module imports
The code caused an error when encountering from PySide6 import QtCore since it was expecting from PySide6.QtCore Fix this to handle this case as well. Turn the module list into a set to suppress duplicated modules. Pick-to: 6.5 Change-Id: I4ec40c28f5a8cc03c7985869597bb2315500a604 Reviewed-by: Shyamnath Premnadh <Shyamnath.Premnadh@qt.io>
Diffstat (limited to 'sources/pyside-tools')
-rw-r--r--sources/pyside-tools/metaobjectdump.py22
1 files changed, 13 insertions, 9 deletions
diff --git a/sources/pyside-tools/metaobjectdump.py b/sources/pyside-tools/metaobjectdump.py
index c69d9e78f..3588bc497 100644
--- a/sources/pyside-tools/metaobjectdump.py
+++ b/sources/pyside-tools/metaobjectdump.py
@@ -8,7 +8,7 @@ import sys
import tokenize
from argparse import ArgumentParser, RawTextHelpFormatter
from pathlib import Path
-from typing import Dict, List, Optional, Tuple, Union
+from typing import Dict, List, Optional, Set, Tuple, Union
DESCRIPTION = """Parses Python source code to create QObject metatype
@@ -104,7 +104,7 @@ class MetaObjectDumpVisitor(ast.NodeVisitor):
self._properties: List[PropertyEntry] = []
self._signals: List[Signal] = []
self._within_class: bool = False
- self._qt_modules: List[str] = []
+ self._qt_modules: Set[str] = set()
self._qml_import_name = ""
self._qml_import_major_version = 0
self._qml_import_minor_version = 0
@@ -119,7 +119,7 @@ class MetaObjectDumpVisitor(ast.NodeVisitor):
return (self._qml_import_major_version, self._qml_import_minor_version)
def qt_modules(self):
- return self._qt_modules
+ return sorted(self._qt_modules)
@staticmethod
def create_ast(filename: Path) -> ast.Module:
@@ -315,16 +315,20 @@ class MetaObjectDumpVisitor(ast.NodeVisitor):
self._properties.append(prop)
def visit_Import(self, node):
- if node.names:
- self._handle_import(node.names[0].name)
+ for n in node.names: # "import PySide6.QtWidgets"
+ self._handle_import(n.name)
def visit_ImportFrom(self, node):
- self._handle_import(node.module)
+ if "." in node.module: # "from PySide6.QtWidgets import QWidget"
+ self._handle_import(node.module)
+ elif node.module == "PySide6": # "from PySide6 import QtWidgets"
+ for n in node.names:
+ if n.name.startswith("Qt"):
+ self._qt_modules.add(n.name)
def _handle_import(self, mod: str):
- if mod.startswith('PySide'):
- dot = mod.index(".")
- self._qt_modules.append(mod[dot + 1:])
+ if mod.startswith("PySide6."):
+ self._qt_modules.add(mod[8:])
def create_arg_parser(desc: str) -> ArgumentParser: