QGIS/scripts/qgis_fixes/fix_uiimport.py
Juergen E. Fischer fb3fcfa3a0 Python3/Qt5/2to3 updates:
* pyqtwrappers update (add QtNetwork, QtXml, QtSql, QtTest, uic)
* 2to3 updates
* move QPyNullVariant/NULL to PyQt.QtCore
* add global unicode/basestring/long for Python3
* expand QtGui, QtCore module and star exports
* Qscintilla2
* replace Set import with set builtin
2016-03-14 20:38:20 +01:00

46 lines
1.3 KiB
Python

"""Fixer for import of generated UIs
"""
# Local imports
from lib2to3 import fixer_base
from lib2to3.fixes.fix_import import FixImport
from lib2to3.fixer_util import FromImport, Node, Leaf, syms
class FixUiimport(fixer_base.BaseFix):
BM_compatible = True
PATTERN = """
import_from< 'from' imp=any 'import' ['('] any [')'] >
|
import_name< 'import' imp=any >
"""
def transform(self, node, results):
imp = results.get('imp')
if node.type == syms.import_from:
# Some imps are top-level (eg: 'import ham')
# some are first level (eg: 'import ham.eggs')
# some are third level (eg: 'import ham.eggs as spam')
# Hence, the loop
while not hasattr(imp, 'value'):
imp = imp.children[0]
if imp.value.startswith("ui_"):
imp.value = u"." + imp.value
imp.changed()
else:
first = imp
if isinstance(imp, Node):
first = imp.leaves().next()
if not isinstance(first, Leaf):
return
if not first.value.startswith("ui_"):
return
new = FromImport(".", [imp])
new.prefix = node.prefix
return new