Skip to content

Commit

Permalink
Add checks for file existing properly
Browse files Browse the repository at this point in the history
  • Loading branch information
cthoyt committed Dec 2, 2024
1 parent a8474cd commit 85afa2b
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/bioversions/resources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
VERSIONS_PATH = HERE.joinpath(HERE, "versions.json")

ROOT = HERE.parent.parent.parent.resolve()
PYPROJECT_TOML_PATH = ROOT.joinpath("pyproject.toml")
DOCS = ROOT.joinpath("docs")
EXPORTS_DIRECTORY = DOCS.joinpath("_data")
EXPORT_PATH = EXPORTS_DIRECTORY.joinpath("versions.yml")
Expand All @@ -26,6 +27,11 @@

def load_versions():
"""Load Bioversions data."""
if not VERSIONS_PATH.is_file():
raise RuntimeError(
f"bioversions was not packaged/built/installed properly -"
f"{VERSIONS_PATH.name} was not found inside the distribution"
)
with open(VERSIONS_PATH) as file:
return json.load(file)

Expand All @@ -35,13 +41,20 @@ def _date_converter(o):
return o.strftime("%Y-%m-%d")


def write_versions(versions, indent: int = 2, **kwargs):
def write_versions(versions, indent: int = 2, **kwargs) -> None:
"""Write Bioversions data."""
_ensure_editable()
with open(VERSIONS_PATH, "w") as file:
json.dump(versions, file, indent=indent, default=_date_converter, **kwargs)


def write_export(versions):
def write_export(versions) -> None:
"""Write Bioversions data to the export directory."""
_ensure_editable()
with open(EXPORT_PATH, "w") as file:
yaml.safe_dump(versions, file)


def _ensure_editable() -> None:
if not PYPROJECT_TOML_PATH.is_file():
raise RuntimeError("can not make export when bioversions is not installed in editable mode")

0 comments on commit 85afa2b

Please sign in to comment.