Skip to content

Commit

Permalink
Merge pull request #1402 from metno/1390-add-pya-init-command-to-cli-…
Browse files Browse the repository at this point in the history
…to-init-mypyaerocom-path

add pya init command
  • Loading branch information
jgriesfeller authored Nov 12, 2024
2 parents 326ef31 + 3bc258a commit 2e4061b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
2 changes: 1 addition & 1 deletion docs/install.rst
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,4 @@ pyaerocom searches in ``~/MyPyaerocom`` for a file named ``paths.ini`` and uses
one in ``data/`` in the pyaerocom installation directory (or at
<https://github.com/metno/pyaerocom/blob/main-dev/pyaerocom/data/paths.ini>).

To change paths, just copy the default file to ``~/MyPyaerocom`` and change paths to your needs.
To change paths, just run the command ``pya init`` and change paths to your needs with your favorite editor.
25 changes: 24 additions & 1 deletion pyaerocom/scripts/cli.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import json
from enum import Enum
from pathlib import Path
from shutil import copy2
from typing import Optional

import typer

from pyaerocom import __package__, __version__, change_verbosity, const, download_minimal_dataset
from pyaerocom.aeroval import EvalSetup, ExperimentProcessor
from pyaerocom.io.cachehandler_ungridded import list_cache_files
Expand Down Expand Up @@ -106,5 +106,28 @@ def getsampledata(
download_minimal_dataset(extract_dir_override=extract_dir)


@main.command()
def init():
"""init ~/MyPyaerocom directory and copy the default paths.ini there"""

mypyaerocom = Path.home() / "MyPyaerocom"
mypyaerocom.mkdir(exist_ok=True)

# copy default paths.ini if it doesn't exit
ini_in_path = Path(__file__).parents[1].joinpath("data/paths.ini")
ini_out_path = mypyaerocom / "paths.ini"
if not ini_in_path.exists():
print(f"Error: {ini_in_path} does not exist. Something is wrong with your pyaerocom installation!")
if ini_out_path.exists():
print(f"Error: {ini_out_path} already exists. Please delete it by hand if you really want to overwrite that with the default from pyaerocom.")
else:
try:
copy2(ini_in_path, mypyaerocom)
print(f"{ini_out_path} successfully created")
except Exception as e:
print(f"Error: file {ini_out_path} could not created. The error message was "
f"{e}.")


if __name__ == "__main__":
main()
5 changes: 5 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ def test_ppiaccess():
assert result.exit_code == 0


def test_init():
result = runner.invoke(main, ["init"])
assert result.exit_code == 0


@pytest.fixture()
def config_json(monkeypatch, tmp_path: Path, eval_config: dict) -> Path:
def do_not_run(self, model_name=None, obs_name=None, var_list=None, update_interface=True):
Expand Down

0 comments on commit 2e4061b

Please sign in to comment.