Option to disable key and value normalization.

This commit is contained in:
Kevin Wurster 2017-02-13 17:36:26 -05:00
parent 3a3eb3bd97
commit 069960b7cb
2 changed files with 27 additions and 7 deletions

View File

@ -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(<const char *>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(<const char *>key, <const char *>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(<const char *>key, NULL)

View File

@ -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.