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 1 commit
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
5 changes: 2 additions & 3 deletions src/check_phat_nguoi/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
from check_phat_nguoi.get_data import GetData
from check_phat_nguoi.notify import SendNotifications
from check_phat_nguoi.print_console import PrintConsole

from .utils.setup_logger import setup_logger
from check_phat_nguoi.utils import setup_logger

logger = getLogger(__name__)

Expand All @@ -17,7 +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}")
await PrintConsole().print_console()
PrintConsole().print_console()
await SendNotifications().send()


Expand Down
10 changes: 5 additions & 5 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 All @@ -54,11 +59,6 @@ class Config(BaseModel):
description="Gửi và chờ tất cả request. Đối với API csgt.vn hãy tắt vì gửi request quá nhiều, trang lỗi. Nếu bật, các request sẽ không đảm bảo thứ tự input. Notify hiện không đảm bảo thứ tự input.",
default=True,
)
print_console: bool = Field(
title="In thông tin ra console",
description="In thông tin của biển vi phạm ra console",
default=True,
)
detail_log: bool = Field(
title="Log chi tiết",
description="Log chi tiết",
Expand Down
47 changes: 15 additions & 32 deletions src/check_phat_nguoi/context/plates/models/plate_detail.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,9 @@ 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

resolution_offices: str | None = (
"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
)
violation_str: str = (
f"Lỗi vi phạm thứ {index}:"
+ (f"\nMàu biển: {violation.color}" if violation.color else "")
Expand All @@ -64,29 +55,21 @@ def create_violation_str(violation: ViolationDetail, index: int) -> str:
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
resolution_offices: str | None = (
"\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
)
return violation_str + (resolution_offices if resolution_offices else "")

plate_detail: str = (
f"Biển số: {self.plate}" + f"\nChủ sở hữu: {self.owner}"
if self.owner
else ""
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
27 changes: 8 additions & 19 deletions src/check_phat_nguoi/print_console/print_console.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
from asyncio import gather
from logging import getLogger
from sys import stdout

from check_phat_nguoi.config.config_reader import config
from check_phat_nguoi.context import PlateDetail, plates_context
Expand All @@ -10,22 +8,13 @@

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

@staticmethod
async def async_print(plate_detail: PlateDetail):
stdout.write(str(plate_detail))
stdout.flush()

async def print_console(self):
self.plate_details = plates_context.plates
if self.plate_details == ():
logger.debug("No plate details. Skip printing")
def print_console(self) -> None:
if not config.print_console:
return
if not self.plate_details:
logger.info("No plate details. Skip printing")
return
if config.print_console:
await gather(
*(
PrintConsole.async_print(plate_detail)
for plate_detail in self.plate_details
)
)
for plate_detail in self.plate_details:
print(plate_detail)
Loading
You are viewing a condensed version of this merge commit. You can view the full changes here.