Skip to content

Commit

Permalink
Move error printing to util submodule
Browse files Browse the repository at this point in the history
  • Loading branch information
MaddyGuthridge committed Feb 4, 2025
1 parent 12478bc commit 2a55665
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 66 deletions.
55 changes: 4 additions & 51 deletions transdoc/__cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@
from typing import IO, Optional
import logging

from colorama import Fore
from transdoc import (
transform_tree,
transform_file,
TransdocTransformer,
get_all_handlers,
)
from transdoc.errors import TransdocTransformationError
from transdoc.util import print_error
from .mutex import Mutex
from .util import pride

Expand Down Expand Up @@ -55,52 +54,6 @@
"""


def error_args(args: tuple) -> str:
msg = []
for arg in args:
if isinstance(arg, tuple):
msg.append(error_args(arg))
else:
msg.append(str(arg))
return " ".join(msg)


def display_transdoc_error(e: TransdocTransformationError):
print(
f"{Fore.CYAN}{e.filename}:{e.pos.start}{Fore.RESET} "
f"{Fore.RED}{type(e).__name__}{Fore.RESET}: "
f"{error_args(e.args)}",
file=sys.stderr,
)


def display_syntax_error(e: SyntaxError):
print(
f"{Fore.CYAN}{e.filename}:{e.lineno}:{e.offset}{Fore.RESET} "
f"{Fore.RED}{type(e).__name__}{Fore.RESET}: "
f"{e.msg}",
file=sys.stderr,
)


def show_error(e: Exception):
"""
Display errors
"""
if isinstance(e, ExceptionGroup):
for sub_error in e.exceptions:
show_error(sub_error)
elif isinstance(e, SyntaxError):
display_syntax_error(e)
elif isinstance(e, TransdocTransformationError):
display_transdoc_error(e)
else:
print(
f"{Fore.RED}{type(e).__name__}{Fore.RESET}: {error_args(e.args)}",
file=sys.stderr,
)


def handle_verbose(verbose: int):
mappings = {
0: "CRITICAL",
Expand Down Expand Up @@ -165,7 +118,7 @@ def cli(
msg = f"Error evaluating rule file '{rule_file}'"
log.exception(msg)
print(msg, file=sys.stderr)
show_error(e)
print_error(e)
return 1
handlers = get_all_handlers()

Expand All @@ -184,7 +137,7 @@ def cli(
out_file,
)
except ExceptionGroup as e:
show_error(e)
print_error(e)
return 1
else:
if output is None and not dryrun:
Expand All @@ -199,6 +152,6 @@ def cli(
force=force,
)
except ExceptionGroup as e:
show_error(e)
print_error(e)
return 1
return 0
2 changes: 2 additions & 0 deletions transdoc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"TransdocRule",
"get_all_handlers",
"TransdocHandler",
"util",
]

from io import StringIO
Expand All @@ -24,6 +25,7 @@
from .handlers import TransdocHandler, PlaintextHandler, get_all_handlers
from .__transform_tree import transform_tree
from .__transform_file import transform_file
from . import util


log = logging.getLogger("transdoc")
Expand Down
2 changes: 1 addition & 1 deletion transdoc/__transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from typing import Any

from transdoc import TransdocRule
from transdoc.__util import indent_by
from transdoc.util import indent_by
from transdoc.source_pos import SourcePos, SourceRange
from transdoc.errors import (
TransdocTransformationError,
Expand Down
14 changes: 0 additions & 14 deletions transdoc/__util.py

This file was deleted.

62 changes: 62 additions & 0 deletions transdoc/util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
"""
# Transdoc / util
Utility functions for Transdoc.
"""

import sys
from colored import Fore, Style
from transdoc.errors import TransdocTransformationError


def indent_by(indent: str, string: str) -> str:
"""
Indent the given string using the given indentation.
"""
return "\n".join(
f"{indent}{line.rstrip()}" for line in string.splitlines()
).lstrip()


def print_error(e: Exception):
"""
Utility function to print errors to `sys.stderr`.
"""

def error_args(args: tuple) -> str:
msg = []
for arg in args:
if isinstance(arg, tuple):
msg.append(error_args(arg))
else:
msg.append(str(arg))
return " ".join(msg)

def display_transdoc_error(e: TransdocTransformationError):
print(
f"{Fore.CYAN}{e.filename}:{e.pos.start}{Style.RESET} "
f"{Fore.RED}{type(e).__name__}{Style.RESET}: "
f"{error_args(e.args)}",
file=sys.stderr,
)

def display_syntax_error(e: SyntaxError):
print(
f"{Fore.CYAN}{e.filename}:{e.lineno}:{e.offset}{Style.RESET} "
f"{Fore.RED}{type(e).__name__}{Style.RESET}: "
f"{e.msg}",
file=sys.stderr,
)

if isinstance(e, ExceptionGroup):
for sub_error in e.exceptions:
print_error(sub_error)
elif isinstance(e, SyntaxError):
display_syntax_error(e)
elif isinstance(e, TransdocTransformationError):
display_transdoc_error(e)
else:
print(
f"{Fore.RED}{type(e).__name__}{Style.RESET}: {error_args(e.args)}",
file=sys.stderr,
)

0 comments on commit 2a55665

Please sign in to comment.