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

8 add cli for create archive functionality #9

Merged
merged 3 commits into from
Aug 15, 2024
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
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ dev = [
"structlog",
]

[project.scripts]
nomad-polymerization = "nomad_polymerization_reactions.cli:cli"

[tool.ruff]
# Exclude a variety of commonly ignored directories.
exclude = [
Expand Down
31 changes: 31 additions & 0 deletions src/nomad_polymerization_reactions/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import click

from nomad_polymerization_reactions.utils import generate_archive_from_json


@click.group(
help="""
This is the entry point to nomad-polymerization-reactions's command line interface
CLI. This CLI provides a set of commands to interact with the utility functions
of the package.
""",
)
def cli():
pass


@cli.command(
help='Create an archive from a JSON file containing polymerization reaction data.',
name='create-archive',
)
@click.argument(
'JSON_FILE_PATH',
required=True,
type=click.Path(exists=True),
)
def _create_archive(json_file_path):
try:
generate_archive_from_json(json_file_path)
click.echo('Archive created successfully.')
except Exception as e:
click.echo(f'Archive creation failed. Error: {e}')
32 changes: 32 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import os

from click.testing import CliRunner
from nomad_polymerization_reactions.cli import cli


def invoke_cli(*args, **kwargs):
return CliRunner().invoke(*args, **kwargs)


def test_cli_help():
result = invoke_cli(cli, ['--help'])
assert result.exit_code == 0
assert 'Usage' in result.output
assert 'This is the entry point to nomad-polymerization-reactions' in result.output


def test_create_archive_help():
result = invoke_cli(cli, ['create-archive', '--help'])
assert result.exit_code == 0
assert 'Usage' in result.output
assert 'Create an archive from a JSON file' in result.output


def test_create_archive_success():
result = invoke_cli(
cli,
['create-archive', 'tests/data/processed_reactions/paper_0_reaction_1.json'],
)
assert result.exit_code == 0
assert 'Archive created successfully.' in result.output
os.remove('paper_0_reaction_1.archive.yaml')