Skip to content
Permalink
Browse files

Merge pull request #3412 from medspx/PyQtQFileDialogFix

[PyQt] Add a lib2to3 custom fix to handle QFileDialog methods in PyQt5
  • Loading branch information
m-kuhn committed Aug 21, 2016
2 parents 1df8474 + ee87b0d commit f64adb9675eb04c0321cc1a539297e67383a92b5
Showing with 46 additions and 0 deletions.
  1. +46 −0 scripts/qgis_fixes/fix_qfiledialog.py
@@ -0,0 +1,46 @@
# -*- coding: utf-8 -*-
"""
Migrate QFileDialog methods from PyQt4 to PyQt5
"""
# Author: Médéric Ribreux <mederic.ribreux@medspx.fr>
# Adapted from fix_pyqt
# and http://python3porting.com/fixers.html

# Local imports
from lib2to3.fixer_base import BaseFix
from lib2to3.fixer_util import Name, Call

class FixQfiledialog(BaseFix):

PATTERN = """
power< 'QFileDialog' trailer< '.' filter=('getOpenFileNameAndFilter'|'getOpenFileNamesAndFilter'|'getSaveFileNameAndFilter') > any >
|
expr_stmt< filename=any '=' power< any trailer< '(' power< 'QFileDialog' trailer< '.' method=('getOpenFileName'|'getOpenFileNames'|'getSaveFileName') > any > ')' > > >
|
expr_stmt< filename=any '=' power< 'QFileDialog' trailer< '.' method=('getOpenFileName'|'getOpenFileNames'|'getSaveFileName') > any > >
"""

def transform(self, node, results):
# First case: getOpen/SaveFileName
# We need to add __ variable because in PyQt5
# getOpen/SaveFileName returns a tuple
if 'filename' in results:
node = results['filename']

# count number of leaves (result variables)
nbLeaves = sum(1 for i in node.leaves())

# If we have less than two args,
# we add __ special variable
if nbLeaves < 3:
fileName = node.value
node.value = u'{}, __'.format(fileName)
node.changed()

# Rename *AndFilter methods
if 'filter' in results:
method = results['filter'][0]
method.value = method.value.replace(u'AndFilter', u'')
method.changed()


0 comments on commit f64adb9

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