Skip to content

Commit

Permalink
app_interface cli "inputs list" does not fail anymore if resources ha…
Browse files Browse the repository at this point in the history
…ve no "name" field value
  • Loading branch information
leoschwarz committed Oct 9, 2024
1 parent c8f4527 commit e92a577
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
3 changes: 2 additions & 1 deletion docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ Versioning currently follows `X.Y.Z` where

### Added

- (experimental) EntityLookupCache that allows to cache entity lookups in a script to avoid redundant requests.
- Relationship: `ExternalJob.executable`
- (experimental) EntityLookupCache that allows to cache entity lookups in a script to avoid redundant requests.

### Fixed

- `Entity.find_all` returns no values when an empty list is passed as an argument.
- (experimental) app_interface cli "inputs list" does not fail anymore if resources have no "name" field value.

## \[1.13.8\] - 2024-10-03

Expand Down
3 changes: 2 additions & 1 deletion src/bfabric/experimental/app_interface/cli/inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,5 @@ def list(
) -> None:
"""Lists the input files for an app."""
setup_script_logging()
print_input_files_list(inputs_yaml=inputs_yaml, target_folder=target_folder)
client = Bfabric.from_config()
print_input_files_list(inputs_yaml=inputs_yaml, target_folder=target_folder, client=client)
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
from __future__ import annotations

from pathlib import Path
from typing import Annotated, Literal, Union
from typing import Annotated, Literal, Union, TYPE_CHECKING

import yaml
from pydantic import BaseModel, ConfigDict, Field, Discriminator

from bfabric.entities import Resource

# ":" are not allowed, as well as absolute paths (starting with "/")
RelativeFilePath = Annotated[str, Field(pattern=r"^[^/][^:]*$")]

if TYPE_CHECKING:
from bfabric.bfabric import Bfabric


class ResourceSpec(BaseModel):
model_config = ConfigDict(extra="forbid")
Expand All @@ -18,6 +23,13 @@ class ResourceSpec(BaseModel):
filename: RelativeFilePath | None = None
check_checksum: bool = True

def resolve_filename(self, client: Bfabric) -> str:
if self.filename:
return self.filename
else:
resource = Resource.find(id=self.id, client=client)
return resource["name"]


class DatasetSpec(BaseModel):
model_config = ConfigDict(extra="forbid")
Expand All @@ -26,9 +38,13 @@ class DatasetSpec(BaseModel):
id: int
filename: RelativeFilePath
separator: Literal[",", "\t"] = ","

# has_header: bool
# invalid_characters: str = ""

def resolve_filename(self, client: Bfabric) -> str:
return self.filename


InputSpecType = Annotated[Union[ResourceSpec, DatasetSpec], Discriminator("type")]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ def prepare_dataset(self, spec: DatasetSpec) -> None:
target_path.write_text(tmp_file.read().decode())

def clean_resource(self, spec: ResourceSpec) -> None:
name = spec.filename if spec.filename else Resource.find(id=spec.id, client=self._client)["name"]
path = self._working_dir / name
filename = spec.resolve_filename(client=self._client)
path = self._working_dir / filename
if path.exists():
logger.info(f"Removing {path}")
path.unlink()
Expand Down Expand Up @@ -124,6 +124,7 @@ def prepare_folder(
def print_input_files_list(
inputs_yaml: Path,
target_folder: Path,
client: Bfabric,
) -> None:
"""Prints a list of inputs and whether they exist locally."""
specs_list = InputsSpec.read_yaml(inputs_yaml)
Expand All @@ -133,7 +134,8 @@ def print_input_files_list(
Column("Exists Locally"),
)
for spec in specs_list:
path = target_folder / spec.filename if target_folder else Path(spec.filename)
filename = spec.resolve_filename(client=client)
path = target_folder / filename if target_folder else Path(filename)
table.add_row(
str(path),
"Resource" if isinstance(spec, ResourceSpec) else "Dataset",
Expand Down

0 comments on commit e92a577

Please sign in to comment.