Skip to content

Commit ebf0679

Browse files
authored
Merge pull request OSGeo#11822 from rouault/python_user_friendly_alg_arg
Python bindings: add syntaxic sugar to get/set algorithm arguments + doc
2 parents 5935820 + 8edd601 commit ebf0679

16 files changed

+428
-249
lines changed

autotest/utilities/test_gdalalg_info.py

+5-7
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717

1818

1919
def get_info_alg():
20-
reg = gdal.GetGlobalAlgorithmRegistry()
21-
return reg.InstantiateAlg("info")
20+
return gdal.GetGlobalAlgorithmRegistry()["info"]
2221

2322

2423
@pytest.mark.parametrize(
@@ -31,7 +30,7 @@ def get_info_alg():
3130
def test_gdalalg_info_on_raster(args):
3231
info = get_info_alg()
3332
assert info.ParseRunAndFinalize(args)
34-
output_string = info.GetActualAlgorithm().GetArg("output-string").Get()
33+
output_string = info["output-string"]
3534
assert output_string.startswith("Driver: GTiff/GeoTIFF")
3635

3736

@@ -57,7 +56,7 @@ def test_gdalalg_info_on_raster_invalid_arg():
5756
def test_gdalalg_info_on_vector(args):
5857
info = get_info_alg()
5958
assert info.ParseRunAndFinalize(args)
60-
output_string = info.GetActualAlgorithm().GetArg("output-string").Get()
59+
output_string = info["output-string"]
6160
assert output_string.startswith("INFO: Open of")
6261

6362

@@ -75,8 +74,7 @@ def test_gdalalg_info_invalid_arg():
7574

7675
def test_gdalalg_info_run_cannot_be_run():
7776
info = get_info_alg()
78-
ds = gdal.GetDriverByName("MEM").Create("", 1, 1)
79-
info.GetArg("input").SetDataset(ds)
77+
info["input"] = gdal.GetDriverByName("MEM").Create("", 1, 1)
8078
with pytest.raises(Exception, match="method should not be called directly"):
8179
info.Run()
8280

@@ -108,6 +106,6 @@ def test_gdalalg_info_mixed_raster_vector_with_invalid_arg(tmp_vsimem):
108106
def test_gdalalg_info_mixed_run_without_arg(tmp_vsimem):
109107

110108
info = get_info_alg()
111-
info.GetArg("input").Get().SetName("data/utmsmall.tif")
109+
info["input"] = "data/utmsmall.tif"
112110
with pytest.raises(Exception, match="should not be called directly"):
113111
assert info.Run()

autotest/utilities/test_gdalalg_pipeline.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717

1818

1919
def get_pipeline_alg():
20-
reg = gdal.GetGlobalAlgorithmRegistry()
21-
return reg.InstantiateAlg("pipeline")
20+
return gdal.GetGlobalAlgorithmRegistry()["pipeline"]
2221

2322

2423
def test_gdalalg_pipeline_read_and_write(tmp_vsimem):
@@ -44,6 +43,6 @@ def my_progress(pct, msg, user_data):
4443
def test_gdalalg_pipeline_mixed_run_without_arg(tmp_vsimem):
4544

4645
pipeline = get_pipeline_alg()
47-
pipeline.GetArg("input").Get().SetDataset(gdal.OpenEx("../ogr/data/poly.shp"))
46+
pipeline["input"] = gdal.OpenEx("../ogr/data/poly.shp")
4847
with pytest.raises(Exception, match="should not be called directly"):
4948
assert pipeline.Run()

autotest/utilities/test_gdalalg_raster_convert.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def test_gdalalg_raster_convert_to_mem():
5353
convert = get_convert_alg()
5454
assert convert.ParseCommandLineArguments(["--of=MEM", "data/utmsmall.tif", ""])
5555
assert convert.Run()
56-
out_ds = convert.GetArg("output").Get().GetDataset()
56+
out_ds = convert["output"].GetDataset()
5757
assert out_ds.GetRasterBand(1).Checksum() == 50054
5858

5959

@@ -82,8 +82,7 @@ def test_gdalalg_raster_convert_append(tmp_vsimem):
8282

8383
def test_gdalalg_raster_convert_error_output_already_set():
8484
convert = get_convert_alg()
85-
ds = gdal.GetDriverByName("MEM").Create("", 1, 1)
86-
convert.GetArg("output").Get().SetDataset(ds)
85+
convert["output"] = gdal.GetDriverByName("MEM").Create("", 1, 1)
8786
assert convert.ParseCommandLineArguments(["data/utmsmall.tif"])
8887
with pytest.raises(
8988
Exception,

autotest/utilities/test_gdalalg_raster_edit.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@
1717

1818

1919
def get_edit_alg():
20-
reg = gdal.GetGlobalAlgorithmRegistry()
21-
raster = reg.InstantiateAlg("raster")
22-
return raster.InstantiateSubAlgorithm("edit")
20+
return gdal.GetGlobalAlgorithmRegistry()["raster"]["edit"]
2321

2422

2523
def test_gdalalg_raster_edit_read_only(tmp_vsimem):
@@ -28,7 +26,7 @@ def test_gdalalg_raster_edit_read_only(tmp_vsimem):
2826
gdal.FileFromMemBuffer(tmp_filename, open("../gcore/data/byte.tif", "rb").read())
2927

3028
pipeline = get_edit_alg()
31-
pipeline.GetArg("dataset").Set(gdal.OpenEx(tmp_filename))
29+
pipeline["dataset"] = gdal.OpenEx(tmp_filename)
3230
with pytest.raises(
3331
Exception, match="edit: Dataset should be opened in update mode"
3432
):

autotest/utilities/test_gdalalg_raster_info.py

+16-18
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@
1919

2020

2121
def get_info_alg():
22-
reg = gdal.GetGlobalAlgorithmRegistry()
23-
raster = reg.InstantiateAlg("raster")
24-
return raster.InstantiateSubAlgorithm("info")
22+
return gdal.GetGlobalAlgorithmRegistry()["raster"]["info"]
2523

2624

2725
def test_gdalalg_raster_info_stdout():
@@ -41,7 +39,7 @@ def test_gdalalg_raster_info_stdout():
4139
def test_gdalalg_raster_info():
4240
info = get_info_alg()
4341
assert info.ParseRunAndFinalize(["--format=text", "data/utmsmall.tif"])
44-
output_string = info.GetArg("output-string").Get()
42+
output_string = info["output-string"]
4543
assert output_string.startswith("Driver: GTiff/GeoTIFF")
4644

4745

@@ -50,45 +48,45 @@ def test_gdalalg_raster_info_mm_checksum():
5048
assert info.ParseRunAndFinalize(
5149
["--format=text", "--mm", "--checksum", "data/utmsmall.tif"]
5250
)
53-
output_string = info.GetArg("output-string").Get()
51+
output_string = info["output-string"]
5452
assert " Computed Min/Max=0.000,255.000" in output_string
5553
assert "Checksum=" in output_string
5654

5755

5856
def test_gdalalg_raster_info_stats():
5957
info = get_info_alg()
6058
ds = gdal.Translate("", "../gcore/data/byte.tif", format="MEM")
61-
info.GetArg("input").SetDataset(ds)
59+
info["input"] = ds
6260
assert info.ParseRunAndFinalize(["--stats"])
63-
output_string = info.GetArg("output-string").Get()
61+
output_string = info["output-string"]
6462
j = json.loads(output_string)
6563
assert "stdDev" in j["bands"][0]
6664

6765

6866
def test_gdalalg_raster_info_approx_stats():
6967
info = get_info_alg()
7068
ds = gdal.Translate("", "../gcore/data/byte.tif", format="MEM")
71-
info.GetArg("input").SetDataset(ds)
69+
info["input"] = ds
7270
assert info.ParseRunAndFinalize(["--approx-stats"])
73-
output_string = info.GetArg("output-string").Get()
71+
output_string = info["output-string"]
7472
j = json.loads(output_string)
7573
assert "stdDev" in j["bands"][0]
7674

7775

7876
def test_gdalalg_raster_info_hist():
7977
info = get_info_alg()
8078
ds = gdal.Translate("", "../gcore/data/byte.tif", format="MEM")
81-
info.GetArg("input").SetDataset(ds)
79+
info["input"] = ds
8280
assert info.ParseRunAndFinalize(["--hist"])
83-
output_string = info.GetArg("output-string").Get()
81+
output_string = info["output-string"]
8482
j = json.loads(output_string)
8583
assert "histogram" in j["bands"][0]
8684

8785

8886
def test_gdalalg_raster_info_no_options():
8987
info = get_info_alg()
9088
ds = gdal.Translate("", "../gcore/data/byte.tif", format="MEM")
91-
info.GetArg("input").SetDataset(ds)
89+
info["input"] = ds
9290
assert info.ParseRunAndFinalize(
9391
["--no-gcp", "--no-md", "--no-ct", "--no-fl", "--no-nodata", "--no-mask"]
9492
)
@@ -98,9 +96,9 @@ def test_gdalalg_raster_info_list_mdd():
9896
info = get_info_alg()
9997
ds = gdal.Translate("", "../gcore/data/byte.tif", format="MEM")
10098
ds.SetMetadataItem("foo", "bar", "MY_DOMAIN")
101-
info.GetArg("input").SetDataset(ds)
99+
info["input"] = ds
102100
assert info.ParseRunAndFinalize(["--list-mdd"])
103-
output_string = info.GetArg("output-string").Get()
101+
output_string = info["output-string"]
104102
j = json.loads(output_string)
105103
assert "MY_DOMAIN" in j["metadata"]["metadataDomains"]
106104

@@ -109,9 +107,9 @@ def test_gdalalg_raster_info_mdd_all():
109107
info = get_info_alg()
110108
ds = gdal.Translate("", "../gcore/data/byte.tif", format="MEM")
111109
ds.SetMetadataItem("foo", "bar", "MY_DOMAIN")
112-
info.GetArg("input").SetDataset(ds)
110+
info["input"] = ds
113111
assert info.ParseRunAndFinalize(["--mdd=all"])
114-
output_string = info.GetArg("output-string").Get()
112+
output_string = info["output-string"]
115113
j = json.loads(output_string)
116114
assert j["metadata"] == {
117115
"": {"AREA_OR_POINT": "Area"},
@@ -128,7 +126,7 @@ def test_gdalalg_raster_info_list_subdataset():
128126
assert info.ParseRunAndFinalize(
129127
["--input=../gcore/data/tiff_with_subifds.tif", "--subdataset=2"]
130128
)
131-
output_string = info.GetArg("output-string").Get()
129+
output_string = info["output-string"]
132130
j = json.loads(output_string)
133131
assert j["description"] == "GTIFF_DIR:2:../gcore/data/tiff_with_subifds.tif"
134132

@@ -149,7 +147,7 @@ def test_gdalalg_raster_info_list_subdataset_error_cannot_open_subdataset():
149147
ds = gdal.GetDriverByName("MEM").Create("", 1, 1)
150148
ds.SetMetadataItem("SUBDATASET_1_DESC", "desc", "SUBDATASETS")
151149
ds.SetMetadataItem("SUBDATASET_1_NAME", "i_do_not_exist", "SUBDATASETS")
152-
info.GetArg("input").SetDataset(ds)
150+
info["input"] = ds
153151
with pytest.raises(
154152
Exception,
155153
match="i_do_not_exist",

0 commit comments

Comments
 (0)