Coverage for src/dependenpy/_internal/helpers.py: 64.44%

33 statements  

« prev     ^ index     » next       coverage.py v7.10.5, created at 2025-08-24 18:36 +0200

1from __future__ import annotations 

2 

3import sys 

4from typing import IO, TYPE_CHECKING, Any 

5 

6if TYPE_CHECKING: 

7 from collections.abc import Sequence 

8 

9 

10CSV = "csv" 

11"""CSV format.""" 

12JSON = "json" 

13"""JSON format.""" 

14TEXT = "text" 

15"""Plain text format.""" 

16FORMAT = (CSV, JSON, TEXT) 

17"""Supported output formats.""" 

18 

19 

20class PrintMixin: 

21 """Print mixin class.""" 

22 

23 def print(self, format: str | None = TEXT, output: IO = sys.stdout, **kwargs: Any) -> None: # noqa: A002 

24 """Print the object in a file or on standard output by default. 

25 

26 Args: 

27 format: output format (csv, json or text). 

28 output: descriptor to an opened file (default to standard output). 

29 **kwargs: additional arguments. 

30 """ 

31 if format is None: 31 ↛ 32line 31 didn't jump to line 32 because the condition on line 31 was never true

32 format = TEXT 

33 

34 if format != TEXT: 34 ↛ 35line 34 didn't jump to line 35 because the condition on line 34 was never true

35 kwargs.pop("zero", "") 

36 

37 if format == TEXT: 37 ↛ 39line 37 didn't jump to line 39 because the condition on line 37 was always true

38 print(self._to_text(**kwargs), file=output) 

39 elif format == CSV: 

40 print(self._to_csv(**kwargs), file=output) 

41 elif format == JSON: 

42 print(self._to_json(**kwargs), file=output) 

43 

44 def _to_text(self, **kwargs: Any) -> str: 

45 raise NotImplementedError 

46 

47 def _to_csv(self, **kwargs: Any) -> str: 

48 raise NotImplementedError 

49 

50 def _to_json(self, **kwargs: Any) -> str: 

51 raise NotImplementedError 

52 

53 

54def guess_depth(packages: Sequence[str]) -> int: 

55 """Guess the optimal depth to use for the given list of arguments. 

56 

57 Args: 

58 packages: List of packages. 

59 

60 Returns: 

61 Guessed depth to use. 

62 """ 

63 if len(packages) == 1: 

64 return packages[0].count(".") + 2 

65 return min(package.count(".") for package in packages) + 1