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

Add Initial CLI Setup for xDEM #626

Closed
wants to merge 4 commits into from
Closed
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
1 change: 1 addition & 0 deletions dev-environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ dependencies:
- scikit-image=0.*
- scikit-gstat>=1.0.18,<1.1
- geoutils=0.1.10
- argcomplete

# Development-specific, to mirror manually in setup.cfg [options.extras_require].
- pip
Expand Down
15 changes: 15 additions & 0 deletions doc/source/quick_start.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,21 @@ import os
os.remove("dh_error.tif")
```

## Command Line Interface (CLI)

The xDEM package can be executed from the command line using the `xdem` command.

### Usage

```bash
xdem path_ref path_sec [options]
```

### Options

- `--loglevel`: Set the logging level (default: INFO).
- `-v`, `--version`: Show the version of the xDEM package.

(quick-gallery)=
## More examples

Expand Down
1 change: 1 addition & 0 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ dependencies:
- scikit-gstat>=1.0.18,<1.1
- geoutils=0.1.10
- pip
- argcomplete

# To run CI against latest GeoUtils
# - pip:
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ scikit-image==0.*
scikit-gstat>=1.0.18,<1.1
geoutils==0.1.10
pip
argcomplete
4 changes: 4 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,7 @@ dev =
%(doc)s
all =
%(dev)s

[options.entry_points]
console_scripts =
xdem = xdem_cli:main
14 changes: 12 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
"""This file now only serves for backward-compatibility for routines explicitly calling python setup.py"""
from setuptools import setup
from setuptools import find_packages, setup

setup()
setup(
name="xdem",
use_scm_version=True, # Enable versioning with setuptools_scm
setup_requires=["setuptools_scm"], # Ensure setuptools_scm is used to determine the version
packages=find_packages(),
entry_points={
"console_scripts": [
"xdem = xdem.xdem_cli:main",
],
},
)
30 changes: 30 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""Function to test the CLI"""

import subprocess

import xdem


class TestCLI:
# Define paths to the DEM files using xDEM examples
ref_dem_path = xdem.examples.get_path("longyearbyen_ref_dem")
tba_dem_path = xdem.examples.get_path("longyearbyen_tba_dem")

def test_xdem_cli(self) -> None:
try:
# Run the xDEM CLI command with the reference and secondary DEM files
result = subprocess.run(
["xdem", self.ref_dem_path, self.tba_dem_path],
capture_output=True,
text=True,
)
assert "hello world" in result.stdout
assert result.returncode == 0

except FileNotFoundError as e:
# In case 'xdem' is not found
raise AssertionError(f"CLI command 'xdem' not found : {e}")

except Exception as e:
# Any other errors during subprocess run
raise AssertionError(f"An error occurred while running the CLI: {e}")
7 changes: 7 additions & 0 deletions xdem/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,10 @@
"virtualenv) and then install it in-place by running: "
"pip install -e ."
)


def run(reference_dem: str, dem_to_be_aligned: str, verbose: str) -> None:
"""
Function to compare DEMs
"""
print("hello world")
78 changes: 78 additions & 0 deletions xdem/xdem_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# Copyright (c) 2024 xDEM developers
#
# This file is part of xDEM project:
# https://github.com/glaciohack/xdem
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import argparse
from argparse import ArgumentParser

import argcomplete

import xdem


def get_parser() -> ArgumentParser:
"""
ArgumentParser for xdem

:return: parser
"""
parser = argparse.ArgumentParser(
description="Compare Digital Elevation Models",
fromfile_prefix_chars="@",
)

parser.add_argument(
"reference_dem",
help="path to a reference dem",
)

parser.add_argument(
"dem_to_be_aligned",
help="path to a second dem",
)

parser.add_argument(
"--loglevel",
default="INFO",
choices=("DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"),
help="Logger level (default: INFO. Should be one of (DEBUG, INFO, WARNING, ERROR, CRITICAL)",
)

parser.add_argument(
"--version",
"-v",
action="version",
version=f"%(prog)s {xdem.__version__}",
)

return parser


def main() -> None:
"""
Call xDEM's main
"""
parser = get_parser()
argcomplete.autocomplete(parser)
args = parser.parse_args()
try:
xdem.run(args.reference_dem, args.dem_to_be_aligned, args.loglevel)
except Exception as e:
print(f"Error: {e}")


if __name__ == "__main__":
main()
Loading