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

29 statements  

« prev     ^ index     » next       coverage.py v6.4.1, created at 2022-09-04 11:35 +0200

1"""dependenpy printer module.""" 

2 

3from __future__ import annotations 

4 

5import sys 

6from typing import IO, Any 

7 

8CSV = "csv" 

9JSON = "json" 

10TEXT = "text" 

11FORMAT = (CSV, JSON, TEXT) 

12 

13 

14class PrintMixin(object): 

15 """Print mixin class.""" 

16 

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

18 """ 

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

20 

21 Args: 

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

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

24 **kwargs: additional arguments. 

25 """ 

26 if format is None: 26 ↛ 27line 26 didn't jump to line 27, because the condition on line 26 was never true

27 format = TEXT 

28 

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

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

31 

32 if format == TEXT: 32 ↛ 34line 32 didn't jump to line 34, because the condition on line 32 was never false

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

34 elif format == CSV: 

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

36 elif format == JSON: 

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

38 

39 def _to_text(self, **kwargs): 

40 raise NotImplementedError 

41 

42 def _to_csv(self, **kwargs): 

43 raise NotImplementedError 

44 

45 def _to_json(self, **kwargs): 

46 raise NotImplementedError 

47 

48 

49def guess_depth(packages: list[str]) -> int: 

50 """ 

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

52 

53 Args: 

54 packages: List of packages. 

55 

56 Returns: 

57 Guessed depth to use. 

58 """ 

59 if len(packages) == 1: 

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

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