Skip to content

Commit

Permalink
Python - Use flynt to transform % strings without braces
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustry authored and nyalldawson committed Feb 1, 2023
1 parent da8bb1d commit 38b4533
Show file tree
Hide file tree
Showing 131 changed files with 554 additions and 617 deletions.
2 changes: 1 addition & 1 deletion tests/src/python/test_authmanager_oauth2_ows.py
Expand Up @@ -234,7 +234,7 @@ def _getWMSLayer(cls, layers, layer_name=None, authcfg=None):
}
if authcfg is not None:
parms.update({'authcfg': authcfg})
uri = '&'.join([("{}={}".format(k, v.replace('=', '%3D'))) for k, v in list(parms.items())])
uri = '&'.join([f"{k}={v.replace('=', '%3D')}" for k, v in list(parms.items())])
wms_layer = QgsRasterLayer(uri, layer_name, 'wms')
return wms_layer

Expand Down
2 changes: 1 addition & 1 deletion tests/src/python/test_authmanager_ogr_postgres.py
Expand Up @@ -163,7 +163,7 @@ def setUpClass(cls):

cls.server = subprocess.Popen([os.path.join(QGIS_POSTGRES_EXECUTABLE_PATH, 'postgres'), '-D',
cls.data_path, '-c',
"config_file=%s" % cls.pg_conf],
f"config_file={cls.pg_conf}"],
env=env,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
Expand Down
8 changes: 4 additions & 4 deletions tests/src/python/test_authmanager_password_ows.py
Expand Up @@ -162,7 +162,7 @@ def _getWMSLayer(cls, layers, layer_name=None, authcfg=None):
}
if authcfg is not None:
parms.update({'authcfg': authcfg})
uri = '&'.join([("{}={}".format(k, v.replace('=', '%3D'))) for k, v in list(parms.items())])
uri = '&'.join([f"{k}={v.replace('=', '%3D')}" for k, v in list(parms.items())])
wms_layer = QgsRasterLayer(uri, layer_name, 'wms')
return wms_layer

Expand All @@ -175,7 +175,7 @@ def _getGeoJsonLayer(cls, type_name, layer_name=None, authcfg=None):
layer_name = 'geojson_' + type_name
uri = f'{cls.protocol}://{cls.hostname}:{cls.port}/?MAP={cls.project_path}&SERVICE=WFS&REQUEST=GetFeature&TYPENAME={urllib.parse.quote(type_name)}&VERSION=2.0.0&OUTPUTFORMAT=geojson'
if authcfg is not None:
uri += " authcfg='%s'" % authcfg
uri += f" authcfg='{authcfg}'"
geojson_layer = QgsVectorLayer(uri, layer_name, 'ogr')
return geojson_layer

Expand Down Expand Up @@ -235,7 +235,7 @@ def testInvalidAuthFileDownload(self):
loop.exec_()

self.assertTrue(self.error_was_called)
self.assertTrue("Download failed: Host requires authentication" in str(self.error_args), "Error args is: %s" % str(self.error_args))
self.assertTrue("Download failed: Host requires authentication" in str(self.error_args), f"Error args is: {str(self.error_args)}")

def testValidAuthFileDownload(self):
"""
Expand Down Expand Up @@ -272,7 +272,7 @@ def testValidAuthFileDownload(self):

# Check the we've got a likely PNG image
self.assertTrue(self.completed_was_called)
self.assertTrue(os.path.getsize(destination) > 2000, "Image size: %s" % os.path.getsize(destination)) # > 1MB
self.assertTrue(os.path.getsize(destination) > 2000, f"Image size: {os.path.getsize(destination)}") # > 1MB
with open(destination, 'rb') as f:
self.assertTrue(b'PNG' in f.read()) # is a PNG

Expand Down
2 changes: 1 addition & 1 deletion tests/src/python/test_authmanager_pki_ows.py
Expand Up @@ -185,7 +185,7 @@ def _getWMSLayer(cls, layers, layer_name=None, authcfg=None):
}
if authcfg is not None:
parms.update({'authcfg': authcfg})
uri = '&'.join([("{}={}".format(k, v.replace('=', '%3D'))) for k, v in list(parms.items())])
uri = '&'.join([f"{k}={v.replace('=', '%3D')}" for k, v in list(parms.items())])
wms_layer = QgsRasterLayer(uri, layer_name, 'wms')
return wms_layer

Expand Down
4 changes: 2 additions & 2 deletions tests/src/python/test_db_manager_postgis.py
Expand Up @@ -147,7 +147,7 @@ def setUpServer(cls):

cls.server = subprocess.Popen([os.path.join(QGIS_POSTGRES_EXECUTABLE_PATH, 'postgres'), '-D',
cls.data_path, '-c',
"config_file=%s" % cls.pg_conf],
f"config_file={cls.pg_conf}"],
env=os.environ,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
Expand All @@ -166,7 +166,7 @@ def setUpServer(cls):
test_sql = os.path.join(unitTestDataPath('provider'), 'testdata_pg.sql')
subprocess.check_call([os.path.join(QGIS_POSTGRES_EXECUTABLE_PATH, 'psql'), '-h', 'localhost', '-p', cls.port, '-f', test_sql, cls.dbname])
# Create a role
subprocess.check_call([os.path.join(QGIS_POSTGRES_EXECUTABLE_PATH, 'psql'), '-h', 'localhost', '-p', cls.port, '-c', 'CREATE ROLE "%s" WITH SUPERUSER LOGIN' % cls.username, cls.dbname])
subprocess.check_call([os.path.join(QGIS_POSTGRES_EXECUTABLE_PATH, 'psql'), '-h', 'localhost', '-p', cls.port, '-c', f'CREATE ROLE "{cls.username}" WITH SUPERUSER LOGIN', cls.dbname])

@classmethod
def setUpProvider(cls, authId):
Expand Down
12 changes: 6 additions & 6 deletions tests/src/python/test_layer_dependencies.py
Expand Up @@ -75,11 +75,11 @@ def setUp(self):
con.commit()
con.close()

self.pointsLayer = QgsVectorLayer("dbname='%s' table=\"node\" (geom) sql=" % fn, "points", "spatialite")
self.pointsLayer = QgsVectorLayer(f"dbname='{fn}' table=\"node\" (geom) sql=", "points", "spatialite")
assert (self.pointsLayer.isValid())
self.linesLayer = QgsVectorLayer("dbname='%s' table=\"section\" (geom) sql=" % fn, "lines", "spatialite")
self.linesLayer = QgsVectorLayer(f"dbname='{fn}' table=\"section\" (geom) sql=", "lines", "spatialite")
assert (self.linesLayer.isValid())
self.pointsLayer2 = QgsVectorLayer("dbname='%s' table=\"node2\" (geom) sql=" % fn, "_points2", "spatialite")
self.pointsLayer2 = QgsVectorLayer(f"dbname='{fn}' table=\"node2\" (geom) sql=", "_points2", "spatialite")
assert (self.pointsLayer2.isValid())
QgsProject.instance().addMapLayers([self.pointsLayer, self.linesLayer, self.pointsLayer2])

Expand Down Expand Up @@ -281,11 +281,11 @@ def test_signalConnection(self):
# remove all layers
QgsProject.instance().removeAllMapLayers()
# set dependencies and add back layers
self.pointsLayer = QgsVectorLayer("dbname='%s' table=\"node\" (geom) sql=" % self.fn, "points", "spatialite")
self.pointsLayer = QgsVectorLayer(f"dbname='{self.fn}' table=\"node\" (geom) sql=", "points", "spatialite")
assert (self.pointsLayer.isValid())
self.linesLayer = QgsVectorLayer("dbname='%s' table=\"section\" (geom) sql=" % self.fn, "lines", "spatialite")
self.linesLayer = QgsVectorLayer(f"dbname='{self.fn}' table=\"section\" (geom) sql=", "lines", "spatialite")
assert (self.linesLayer.isValid())
self.pointsLayer2 = QgsVectorLayer("dbname='%s' table=\"node2\" (geom) sql=" % self.fn, "_points2", "spatialite")
self.pointsLayer2 = QgsVectorLayer(f"dbname='{self.fn}' table=\"node2\" (geom) sql=", "_points2", "spatialite")
assert (self.pointsLayer2.isValid())
self.pointsLayer.setDependencies([QgsMapLayerDependency(self.linesLayer.id())])
self.pointsLayer2.setDependencies([QgsMapLayerDependency(self.pointsLayer.id())])
Expand Down
2 changes: 1 addition & 1 deletion tests/src/python/test_offline_editing_wfs.py
Expand Up @@ -95,7 +95,7 @@ def setUp(self):
self.port = int(re.findall(br':(\d+)', line)[0])
assert self.port != 0
# Wait for the server process to start
assert waitServer('http://127.0.0.1:%s' % self.port), "Server is not responding!"
assert waitServer(f'http://127.0.0.1:{self.port}'), "Server is not responding!"
self._setUp()

def tearDown(self):
Expand Down
2 changes: 1 addition & 1 deletion tests/src/python/test_plugindependencies.py
Expand Up @@ -38,7 +38,7 @@ def setUpClass(cls):

QCoreApplication.setOrganizationName("QGIS")
QCoreApplication.setOrganizationDomain("qgis.org")
QCoreApplication.setApplicationName("QGIS-TEST-%s" % uuid.uuid1())
QCoreApplication.setApplicationName(f"QGIS-TEST-{uuid.uuid1()}")
qgis_app = start_app()

# Installed plugins
Expand Down
2 changes: 1 addition & 1 deletion tests/src/python/test_processing_importintopostgis.py
Expand Up @@ -86,7 +86,7 @@ def test_import(self):
# Check that data have been imported correctly
exported = QgsVectorLayer(unitTestDataPath() + '/points.shp', 'exported')
self.assertTrue(exported.isValid())
imported = QgsVectorLayer("service='qgis_test' table=\"CamelCaseSchema\".\"%s\" (geom)" % table_name, 'imported', 'postgres')
imported = QgsVectorLayer(f"service='qgis_test' table=\"CamelCaseSchema\".\"{table_name}\" (geom)", 'imported', 'postgres')
self.assertTrue(imported.isValid())
imported_fields = [f.name() for f in imported.fields()]
for f in exported.fields():
Expand Down
26 changes: 9 additions & 17 deletions tests/src/python/test_provider_memory.py
Expand Up @@ -142,7 +142,7 @@ def testCtors(self):
testVectors = ["Point", "LineString", "Polygon", "MultiPoint", "MultiLineString", "MultiPolygon", "None"]
for v in testVectors:
layer = QgsVectorLayer(v, "test", "memory")
assert layer.isValid(), "Failed to create valid %s memory layer" % (v)
assert layer.isValid(), f"Failed to create valid {v} memory layer"

def testLayerGeometry(self):
testVectors = [("Point", QgsWkbTypes.PointGeometry, QgsWkbTypes.Point),
Expand Down Expand Up @@ -179,12 +179,10 @@ def testLayerGeometry(self):
for v in testVectors:
layer = QgsVectorLayer(v[0], "test", "memory")

myMessage = ('Expected: %s\nGot: %s\n' %
(v[1], layer.geometryType()))
myMessage = f'Expected: {v[1]}\nGot: {layer.geometryType()}\n'
assert layer.geometryType() == v[1], myMessage

myMessage = ('Expected: %s\nGot: %s\n' %
(v[2], layer.wkbType()))
myMessage = f'Expected: {v[2]}\nGot: {layer.wkbType()}\n'
assert layer.wkbType() == v[2], myMessage

def testAddFeatures(self):
Expand All @@ -196,8 +194,7 @@ def testAddFeatures(self):
QgsField("size", QVariant.Double)])
assert res, "Failed to add attributes"

myMessage = ('Expected: %s\nGot: %s\n' %
(3, len(provider.fields())))
myMessage = f'Expected: {3}\nGot: {len(provider.fields())}\n'

assert len(provider.fields()) == 3, myMessage

Expand All @@ -210,30 +207,25 @@ def testAddFeatures(self):

assert res, "Failed to add feature"

myMessage = ('Expected: %s\nGot: %s\n' %
(1, provider.featureCount()))
myMessage = f'Expected: {1}\nGot: {provider.featureCount()}\n'
assert provider.featureCount() == 1, myMessage

for f in provider.getFeatures(QgsFeatureRequest()):
myMessage = ('Expected: %s\nGot: %s\n' %
("Johny", f[0]))
myMessage = f"Expected: {'Johny'}\nGot: {f[0]}\n"

assert f[0] == "Johny", myMessage

myMessage = ('Expected: %s\nGot: %s\n' %
(20, f[1]))
myMessage = f'Expected: {20}\nGot: {f[1]}\n'

assert f[1] == 20, myMessage

myMessage = ('Expected: %s\nGot: %s\n' %
(0.3, f[2]))
myMessage = f'Expected: {0.3}\nGot: {f[2]}\n'

assert (f[2] - 0.3) < 0.0000001, myMessage

geom = f.geometry()

myMessage = ('Expected: %s\nGot: %s\n' %
("Point (10 10)", str(geom.asWkt())))
myMessage = f"Expected: {'Point (10 10)'}\nGot: {str(geom.asWkt())}\n"

assert compareWkt(str(geom.asWkt()), "Point (10 10)"), myMessage

Expand Down
2 changes: 1 addition & 1 deletion tests/src/python/test_provider_mssql.py
Expand Up @@ -430,7 +430,7 @@ def testStyle(self):
self.assertTrue(errmsg)

mFilePath = QDir.toNativeSeparators(
'{}/symbol_layer/{}.qml'.format(unitTestDataPath(), "singleSymbol"))
f"{unitTestDataPath()}/symbol_layer/singleSymbol.qml")
status = vl.loadNamedStyle(mFilePath)
self.assertTrue(status)

Expand Down
6 changes: 3 additions & 3 deletions tests/src/python/test_provider_ogr.py
Expand Up @@ -390,7 +390,7 @@ def testTriangleTINPolyhedralSurface(self):
datasource = os.path.join(self.basetestpath, 'test.csv')
with open(datasource, 'w') as f:
f.write('id,WKT\n')
f.write('1,"%s"' % row[0])
f.write(f'1,"{row[0]}"')

vl = QgsVectorLayer(datasource, 'test', 'ogr')
self.assertTrue(vl.isValid())
Expand Down Expand Up @@ -2312,7 +2312,7 @@ def test_provider_sublayer_details(self):
self.assertEqual(res[0].layerNumber(), 0)
self.assertEqual(res[0].name(), "ARC")
self.assertEqual(res[0].description(), "")
self.assertEqual(res[0].uri(), '{}|layername=ARC'.format(os.path.join(TEST_DATA_DIR, 'esri_coverage', 'testpolyavc')))
self.assertEqual(res[0].uri(), f"{os.path.join(TEST_DATA_DIR, 'esri_coverage', 'testpolyavc')}|layername=ARC")
self.assertEqual(res[0].providerKey(), "ogr")
self.assertEqual(res[0].type(), QgsMapLayerType.VectorLayer)
self.assertFalse(res[0].skippedContainerScan())
Expand Down Expand Up @@ -2465,7 +2465,7 @@ def test_provider_sublayer_details_fast_scan(self):
self.assertEqual(res[0].layerNumber(), 0)
self.assertEqual(res[0].name(), "ARC")
self.assertEqual(res[0].description(), "")
self.assertEqual(res[0].uri(), '{}|layername=ARC'.format(os.path.join(TEST_DATA_DIR, 'esri_coverage', 'testpolyavc')))
self.assertEqual(res[0].uri(), f"{os.path.join(TEST_DATA_DIR, 'esri_coverage', 'testpolyavc')}|layername=ARC")
self.assertEqual(res[0].providerKey(), "ogr")
self.assertEqual(res[0].type(), QgsMapLayerType.VectorLayer)
self.assertFalse(res[0].skippedContainerScan())
Expand Down
4 changes: 2 additions & 2 deletions tests/src/python/test_provider_ogr_gpkg.py
Expand Up @@ -1397,7 +1397,7 @@ def _lessdigits(s):

testPath = tmpfile + '|layername=bug_17795'
subSetString = '"name" = \'int\''
subSet = '|subset=%s' % subSetString
subSet = f'|subset={subSetString}'

# unfiltered
vl = QgsVectorLayer(testPath, 'test', 'ogr')
Expand Down Expand Up @@ -2131,7 +2131,7 @@ def testTransactionGroup(self):
for tmpfile in (tmpfile1, tmpfile2):
ds = ogr.GetDriverByName('GPKG').CreateDataSource(tmpfile)
for i in range(2):
lyr = ds.CreateLayer('test%s' % i, geom_type=ogr.wkbPoint)
lyr = ds.CreateLayer(f'test{i}', geom_type=ogr.wkbPoint)
lyr.CreateField(ogr.FieldDefn('str_field', ogr.OFTString))
f = ogr.Feature(lyr.GetLayerDefn())
f.SetGeometry(ogr.CreateGeometryFromWkt('POINT (1 1)'))
Expand Down

0 comments on commit 38b4533

Please sign in to comment.