Skip to content

Commit

Permalink
make execute() not require explicit '**kwargs'
Browse files Browse the repository at this point in the history
  • Loading branch information
ungarj committed Oct 29, 2018
1 parent 9e17e4c commit e25f0ca
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 31 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Expand Up @@ -8,6 +8,7 @@ Changelog
* use ``concurrent.futures`` instead of ``multiprocessing``
* make some dependencies optional (Flask, boto3, etc.)
* speed up ``count_tiles()``
* ``execute()`` function does not require explicit ``**kwargs`` anymore


----
Expand Down
2 changes: 1 addition & 1 deletion README.rst
Expand Up @@ -75,7 +75,7 @@ and a ``.py`` file or a Python module path where you specify the process itself

.. code-block:: python
def execute(mp, resampling="nearest", **kwargs):
def execute(mp, resampling="nearest"):
# Open elevation model.
with mp.open("dem", resampling=resampling) as src:
# Skip tile if there is no data available.
Expand Down
21 changes: 8 additions & 13 deletions mapchete/_core.py
Expand Up @@ -498,19 +498,14 @@ def _execute(self, process_tile, raise_nodata=False):
try:
with Timer() as t:
# Actually run process.
if len(inspect.getargspec(self.config.process_func).args) == 1:
process_data = self.config.process_func(tile_process)
else:
process_data = self.config.process_func(
tile_process,
**{
k: v for k, v in six.iteritems(params)
if k not in [
"input", "output", "pyramid", "zoom_levels",
"mapchete_file", "init_bounds", "init_zoom_levels"
]
}
)
process_data = self.config.process_func(
tile_process,
# only pass on kwargs which are defined in execute()
**{
k: v for k, v in params.items()
if k in inspect.signature(self.config.process_func).parameters
}
)
except Exception as e:
# Log process time
logger.exception(
Expand Down
10 changes: 2 additions & 8 deletions mapchete/config.py
Expand Up @@ -14,7 +14,6 @@
from copy import deepcopy
import imp
import importlib
import inspect
import logging
import operator
import os
Expand Down Expand Up @@ -51,7 +50,7 @@
_RESERVED_PARAMETERS = [
"baselevels", # enable interpolation from other zoom levels
"bounds", # process bounds
"process", # path to .py file or module path
"process", # path to .py file or module path
"config_dir", # configuration base directory
"process_minzoom", # minimum zoom where process is valid (deprecated)
"process_maxzoom", # maximum zoom where process is valid (deprecated)
Expand Down Expand Up @@ -422,12 +421,7 @@ def process_func(self):
"""instanciating MapcheteProcess is deprecated, """
"""provide execute() function instead""")
if hasattr(process_module, "execute"):
user_execute = process_module.execute
if len(inspect.signature(user_execute).parameters) == 0:
raise ImportError(
"execute() function has to accept at least one argument"
)
return user_execute
return process_module.execute
else:
raise ImportError(
"No execute() function found in %s" % self._raw["process"]
Expand Down
2 changes: 1 addition & 1 deletion test/example_process.py
@@ -1,7 +1,7 @@
"""Example process file."""


def execute(mp, **kwargs):
def execute(mp):
"""User defined process."""
# Reading and writing data works like this:
with mp.open("file1", resampling="bilinear") as raster_file:
Expand Down
8 changes: 0 additions & 8 deletions test/test_errors.py
Expand Up @@ -230,14 +230,6 @@ def test_malformed_process(cleantopo_br, malformed_py):
mapchete.open(config)


def test_execute_params(cleantopo_br, execute_params_error_py):
"""Assert import error is raised."""
config = cleantopo_br.dict
config.update(process=execute_params_error_py)
with pytest.raises(errors.MapcheteProcessImportError):
mapchete.open(config)


def test_syntax_error(mp_tmpdir, cleantopo_br, syntax_error_py):
"""Assert syntax error is raised."""
config = cleantopo_br.dict
Expand Down
7 changes: 7 additions & 0 deletions test/test_mapchete.py
Expand Up @@ -415,3 +415,10 @@ def test_snap_bounds_errors():
mapchete.config.snap_bounds(bounds=(0, 1, ))
with pytest.raises(TypeError):
mapchete.config.snap_bounds(bounds=bounds, pyramid="invalid")


def test_execute_params(cleantopo_br, execute_params_error_py):
"""Assert execute() without parameters passes."""
config = cleantopo_br.dict
config.update(process=execute_params_error_py)
mapchete.open(config)

0 comments on commit e25f0ca

Please sign in to comment.