2024-01-19 17:52:51 +00:00
|
|
|
import shutil
|
2024-06-05 07:44:44 +00:00
|
|
|
from dataclasses import dataclass
|
2024-01-16 15:16:12 +00:00
|
|
|
from math import floor
|
2024-01-10 16:58:39 +00:00
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
|
2024-01-16 15:16:12 +00:00
|
|
|
def get_term_filler(name: str) -> int:
|
2024-01-19 17:52:51 +00:00
|
|
|
width, height = shutil.get_terminal_size()
|
2024-01-16 15:16:12 +00:00
|
|
|
|
|
|
|
filler = floor((width - len(name)) / 2)
|
|
|
|
return filler - 1
|
|
|
|
|
|
|
|
|
|
|
|
def text_heading(heading: str) -> str:
|
|
|
|
filler = get_term_filler(heading)
|
|
|
|
return f"{'=' * filler} {heading} {'=' * filler}"
|
|
|
|
|
|
|
|
|
2024-06-05 07:44:44 +00:00
|
|
|
@dataclass
|
2024-01-19 17:52:51 +00:00
|
|
|
class CmdOut:
|
2024-06-05 07:44:44 +00:00
|
|
|
stdout: str
|
|
|
|
stderr: str
|
|
|
|
cwd: Path
|
|
|
|
command: str
|
|
|
|
returncode: int
|
|
|
|
msg: str | None
|
2024-01-10 16:58:39 +00:00
|
|
|
|
2024-06-05 07:44:44 +00:00
|
|
|
def __str__(self) -> str:
|
|
|
|
error_str = f"""
|
2024-01-16 15:16:12 +00:00
|
|
|
{text_heading(heading="Command")}
|
2024-01-10 17:54:34 +00:00
|
|
|
{self.command}
|
2024-01-16 15:16:12 +00:00
|
|
|
{text_heading(heading="Stderr")}
|
2024-01-10 17:54:34 +00:00
|
|
|
{self.stderr}
|
2024-01-16 15:16:12 +00:00
|
|
|
{text_heading(heading="Stdout")}
|
2024-01-10 17:54:34 +00:00
|
|
|
{self.stdout}
|
2024-01-16 15:16:12 +00:00
|
|
|
{text_heading(heading="Metadata")}
|
|
|
|
Message: {self.msg}
|
|
|
|
Working Directory: '{self.cwd}'
|
|
|
|
Return Code: {self.returncode}
|
2024-01-19 17:52:51 +00:00
|
|
|
"""
|
2024-06-05 07:44:44 +00:00
|
|
|
return error_str
|
2024-01-10 17:54:34 +00:00
|
|
|
|
2024-01-10 16:58:39 +00:00
|
|
|
|
2023-07-26 13:50:27 +00:00
|
|
|
class ClanError(Exception):
|
|
|
|
"""Base class for exceptions in this module."""
|
|
|
|
|
2024-06-05 07:44:44 +00:00
|
|
|
description: str | None
|
|
|
|
location: str
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
msg: str | None = None,
|
|
|
|
*,
|
|
|
|
description: str | None = None,
|
|
|
|
location: str | None = None,
|
|
|
|
) -> None:
|
|
|
|
self.description = description
|
|
|
|
self.location = location or "Unknown location"
|
|
|
|
self.msg = msg or ""
|
|
|
|
if self.description:
|
2024-06-15 15:35:50 +00:00
|
|
|
exception_msg = f"{self.location}: \n{self.msg} - {self.description}"
|
2024-06-05 07:44:44 +00:00
|
|
|
else:
|
2024-06-15 15:35:50 +00:00
|
|
|
exception_msg = f"{self.location}: \n{self.msg}"
|
2024-06-05 07:44:44 +00:00
|
|
|
super().__init__(exception_msg)
|
2023-11-21 13:34:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ClanHttpError(ClanError):
|
|
|
|
status_code: int
|
|
|
|
msg: str
|
|
|
|
|
2023-11-30 12:42:15 +00:00
|
|
|
def __init__(self, status_code: int, msg: str) -> None:
|
2023-11-21 13:34:31 +00:00
|
|
|
self.status_code = status_code
|
|
|
|
self.msg = msg
|
|
|
|
super().__init__(msg)
|
2024-01-10 16:58:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ClanCmdError(ClanError):
|
|
|
|
cmd: CmdOut
|
|
|
|
|
|
|
|
def __init__(self, cmd: CmdOut) -> None:
|
|
|
|
self.cmd = cmd
|
|
|
|
super().__init__()
|
2024-01-10 17:54:34 +00:00
|
|
|
|
|
|
|
def __str__(self) -> str:
|
|
|
|
return str(self.cmd)
|
|
|
|
|
|
|
|
def __repr__(self) -> str:
|
|
|
|
return f"ClanCmdError({self.cmd})"
|