Skip to content

Commit f92389c

Browse files
committed
feat: add DB status
Signed-off-by: ktro2828 <kotaro.uetake@tier4.jp>
1 parent 298529e commit f92389c

2 files changed

Lines changed: 45 additions & 15 deletions

File tree

t4_devkit/cli/sanity.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,10 @@ def _run_sanity_check(
2424
revision: str | None = None,
2525
include_warning: bool = False,
2626
) -> list[DBException]:
27-
exceptions: list[DBException] = []
28-
29-
for db_root in tqdm(Path(db_parent).glob("*"), desc=">>>Sanity checking..."):
30-
result = sanity_check(db_root, revision=revision, include_warning=include_warning)
31-
if result:
32-
exceptions.append(result)
33-
return exceptions
27+
return [
28+
sanity_check(db_root, revision=revision, include_warning=include_warning)
29+
for db_root in tqdm(Path(db_parent).glob("*"), desc=">>>Sanity checking...")
30+
]
3431

3532

3633
@cli.command()
@@ -53,10 +50,10 @@ def main(
5350
) -> None:
5451
exceptions = _run_sanity_check(db_parent, revision=revision, include_warning=include_warning)
5552

56-
if not exceptions:
53+
if all(e.is_ok() for e in exceptions):
5754
print("✅ No exceptions occurred!!")
5855
else:
5956
print("⚠️ Encountered some exceptions!!")
60-
headers = ["DatasetID", "Version", "Message"]
61-
table = [[e.dataset_id, e.version, e.message] for e in exceptions]
57+
headers = ["DatasetID", "Version", "status", "Message"]
58+
table = [[e.dataset_id, e.version, e.status, e.message] for e in exceptions]
6259
print(tabulate(table, headers=headers, tablefmt="pretty"))

t4_devkit/common/sanity.py

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import warnings
4+
from enum import Enum, unique
45
from pathlib import Path
56

67
from attrs import define
@@ -12,19 +13,37 @@
1213

1314
@define
1415
class DBException:
15-
"""A dataclass to store error message of the corresponding dataset."""
16+
"""A dataclass to store error message of the corresponding dataset.
17+
18+
Attributes:
19+
dataset_id (str): Dataset ID.
20+
version (str | None): Dataset version.
21+
status (DBStatus): Status of the dataset.
22+
message (str): Error or warning message.
23+
"""
1624

1725
dataset_id: str
1826
version: str | None
19-
message: str
27+
status: DBStatus
28+
message: str | None = None
29+
30+
def is_ok(self) -> bool:
31+
return self.status == DBStatus.OK
32+
33+
34+
@unique
35+
class DBStatus(str, Enum):
36+
OK = "OK"
37+
WARNING = "WARNING"
38+
ERROR = "ERROR"
2039

2140

2241
def sanity_check(
2342
db_root: str | Path,
2443
*,
2544
revision: str | None = None,
2645
include_warning: bool = False,
27-
) -> DBException | None:
46+
) -> DBException:
2847
"""Perform sanity check and report exception or warning encountered while loading the dataset.
2948
3049
Args:
@@ -44,14 +63,28 @@ def sanity_check(
4463
warnings.filterwarnings("ignore")
4564

4665
try:
47-
_ = Tier4(data_root=db_root, revision=revision, verbose=False)
48-
exception = None
66+
t4 = Tier4(data_root=db_root, revision=revision, verbose=False)
67+
exception = DBException(
68+
dataset_id=t4.dataset_id,
69+
version=t4.version,
70+
status=DBStatus.OK,
71+
)
72+
except Warning as w:
73+
metadata = load_metadata(db_root)
74+
75+
exception = DBException(
76+
dataset_id=metadata.dataset_id,
77+
version=metadata.version,
78+
status=DBStatus.WARNING,
79+
message=str(w),
80+
)
4981
except Exception as e:
5082
metadata = load_metadata(db_root)
5183

5284
exception = DBException(
5385
dataset_id=metadata.dataset_id,
5486
version=metadata.version,
87+
status=DBStatus.ERROR,
5588
message=str(e),
5689
)
5790
return exception

0 commit comments

Comments
 (0)