Skip to content
Permalink
Browse files

Add utility to determine whether a certain file name matches a given …

…file filter string
  • Loading branch information
nyalldawson committed Nov 4, 2020
1 parent e5fe9bf commit 74722b350c57c4736d509320cb93a2b0018d1612
@@ -37,6 +37,16 @@ containing "tiff", "tif". The initial '.' is stripped off the extension.
.. seealso:: :py:func:`ensureFileNameHasExtension`

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

static bool fileMatchesFilter( const QString &fileName, const QString &filter );
%Docstring
Returns ``True`` if the given ``fileName`` matches a file ``filter`` string.

E.g a filter of "GeoTIFF Files (*.tiff *.tif)" would return ``True`` for a ``fileName``
of "/home/test.tif", or ``False`` for "/home/test.jpg".

.. versionadded:: 3.18
%End

static QString ensureFileNameHasExtension( const QString &fileName, const QStringList &extensions );
@@ -56,6 +56,30 @@ QStringList QgsFileUtils::extensionsFromFilter( const QString &filter )
return extensions;
}

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() )
{
const QStringList globPatterns = matches.captured( 1 ).split( ' ' );
for ( const QString &glob : globPatterns )
{
const QString re = QRegularExpression::wildcardToRegularExpression( glob );
const QRegularExpression globRx( re );
if ( globRx.match( name ).hasMatch() )
return true;
}
}
}
return false;
}

QString QgsFileUtils::ensureFileNameHasExtension( const QString &f, const QStringList &extensions )
{
if ( extensions.empty() || f.isEmpty() )
@@ -46,6 +46,16 @@ class CORE_EXPORT QgsFileUtils
*/
static QStringList extensionsFromFilter( const QString &filter );

/**
* Returns TRUE if the given \a fileName matches a file \a filter string.
*
* E.g a filter of "GeoTIFF Files (*.tiff *.tif)" would return TRUE for a \a fileName
* of "/home/test.tif", or FALSE for "/home/test.jpg".
*
* \since QGIS 3.18
*/
static bool fileMatchesFilter( const QString &fileName, const QString &filter );

/**
* Ensures that a \a fileName ends with an extension from the provided list of
* \a extensions.
@@ -31,6 +31,34 @@ def testExtensionsFromFilter(self):
self.assertEqual(QgsFileUtils.extensionsFromFilter('PNG Files (*.PNG)'), ['PNG'])
self.assertEqual(QgsFileUtils.extensionsFromFilter('Geotiff Files (*.tiff *.tif)'), ['tiff', 'tif'])

def testFileMatchesFilter(self):
self.assertFalse(QgsFileUtils.fileMatchesFilter('/home/me/test.tif', ''))
self.assertFalse(QgsFileUtils.fileMatchesFilter('/home/me/test.tif', 'bad'))
self.assertFalse(QgsFileUtils.fileMatchesFilter('/home/me/test.tif', '*'))
self.assertFalse(QgsFileUtils.fileMatchesFilter('/home/me/test.tif', '*.'))
self.assertFalse(QgsFileUtils.fileMatchesFilter('/home/me/test.tif', 'Tiff files'))
self.assertFalse(QgsFileUtils.fileMatchesFilter('/home/me/test.tif', '(*.)'))
self.assertTrue(QgsFileUtils.fileMatchesFilter('/home/me/test.tif', '(*.*)'))
self.assertFalse(QgsFileUtils.fileMatchesFilter('/home/me/test.tif', 'PNG Files (*.png)'))
self.assertTrue(QgsFileUtils.fileMatchesFilter('/home/me/test.tif', 'Tif Files (*.tif)'))
self.assertFalse(QgsFileUtils.fileMatchesFilter('/home/me/test.tif', 'PNG Files (*.PNG)'))
self.assertFalse(QgsFileUtils.fileMatchesFilter('/home/me/test.tif', 'Tif Files (*.TIF)'))
self.assertTrue(QgsFileUtils.fileMatchesFilter('/home/me/test.tif', 'Geotiff Files (*.tiff *.tif)'))
self.assertTrue(QgsFileUtils.fileMatchesFilter('/home/me/test.tiff', 'Geotiff Files (*.tiff *.tif)'))
self.assertTrue(QgsFileUtils.fileMatchesFilter('/home/me/test.TIFF', 'Geotiff Files (*.tiff *.tif *.TIFF)'))
self.assertFalse(QgsFileUtils.fileMatchesFilter('/home/me/test.tif', 'PNG Files (*.png);;BMP Files (*.bmp)'))
self.assertTrue(QgsFileUtils.fileMatchesFilter('/home/me/test.bmp', 'PNG Files (*.png);;BMP Files (*.bmp)'))
self.assertTrue(QgsFileUtils.fileMatchesFilter('/home/me/test.png', 'PNG Files (*.png);;BMP Files (*.bmp)'))
self.assertFalse(QgsFileUtils.fileMatchesFilter('/home/me/test.png', 'EPT files (ept.json)'))
self.assertTrue(QgsFileUtils.fileMatchesFilter('/home/me/ept.json', 'EPT files (ept.json)'))
self.assertTrue(QgsFileUtils.fileMatchesFilter('/home/me/ept.json', 'EPT files (ept.json EPT.JSON)'))
self.assertTrue(QgsFileUtils.fileMatchesFilter('/home/me/EPT.JSON', 'EPT files (ept.json EPT.JSON)'))
self.assertTrue(QgsFileUtils.fileMatchesFilter('/home/me/ept.json', 'EPT files (ept.json);;Entwine files (entwine.json)'))
self.assertTrue(
QgsFileUtils.fileMatchesFilter('/home/me/entwine.json', 'EPT files (ept.json);;Entwine files (entwine.json)'))
self.assertFalse(
QgsFileUtils.fileMatchesFilter('/home/me/ep.json', 'EPT files (ept.json);;Entwine files (entwine.json)'))

def testEnsureFileNameHasExtension(self):
self.assertEqual(QgsFileUtils.ensureFileNameHasExtension('', ['']), '')
self.assertEqual(QgsFileUtils.ensureFileNameHasExtension('', []), '')

0 comments on commit 74722b3

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