Skip to content

Commit

Permalink
Change the command line interface to move common option before the su…
Browse files Browse the repository at this point in the history
…b-command.
  • Loading branch information
alexamici committed Mar 29, 2016
1 parent 75437c2 commit cdcf88b
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 44 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ subsequent accesses to the same and nearby areas are much faster.

It is possible to pre-populate the cache for an area, for example to seed the SRTM3 90m DEM of Italy execute::

$ eio seed --product SRTM3 --bounds 6.6 36.6 18.6 47.1
$ eio --product SRTM3 seed --bounds 6.6 36.6 18.6 47.1


Python API
Expand Down
31 changes: 15 additions & 16 deletions docs/usersguide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ The following command runs some basic checks and reports common issues::

GNU make, curl and unzip come pre-installed with most operating systems.
The best way to install GDAL command line tools varies across operating systems
and distributions, please refer to the `GDAL install documentation`_.
and distributions, please refer to the
`GDAL install documentation <https://trac.osgeo.org/gdal/wiki/DownloadingGdalBinaries>`_.


Command line usage
Expand All @@ -47,11 +48,17 @@ The ``eio`` command as the following sub-commands and options::
Usage: eio [OPTIONS] COMMAND [ARGS]...

Options:
--help Show this message and exit.
--product [SRTM1|SRTM3] DEM product choice (default: 'SRTM1').
--cache_dir DIRECTORY Root of the DEM cache folder (default:
'/Users/amici/Library/Caches/elevation').
--make_flags TEXT Options to be passed to make (default: '-s -k').
--help Show this message and exit.

Commands:
clean Clean up the cache from temporary files.
clip Clip the DEM to given bounds.
distclean Clean up the cache from temporary files.
info
seed Seed the DEM to given bounds.
selfcheck Audits your installation for common issues.

Expand All @@ -61,24 +68,16 @@ The ``seed`` sub-command::
Usage: eio seed [OPTIONS]

Options:
--product [SRTM1|SRTM3] DEM product choice (default: 'SRTM1').
--cache_dir DIRECTORY Root of the DEM cache folder (default:
'$HOME/Library/Caches/elevation').
--make_flags TEXT Options to be passed to make (default: '-s -k').
--bounds FLOAT... Output bounds: left bottom right top.
--help Show this message and exit.
--bounds FLOAT... Output bounds: left bottom right top.
--help Show this message and exit.

The ``clip`` sub-command::

$ eio clip --help
Usage: eio clip [OPTIONS]

Options:
--product [SRTM1|SRTM3] DEM product choice (default: 'SRTM1').
--cache_dir DIRECTORY Root of the DEM cache folder (default:
'$HOME/Library/Caches/elevation').
--make_flags TEXT Options to be passed to make (default: '-s -k').
-o, --output PATH Path to output file. Existing files will be
overwritten.
--bounds FLOAT... Output bounds: left bottom right top.
--help Show this message and exit.
-o, --output PATH Path to output file. Existing files will be overwritten.
--bounds FLOAT... Output bounds: left bottom right top.
--help Show this message and exit.

48 changes: 30 additions & 18 deletions elevation/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,14 @@


@click.group()
def eio():
@click.option('--product', default=elevation.DEFAULT_PRODUCT, type=click.Choice(elevation.PRODUCTS),
help='DEM product choice (default: %r).' % elevation.DEFAULT_PRODUCT)
@click.option('--cache_dir', default=elevation.CACHE_DIR,
type=click.Path(resolve_path=True, file_okay=False),
help='Root of the DEM cache folder (default: %r).' % elevation.CACHE_DIR)
@click.option('--make_flags', default='-k',
help='Options to be passed to make (default: %r).' % elevation.MAKE_FLAGS)
def eio(**kwargs):
pass


Expand All @@ -37,43 +44,48 @@ def selfcheck():
print(util.selfcheck(tools=elevation.TOOLS))


product_options = util.composed(
click.option('--product', default=elevation.DEFAULT_PRODUCT, type=click.Choice(elevation.PRODUCTS),
help='DEM product choice (default: %r).' % elevation.DEFAULT_PRODUCT),
click.option('--cache_dir', default=elevation.CACHE_DIR,
type=click.Path(resolve_path=True, file_okay=False),
help='Root of the DEM cache folder (default: %r).' % elevation.CACHE_DIR),
click.option('--make_flags', default='-k',
help='Options to be passed to make (default: %r).' % elevation.MAKE_FLAGS),
)
@eio.command(short_help='')
@click.pass_context
def info(ctx, **kwargs):
if ctx.parent and ctx.parent.params:
kwargs.update(ctx.parent.params)
elevation.info(**kwargs)


@eio.command(short_help='Seed the DEM to given bounds.')
@product_options
@click.option('--bounds', nargs=4, type=float, default=None,
help='Output bounds: left bottom right top.')
def seed(**kwargs):
@click.pass_context
def seed(ctx, **kwargs):
if ctx.parent and ctx.parent.params:
kwargs.update(ctx.parent.params)
elevation.seed(**kwargs)


@eio.command(short_help='Clip the DEM to given bounds.')
@product_options
@click.option('-o', '--output', default=elevation.DEFAULT_OUTPUT,
type=click.Path(resolve_path=True, dir_okay=False),
help="Path to output file. Existing files will be overwritten.")
@click.option('--bounds', nargs=4, type=float, default=None,
help='Output bounds: left bottom right top.')
def clip(**kwargs):
@click.pass_context
def clip(ctx, **kwargs):
if ctx.parent and ctx.parent.params:
kwargs.update(ctx.parent.params)
elevation.clip(**kwargs)


@eio.command(short_help='Clean up the cache from temporary files.')
@product_options
def clean(**kwargs):
@click.pass_context
def clean(ctx, **kwargs):
if ctx.parent and ctx.parent.params:
kwargs.update(ctx.parent.params)
elevation.clean(**kwargs)


@eio.command(short_help='Clean up the cache from temporary files.')
@product_options
def distclean(**kwargs):
@click.pass_context
def distclean(ctx, **kwargs):
if ctx.parent and ctx.parent.params:
kwargs.update(ctx.parent.params)
elevation.distclean(**kwargs)
7 changes: 6 additions & 1 deletion elevation/datasource.mk
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,19 @@ download: $(ENSURE_TILE_PATHS)
clip: $(PRODUCT).vrt
gdal_translate -q -co TILED=YES -co COMPRESS=DEFLATE -co ZLEVEL=9 -co PREDICTOR=2 -projwin $(PROJWIN) $(PRODUCT).vrt $(OUTPUT)

info:
@echo 'Product folder: $(shell pwd)'
@echo 'Tiles count: $(shell ls cache/*.tif | wc -l)'
@echo 'Cache size: $(shell du -sh .)'

clean:
$(RM) spool/*

distclean: clean
$(RM) cache/* $(PRODUCT).vrt Makefile

.DELETE_ON_ERROR:
.PHONY: all download clip clean distclean
.PHONY: all info download clip clean distclean

#
# override most of make default behaviour
Expand Down
7 changes: 6 additions & 1 deletion elevation/datasource.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

# declare public all API functions and constants
__all__ = [
'seed', 'clip', 'clean', 'distclean',
'info', 'seed', 'clip', 'clean', 'distclean',
'CACHE_DIR', 'DEFAULT_PRODUCT', 'PRODUCTS', 'DEFAULT_OUTPUT', 'MAKE_FLAGS', 'TOOLS',
]

Expand Down Expand Up @@ -138,6 +138,11 @@ def clip(cache_dir=CACHE_DIR, product=DEFAULT_PRODUCT, bounds=None, output=DEFAU
do_clip(datasource_root, bounds, output, **kwargs)


def info(cache_dir=CACHE_DIR, product=DEFAULT_PRODUCT, **kwargs):
datasource_root, _ = ensure_setup(cache_dir, product)
util.check_call_make(datasource_root, targets=['info'])


def clean(cache_dir=CACHE_DIR, product=DEFAULT_PRODUCT, **kwargs):
datasource_root, _ = ensure_setup(cache_dir, product)
util.check_call_make(datasource_root, targets=['clean'])
Expand Down
14 changes: 7 additions & 7 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,37 +24,37 @@ def test_eio_selfcheck(mocker):
def test_eio_seed(mocker, tmpdir):
root = tmpdir.join('root')
runner = click.testing.CliRunner()
options = '--cache_dir %s --bounds 12.5 42 12.5 42' % str(root)
options = '--cache_dir %s seed --bounds 12.5 42 12.5 42' % str(root)
with mocker.patch('subprocess.check_call'):
result = runner.invoke(cli.seed, options.split())
result = runner.invoke(cli.eio, options.split())
assert not result.exception
assert subprocess.check_call.call_count == 2


def test_eio_clip(mocker, tmpdir):
root = tmpdir.join('root')
runner = click.testing.CliRunner()
options = '--cache_dir %s --bounds 12.5 42 12.5 42' % str(root)
options = '--cache_dir %s clip --bounds 12.5 42 12.5 42' % str(root)
with mocker.patch('subprocess.check_call'):
result = runner.invoke(cli.clip, options.split())
result = runner.invoke(cli.eio, options.split())
assert not result.exception
assert subprocess.check_call.call_count == 3


def test_eio_clean(mocker, tmpdir):
root = tmpdir.join('root')
runner = click.testing.CliRunner()
options = '--cache_dir %s' % str(root)
options = '--cache_dir %s clean' % str(root)
with mocker.patch('subprocess.check_call'):
result = runner.invoke(cli.clean, options.split())
result = runner.invoke(cli.eio, options.split())
assert not result.exception
assert subprocess.check_call.call_count == 1


def test_eio(mocker, tmpdir):
root = tmpdir.join('root')
runner = click.testing.CliRunner()
options = 'seed --cache_dir %s --bounds 12.5 42 12.5 42' % str(root)
options = '--cache_dir %s seed --bounds 12.5 42 12.5 42' % str(root)
with mocker.patch('subprocess.check_call'):
result = runner.invoke(cli.eio, options.split())
assert not result.exception
Expand Down

0 comments on commit cdcf88b

Please sign in to comment.