Skip to content
Permalink
Browse files

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)
  • Loading branch information
mbernasocchi authored and m-kuhn committed Mar 24, 2018
1 parent ac09795 commit 20c071b51572fea9a1a59e469507ca71062bf130
Showing with 16 additions and 3 deletions.
  1. +16 −3 scripts/qgis_fixes/fix_pyqt.py
@@ -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 @@
}


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* >) >

0 comments on commit 20c071b

Please sign in to comment.
You can’t perform that action at this time.