Skip to content

Commit

Permalink
Change loading semantics for taxonomy db.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 553612856
  • Loading branch information
sdenton4 authored and copybara-github committed Aug 28, 2023
1 parent 7e5087a commit 08255bc
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
5 changes: 5 additions & 0 deletions chirp/path_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
General utilities to help with handling paths.
"""
import os
from typing import BinaryIO, TextIO

from etils import epath

Expand All @@ -33,3 +34,7 @@ def get_absolute_path(relative_path: os.PathLike[str] | str) -> epath.Path:
"""
file_path = epath.Path(__file__).parent / relative_path
return file_path


def open_file(relative_path: os.PathLike[str] | str, mode) -> TextIO | BinaryIO:
return open(get_absolute_path(relative_path), mode)
11 changes: 5 additions & 6 deletions chirp/taxonomy/namespace_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@

from chirp import path_utils
from chirp.taxonomy import namespace
from etils import epath

TAXONOMY_DATABASE_FILENAME = os.fspath(
path_utils.get_absolute_path("taxonomy/taxonomy_database.json")
)
TAXONOMY_DATABASE_FILENAME = "taxonomy/taxonomy_database.json"


@dataclasses.dataclass
Expand Down Expand Up @@ -136,7 +135,8 @@ def dump_db(taxonomy_database: TaxonomyDatabase, validate: bool = True) -> str:

@functools.cache
def load_db(
path: str = TAXONOMY_DATABASE_FILENAME, validate: bool = True
path: os.PathLike[str] | str = TAXONOMY_DATABASE_FILENAME,
validate: bool = True,
) -> TaxonomyDatabase:
"""Load the taxonomy database.
Expand All @@ -151,8 +151,7 @@ def load_db(
Returns:
The taxonomy database.
"""
fileobj = open(path, "r")
with fileobj as f:
with path_utils.open_file(path, "r") as f:
data = json.load(f)
taxonomy_database = load_taxonomy_database(data)
if validate:
Expand Down

0 comments on commit 08255bc

Please sign in to comment.