Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix enhancement config loading when yaml file is empty #387

Merged
merged 5 commits into from
Aug 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ env:
- PYTHON_VERSION=$TRAVIS_PYTHON_VERSION
- NUMPY_VERSION=stable
- MAIN_CMD='python setup.py'
# 'krb5' is added as a workaround for an issue with gdal
# see: https://github.com/conda-forge/gdal-feedstock/issues/205
- CONDA_DEPENDENCIES='xarray dask toolz Cython sphinx cartopy pillow matplotlib scipy pyyaml pyproj pyresample coveralls coverage codecov behave netcdf4 h5py h5netcdf gdal rasterio imageio pyhdf mock krb5 libtiff'
- CONDA_DEPENDENCIES='xarray dask toolz Cython sphinx cartopy pillow matplotlib scipy pyyaml pyproj pyresample coveralls coverage codecov behave netcdf4 h5py h5netcdf gdal rasterio imageio pyhdf mock libtiff'
- PIP_DEPENDENCIES='trollsift trollimage pyspectral pyorbital'
- SETUP_XVFB=False
- EVENT_TYPE='push pull_request'
Expand All @@ -28,6 +26,9 @@ matrix:
install:
- git clone --depth 1 git://github.com/astropy/ci-helpers.git
- source ci-helpers/travis/setup_conda.sh
# See https://github.com/conda/conda/issues/7626#issuecomment-412922028
- conda config --remove channels defaults
- conda install -y $CONDA_DEPENDENCIES
script:
- coverage run --source=satpy setup.py test
- coverage run -a --source=satpy -m behave satpy/tests/features --tags=-download
Expand Down
15 changes: 15 additions & 0 deletions satpy/tests/test_writers.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ class TestEnhancerUserConfigs(unittest.TestCase):
ENH_ENH_FN = os.path.join('enhancements', ENH_FN)
ENH_FN2 = 'test_sensor2.yaml'
ENH_ENH_FN2 = os.path.join('enhancements', ENH_FN2)
ENH_FN3 = 'test_empty.yaml'

TEST_CONFIGS = {
ENH_FN: """
Expand Down Expand Up @@ -175,6 +176,7 @@ class TestEnhancerUserConfigs(unittest.TestCase):
sensor_name: visir/test_sensor2

""",
ENH_FN3: """""",
}

@classmethod
Expand All @@ -196,6 +198,19 @@ def tearDownClass(cls):
elif os.path.isfile(fn):
os.remove(fn)

def test_enhance_empty_config(self):
"""Test Enhancer doesn't fail with empty enhancement file."""
from satpy.writers import Enhancer, get_enhanced_image
from xarray import DataArray
ds = DataArray(np.arange(1, 11.).reshape((2, 5)),
attrs=dict(sensor='test_empty', mode='L'),
dims=['y', 'x'])
e = Enhancer()
self.assertIsNotNone(e.enhancement_tree)
get_enhanced_image(ds, enhancer=e)
self.assertSetEqual(set(e.sensor_enhancement_configs),
{self.ENH_FN3})

def test_enhance_with_sensor_no_entry(self):
"""Test enhancing an image that has no configuration sections."""
from satpy.writers import Enhancer, get_enhanced_image
Expand Down
7 changes: 6 additions & 1 deletion satpy/writers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,12 @@ def add_config_to_tree(self, *decision_dict):
for config_file in decision_dict:
if os.path.isfile(config_file):
with open(config_file) as fd:
enhancement_section = yaml.load(fd).get(self.prefix, {})
enhancement_config = yaml.load(fd)
if enhancement_config is None:
# empty file
continue
enhancement_section = enhancement_config.get(
self.prefix, {})
if not enhancement_section:
LOG.debug("Config '{}' has no '{}' section or it is empty".format(config_file, self.prefix))
continue
Expand Down