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 10, 2023
1 parent 52a7ea1 commit 97f7d07
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions chirp/taxonomy/namespace_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,13 @@
import dataclasses
import functools
import json
import os
import typing

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 @@ -135,9 +133,7 @@ def dump_db(taxonomy_database: TaxonomyDatabase, validate: bool = True) -> str:


@functools.cache
def load_db(
path: str = TAXONOMY_DATABASE_FILENAME, validate: bool = True
) -> TaxonomyDatabase:
def load_db(path: str | None = None, validate: bool = True) -> TaxonomyDatabase:
"""Load the taxonomy database.
This loads the taxonomy database from the given JSON file. It converts the
Expand All @@ -151,8 +147,11 @@ def load_db(
Returns:
The taxonomy database.
"""
fileobj = open(path, "r")
with fileobj as f:
if path is None:
path = path_utils.get_absolute_path(TAXONOMY_DATABASE_FILENAME)
else:
path = epath.Path(path)
with path.open("r") as f:
data = json.load(f)
taxonomy_database = load_taxonomy_database(data)
if validate:
Expand Down

0 comments on commit 97f7d07

Please sign in to comment.