Skip to content

Commit 233ecf2

Browse files
committed
fix(#167): Separate "messages" from "logging"
1 parent ea5e3e9 commit 233ecf2

File tree

12 files changed

+58
-34
lines changed

12 files changed

+58
-34
lines changed

src/twyn/base/exceptions.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
1-
class TwynError(Exception):
1+
import logging
2+
from typing import IO, Any, Optional
3+
4+
import click
5+
6+
logger = logging.getLogger("twyn.errors")
7+
8+
9+
class TwynError(click.ClickException):
210
message = ""
311

412
def __init__(self, message: str = "") -> None:
513
super().__init__(message or self.message)
14+
15+
def show(self, file: Optional[IO[Any]] = None) -> None:
16+
logger.debug(self.format_message(), exc_info=True)
17+
logger.error(self.format_message(), exc_info=False)

src/twyn/cli.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,9 @@ def run(
6565
vv: bool,
6666
) -> int:
6767
if v and vv:
68-
raise ValueError("Only one verbosity level is allowed. Choose either -v or -vv.")
68+
raise click.UsageError(
69+
"Only one verbosity level is allowed. Choose either -v or -vv.", ctx=click.get_current_context()
70+
)
6971

7072
if v:
7173
verbosity = AvailableLoggingLevels.info
@@ -75,10 +77,12 @@ def run(
7577
verbosity = AvailableLoggingLevels.none
7678

7779
if dependency and dependency_file:
78-
raise ValueError("Only one of --dependency or --dependency-file can be set at a time.")
80+
raise click.UsageError(
81+
"Only one of --dependency or --dependency-file can be set at a time.", ctx=click.get_current_context()
82+
)
7983

8084
if dependency_file and not any(dependency_file.endswith(key) for key in DEPENDENCY_FILE_MAPPING):
81-
raise ValueError("Dependency file name not supported.")
85+
raise click.UsageError("Dependency file name not supported.", ctx=click.get_current_context())
8286

8387
return int(
8488
check_dependencies(

src/twyn/config/config_handler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
TOMLError,
2020
)
2121

22-
logger = logging.getLogger()
22+
logger = logging.getLogger("twyn")
2323

2424

2525
@dataclass(frozen=True)

src/twyn/dependency_parser/abstract_parser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from twyn.file_handler.file_handler import FileHandlerPathlib
77

8-
logger = logging.getLogger()
8+
logger = logging.getLogger("twyn")
99

1010

1111
class AbstractParser(ABC):

src/twyn/dependency_parser/dependency_selector.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
NoMatchingParserError,
99
)
1010

11-
logger = logging.getLogger()
11+
logger = logging.getLogger("twyn")
1212

1313

1414
class DependencySelector:

src/twyn/file_handler/file_handler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from twyn.base.exceptions import TwynError
77
from twyn.file_handler.exceptions import PathIsNotFileError, PathNotFoundError
88

9-
logger = logging.getLogger()
9+
logger = logging.getLogger("twyn")
1010

1111

1212
class BaseFileHandler(Protocol):

src/twyn/main.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import re
33
from typing import Optional
44

5+
import click
56
from rich.logging import RichHandler
67
from rich.progress import track
78

@@ -24,7 +25,7 @@
2425
datefmt="[%X]",
2526
handlers=[RichHandler(rich_tracebacks=True, show_path=False)],
2627
)
27-
logger = logging.getLogger()
28+
logger = logging.getLogger("twyn")
2829

2930

3031
def check_dependencies(
@@ -61,11 +62,15 @@ def check_dependencies(
6162
errors.append(typosquat_results)
6263

6364
for possible_typosquats in errors:
64-
logger.error(
65-
f"Possible typosquat detected: `{possible_typosquats.candidate_dependency}`, "
66-
f"did you mean any of [{', '.join(possible_typosquats.similar_dependencies)}]?"
65+
click.echo(
66+
click.style("Possible typosquat detected: ", fg="red") + f"`{possible_typosquats.candidate_dependency}`, "
67+
f"did you mean any of [{', '.join(possible_typosquats.similar_dependencies)}]?",
68+
color=True,
6769
)
6870

71+
if not errors:
72+
click.echo(click.style("No typosquats detected", fg="green"), color=True)
73+
6974
return bool(errors)
7075

7176

src/twyn/similarity/algorithm.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from twyn.similarity.exceptions import DistanceAlgorithmError, ThresholdError
99

10-
logger = logging.getLogger()
10+
logger = logging.getLogger("twyn")
1111

1212

1313
class SimilarityThreshold:
@@ -30,9 +30,7 @@ def from_name(cls, name: str) -> SimilarityThreshold:
3030
logger.debug(f"max length of {cls.MAX_FOR_SHORT_WORDS} selected for {name}")
3131
return cls(max=cls.MAX_FOR_SHORT_WORDS)
3232
logger.debug(f"max length of {cls.MAX_FOR_LONG_WORDS} selected for {name}")
33-
return cls(
34-
max=cls.MAX_FOR_LONG_WORDS
35-
) # we allow more typos if the name is longer
33+
return cls(max=cls.MAX_FOR_LONG_WORDS) # we allow more typos if the name is longer
3634

3735
def is_inside_threshold(self, value: float) -> bool:
3836
return self.min <= value <= self.max

src/twyn/trusted_packages/references.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
InvalidPyPiFormatError,
1111
)
1212

13-
logger = logging.getLogger()
13+
logger = logging.getLogger("twyn")
1414

1515

1616
class AbstractPackageReference(ABC):

src/twyn/trusted_packages/selectors.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
if TYPE_CHECKING:
1111
from twyn.trusted_packages.trusted_packages import _PackageNames
1212

13-
logger = logging.getLogger()
13+
logger = logging.getLogger("twyn")
1414

1515

1616
class AbstractSelector(ABC):

0 commit comments

Comments
 (0)