Skip to content

Commit

Permalink
Add support for imports from qgis.PyQt
Browse files Browse the repository at this point in the history
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 20c071b
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions scripts/qgis_fixes/fix_pyqt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down Expand Up @@ -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()):
Expand All @@ -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* >) >
Expand Down

0 comments on commit 20c071b

Please sign in to comment.