Skip to content
This repository has been archived by the owner on Jan 18, 2025. It is now read-only.

feat: add print console #37

Merged
merged 3 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config.sample.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,6 @@
"asynchronous": true,
"detail": true,
"detail_log": false,
"print_console": true,
"log_level": "INFO"
}
2 changes: 2 additions & 0 deletions src/check_phat_nguoi/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from check_phat_nguoi.context import plates_context
from check_phat_nguoi.get_data import GetData
from check_phat_nguoi.notify import SendNotifications
from check_phat_nguoi.print_console import PrintConsole
from check_phat_nguoi.utils import setup_logger

logger = getLogger(__name__)
Expand All @@ -15,6 +16,7 @@ async def async_main() -> None:
logger.debug(f"Config read: {config}")
await GetData().get_data()
logger.debug(f"Data got: {plates_context.plates}")
PrintConsole().print_console()
await SendNotifications().send()


Expand Down
5 changes: 5 additions & 0 deletions src/check_phat_nguoi/config/models/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ class Config(BaseModel):
default=(ApiEnum.checkphatnguoi_vn),
min_length=1,
)
print_console: bool = Field(
title="In thông tin ra console",
description="In thông tin của các biển ra console",
default=True,
)
pending_fines_only: bool = Field(
title="Lọc chưa nộp phạt",
description="Chỉ lọc các thông tin vi phạm chưa nộp phạt",
Expand Down
79 changes: 37 additions & 42 deletions src/check_phat_nguoi/context/plates/models/plate_detail.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,54 +27,49 @@ def __eq__(self, other: Any):
return self.plate == other.plate
return False

def __str__(self):
# TODO: Handle show details later when main updates that option
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Handle show details later when main updates that option

def __str__(self) -> str:
def create_violation_str(violation: ViolationDetail, index: int) -> str:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

create str method in violation model instead in this big class

violation_str: str = (
f"Lỗi vi phạm thứ {index}:"
+ (f"\nMàu biển: {violation.color}" if violation.color else "")
+ (f"\nThời điểm vi phạm: {violation.date}" if violation.date else "")
+ (
f"\nVị trí vi phạm: {violation.location}"
if violation.location
else ""
)
+ (
f"\nHành vi vi phạm: {violation.violation}"
if violation.violation
else ""
)
+ (
f"\nTrạng thái: {'Chưa xử phạt' if not violation.status else 'Đã xử phạt'}"
if violation.status is not None
else ""
)
+ (
f"\nĐơn vị phát hiện vi phạm: {violation.enforcement_unit}"
if violation.enforcement_unit
else ""
)
)
resolution_offices: str | None = (
f"""
Nơi giải quyết vụ việc:
{
"\n".join(
resolution_office_detail
for resolution_office_detail in violation.resolution_offices_details
)
}
"""
"\n"
+ "Nơi giải quyết vụ việc:"
+ "\n"
+ "\n".join(
resolution_office_detail.strip()
for resolution_office_detail in violation.resolution_offices_details
)
if violation.resolution_offices_details
else None
)
# TODO: Keep going on
violation_str: str = (
f"Lỗi vi phạm thứ {index}:" + f"\nMàu biển: {violation.color}"
if violation.color
else ""
)
# violation_str = "\n".join(
# line
# for line in f"""
# Lỗi vi phạm thứ {index}:
# Màu biển: {violation.color if violation.color else " "}
# Thời điểm vi phạm: {violation.date if violation.date else " "}
# Vị trí vi phạm: {violation.location if violation.location else " "}
# Hành vi vi phạm: {violation.violation if violation.violation else " "}
# Trạng thái: {"Đã xử phạt" if violation.status else ("Chưa xử phạt" if not violation.status else " ")}
# Đơn vị phát hiện vi phạm: {violation.enforcement_unit if violation.enforcement_unit else " "}
# """.splitlines()
# if line.strip()
# )
return (
"\n".join([violation_str, resolution_offices])
if resolution_offices
else violation_str
)
return violation_str + (resolution_offices if resolution_offices else "")

# TODO: Ye going on hehehe
plate_detail: str = "\n".join(
line
for line in f"""
Biển số: {self.plate}
Chủ sở hữu: {self.owner if self.owner else " "}
""".splitlines()
if line.strip()
plate_detail: str = f"Biển số: {self.plate}" + (
f"\nChủ sở hữu: {self.owner}" if self.owner else ""
)

if self.violations:
Expand Down
3 changes: 3 additions & 0 deletions src/check_phat_nguoi/print_console/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .print_console import PrintConsole

__all__ = ["PrintConsole"]
20 changes: 20 additions & 0 deletions src/check_phat_nguoi/print_console/print_console.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from logging import getLogger

from check_phat_nguoi.config.config_reader import config
from check_phat_nguoi.context import PlateDetail, plates_context

logger = getLogger(__name__)


class PrintConsole:
def __init__(self):
self.plate_details: tuple[PlateDetail, ...] = plates_context.plates

def print_console(self) -> None:
if not config.print_console:
return
if not self.plate_details:
logger.info("No plate details. Skip printing")
return
for plate_detail in self.plate_details:
print(plate_detail)
Loading