Skip to content
Permalink
Browse files

Add utility function to extract wildcards from a filter string

  • Loading branch information
nyalldawson committed Nov 5, 2020
1 parent 36cdb08 commit d51e752e04f8752fb88894f2afd79d99b1cde21c
@@ -37,6 +37,14 @@ containing "tiff", "tif". The initial '.' is stripped off the extension.
.. seealso:: :py:func:`ensureFileNameHasExtension`

.. seealso:: :py:func:`addExtensionFromFilter`
%End

static QString wildcardsFromFilter( const QString &filter );
%Docstring
Giiven a ``filter`` string like "GeoTIFF Files (*.tiff *.tif)", extracts
the wildcard portion of this filter (i.e. "*.tiff *.tif").

.. versionadded:: 3.18
%End

static bool fileMatchesFilter( const QString &fileName, const QString &filter );
@@ -56,33 +56,42 @@ QStringList QgsFileUtils::extensionsFromFilter( const QString &filter )
return extensions;
}

QString QgsFileUtils::wildcardsFromFilter( const QString &filter )
{
const QRegularExpression globPatternsRx( QStringLiteral( ".*\\((.*?)\\)$" ) );
const QRegularExpressionMatch matches = globPatternsRx.match( filter );
if ( matches.hasMatch() )
return matches.captured( 1 );
else
return QString();
}

bool QgsFileUtils::fileMatchesFilter( const QString &fileName, const QString &filter )
{
QFileInfo fi( fileName );
const QString name = fi.fileName();
const QStringList parts = filter.split( QStringLiteral( ";;" ) );
for ( const QString &part : parts )
{
const QRegularExpression globPatternsRx( QStringLiteral( ".*\\((.*?)\\)$" ) );
const QRegularExpressionMatch matches = globPatternsRx.match( part );
if ( matches.hasMatch() )
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
const QStringList globPatterns = wildcardsFromFilter( part ).split( ' ', QString::SkipEmptyParts );
#else
const QStringList globPatterns = wildcardsFromFilter( part ).split( ' ', Qt::SkipEmptyParts );
#endif
for ( const QString &glob : globPatterns )
{
const QStringList globPatterns = matches.captured( 1 ).split( ' ' );
for ( const QString &glob : globPatterns )
{
#if QT_VERSION >= QT_VERSION_CHECK(5, 12, 0)
const QString re = QRegularExpression::wildcardToRegularExpression( glob );
const QString re = QRegularExpression::wildcardToRegularExpression( glob );

const QRegularExpression globRx( re );
if ( globRx.match( name ).hasMatch() )
return true;
const QRegularExpression globRx( re );
if ( globRx.match( name ).hasMatch() )
return true;
#else
QRegExp rx( glob );
rx.setPatternSyntax( QRegExp::Wildcard );
if ( rx.indexIn( name ) != -1 )
return true;
QRegExp rx( glob );
rx.setPatternSyntax( QRegExp::Wildcard );
if ( rx.indexIn( name ) != -1 )
return true;
#endif
}
}
}
return false;
@@ -46,6 +46,14 @@ class CORE_EXPORT QgsFileUtils
*/
static QStringList extensionsFromFilter( const QString &filter );

/**
* Giiven a \a filter string like "GeoTIFF Files (*.tiff *.tif)", extracts
* the wildcard portion of this filter (i.e. "*.tiff *.tif").
*
* \since QGIS 3.18
*/
static QString wildcardsFromFilter( const QString &filter );

/**
* Returns TRUE if the given \a fileName matches a file \a filter string.
*
@@ -31,6 +31,21 @@ def testExtensionsFromFilter(self):
self.assertEqual(QgsFileUtils.extensionsFromFilter('PNG Files (*.PNG)'), ['PNG'])
self.assertEqual(QgsFileUtils.extensionsFromFilter('Geotiff Files (*.tiff *.tif)'), ['tiff', 'tif'])

def testWildcardsFromFilter(self):
self.assertEqual(QgsFileUtils.wildcardsFromFilter(''), '')
self.assertEqual(QgsFileUtils.wildcardsFromFilter('bad'), '')
self.assertEqual(QgsFileUtils.wildcardsFromFilter('*'), '')
self.assertEqual(QgsFileUtils.wildcardsFromFilter('*.'), '')
self.assertEqual(QgsFileUtils.wildcardsFromFilter('Tiff files'), '')
self.assertEqual(QgsFileUtils.wildcardsFromFilter('(*.*)'), '*.*')
self.assertEqual(QgsFileUtils.wildcardsFromFilter('PNG Files (*.png)'), '*.png')
self.assertEqual(QgsFileUtils.wildcardsFromFilter('Tif Files (*.tif)'), '*.tif')
self.assertEqual(QgsFileUtils.wildcardsFromFilter('PNG Files (*.PNG)'), '*.PNG')
self.assertEqual(QgsFileUtils.wildcardsFromFilter('Geotiff Files (*.tiff *.tif)'), '*.tiff *.tif')
self.assertEqual(QgsFileUtils.wildcardsFromFilter('Geotiff Files (*.tiff *.tif *.TIFF)'), '*.tiff *.tif *.TIFF')
self.assertEqual(QgsFileUtils.wildcardsFromFilter('EPT files (ept.json)'), 'ept.json')
self.assertEqual(QgsFileUtils.wildcardsFromFilter('EPT files (ept.json EPT.JSON)'), 'ept.json EPT.JSON')

def testFileMatchesFilter(self):
self.assertFalse(QgsFileUtils.fileMatchesFilter('/home/me/test.tif', ''))
self.assertFalse(QgsFileUtils.fileMatchesFilter('/home/me/test.tif', 'bad'))

0 comments on commit d51e752

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