Skip to content

Commit 89e4469

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

2 files changed

Lines changed: 46 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: 39 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,38 @@
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 True if the status is OK."""
32+
return self.status == DBStatus.OK
33+
34+
35+
@unique
36+
class DBStatus(str, Enum):
37+
OK = "OK"
38+
WARNING = "WARNING"
39+
ERROR = "ERROR"
2040

2141

2242
def sanity_check(
2343
db_root: str | Path,
2444
*,
2545
revision: str | None = None,
2646
include_warning: bool = False,
27-
) -> DBException | None:
47+
) -> DBException:
2848
"""Perform sanity check and report exception or warning encountered while loading the dataset.
2949
3050
Args:
@@ -44,14 +64,28 @@ def sanity_check(
4464
warnings.filterwarnings("ignore")
4565

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

5285
exception = DBException(
5386
dataset_id=metadata.dataset_id,
5487
version=metadata.version,
88+
status=DBStatus.ERROR,
5589
message=str(e),
5690
)
5791
return exception

0 commit comments

Comments
 (0)