From 069960b7cbee39d8a014631c46d8e9bff3d2c603 Mon Sep 17 00:00:00 2001 From: Kevin Wurster Date: Mon, 13 Feb 2017 17:36:26 -0500 Subject: [PATCH] Option to disable key and value normalization. --- rasterio/_env.pyx | 23 ++++++++++++++++------- tests/test_env.py | 11 +++++++++++ 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/rasterio/_env.pyx b/rasterio/_env.pyx index 6b66fe92..2dba37ec 100644 --- a/rasterio/_env.pyx +++ b/rasterio/_env.pyx @@ -59,12 +59,17 @@ def driver_count(): return GDALGetDriverCount() + OGRGetDriverCount() -cpdef get_gdal_config(key): +cpdef get_gdal_config(key, normalize=True): """Get the value of a GDAL configuration option""" - key = key.upper().encode('utf-8') + if normalize: + key = key.upper() + + key = key.encode('utf-8') val = CPLGetConfigOption(key, NULL) if not val: return None + elif not normalize: + return val else: if val == u'ON': return True @@ -74,19 +79,23 @@ cpdef get_gdal_config(key): return val -cpdef set_gdal_config(key, val): +cpdef set_gdal_config(key, val, normalize=True): """Set a GDAL configuration option's value""" - key = key.upper().encode('utf-8') + if normalize: + key = key.upper() + key = key.encode('utf-8') if isinstance(val, string_types): val = val.encode('utf-8') - else: + elif normalize: val = ('ON' if val else 'OFF').encode('utf-8') CPLSetConfigOption(key, val) -cpdef del_gdal_config(key): +cpdef del_gdal_config(key, normalize=True): """Delete a GDAL configuration option""" - key = key.upper().encode('utf-8') + if normalize: + key = key.upper() + key = key.encode('utf-8') CPLSetConfigOption(key, NULL) diff --git a/tests/test_env.py b/tests/test_env.py index 807117c8..5dc57451 100644 --- a/tests/test_env.py +++ b/tests/test_env.py @@ -41,6 +41,17 @@ def test_gdal_config_accessers(): assert get_gdal_config('foo') is None +def test_gdal_config_accessors_no_normalize(): + """Disables casting keys to upper case and normalizing values to boolean + Python values. + """ + assert get_gdal_config('foo') is None + set_gdal_config('foo', 'ON', normalize=False) + assert get_gdal_config('foo', normalize=False) == 'ON' + del_gdal_config('foo', normalize=False) + assert get_gdal_config('foo', normalize=False) is None + + # The 'gdalenv' fixture ensures that gdal configuration is deleted # at the end of the test, making tests as isolates as GDAL allows.