Add support for imports from qgis.PyQt

this adds support to imports from the qgis.PyQt packages like
from qgis.PyQt.QtGui import (QCheckBox, QIcon)

this kind of imports are already used by some plugins
without having the PyQt5 structure of packages (i.e. they use
from qgis.PyQt.QtGui import (QCheckBox, QIcon) instead of
from qgis.PyQt.QtGui import (QIcon)
from qgis.PyQt.QtWidgets import (QCheckBox)
This commit is contained in:
Marco Bernasocchi 2018-03-14 19:46:35 +01:00 committed by Matthias Kuhn
parent ac09795900
commit 20c071b515

View File

@ -7,7 +7,7 @@
from lib2to3.fixes.fix_imports import alternates, FixImports
from lib2to3 import fixer_base
from lib2to3.fixer_util import (Name, Comma, FromImport, Newline,
find_indentation, Node, syms)
find_indentation, Node, syms, Leaf)
MAPPING = {
"PyQt4.QtGui": [
@ -382,6 +382,15 @@ MAPPING = {
}
new_mappings = {}
for key, value in MAPPING.items():
match_str = key.replace('PyQt4', '')
match_str = '{}{}'.format('qgis.PyQt', match_str)
new_mappings[match_str] = value
MAPPING.update(new_mappings)
def build_pattern():
bare = set()
for old_module, changes in list(MAPPING.items()):
@ -391,10 +400,14 @@ def build_pattern():
if '.' not in old_module:
from_name = "%r" % old_module
else:
dotted = old_module.split('.')
assert len(dotted) == 2
from_name = "dotted_name<%r '.' %r>" % (dotted[0], dotted[1])
if len(dotted) == 3:
from_name = "dotted_name<%r '.' %r '.' %r>" % (dotted[0], dotted[1], dotted[2])
else:
assert len(dotted) == 2
from_name = "dotted_name<%r '.' %r>" % (dotted[0], dotted[1])
yield """import_name< 'import' (module=%s
| dotted_as_names< any* module=%s any* >) >