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 beam command line interface #4

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
51 changes: 47 additions & 4 deletions beam/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

"""

import argparse
import json
import multiprocessing.managers
import multiprocessing.shared_memory
Expand All @@ -25,6 +26,7 @@
import urllib.request
import venv
from enum import Enum
from importlib import metadata
from pathlib import Path
from types import ModuleType
from typing import Any, Callable, Optional
Expand All @@ -46,6 +48,43 @@ class SupportedInstallationMethod(Enum):
CONDA = "conda"


def run_beam():
argparser = argparse.ArgumentParser()
argparser.add_argument(
"--version",
action="version",
version=f'%(prog)s version {metadata.version("datatractor_beam")}',
)

argparser.add_argument(
"filetype",
help="FileType.ID of the input file",
default=None,
)

argparser.add_argument(
"infile",
help="Path of the input file",
default=None,
)

argparser.add_argument(
"--outfile",
"-o",
help="Optional path of the output file",
default=None,
)

args = argparser.parse_args()

extract(
input_path=args.infile,
input_type=args.filetype,
output_path=args.outfile,
preferred_mode=SupportedExecutionMethod.CLI,
)


def extract(
input_path: Path | str,
input_type: str,
Expand All @@ -61,15 +100,16 @@ def extract(

Parameters:
input_path: The path or URL of the file to parse.
input_type: The ID of the `FileType` in the registry.
input_type: The ID of the ``FileType`` in the registry.
output_path: The path to write the output to.
If not provided, the output will be requested to be written
to a file with the same name as the input file, but with a .json extension.
to a file with the same name as the input file, but with an extension as
defined using the ``output_type``. Defaults to ``{input_path}.out``.
output_type: A string specifying the desired output type.
preferred_mode: The preferred execution method.
If the extractor supports both Python and CLI, this will be used to determine
which to use. If the extractor only supports one method, this will be ignored.
Accepts the `SupportedExecutionMethod` values of "cli" or "python".
Accepts the ``SupportedExecutionMethod`` values of "cli" or "python".
install: Whether to install the extractor package before running it. Defaults to True.
extractor_definition: A dictionary containing the extractor definition to use instead
of a registry lookup.
Expand Down Expand Up @@ -265,7 +305,10 @@ def execute(
)

if output_path is None:
output_path = input_path.with_suffix(".json")
suffix = ".out" if output_type is None else f".{output_type}"
output_path = input_path.with_suffix(suffix)

print(f"{output_type=}")

command = self.apply_template_args(
command,
Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ dev = [
[project.urls]
repository = "https://github.com/datatractor/beam"

[project.scripts]
beam = "beam:run_beam"

[tool.ruff]
extend-exclude = [
"providers",
Expand Down
11 changes: 11 additions & 0 deletions tests/test_mpr.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import subprocess
import urllib.request
from pathlib import Path

Expand Down Expand Up @@ -146,3 +147,13 @@ def test_extractorplan_python_method():
function, args, kwargs = ExtractorPlan._prepare_python(
'extract(filename="example.txt", type={"test": "example", "dictionary": "example"})'
)


def test_biologic_beam(tmp_path, test_mprs):
for ind, test_mpr in enumerate(test_mprs):
input_path = tmp_path / test_mpr
output_path = tmp_path / test_mpr.name.replace(".mpr", ".nc")
task = ["beam", "biologic-mpr", str(input_path), "--outfile", str(output_path)]
subprocess.run(task)
assert output_path.exists()
break
Loading