Coverage for bookbuilderpy/build_result.py: 54%

52 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-07-17 23:15 +0000

1"""A collection for build results.""" 

2import os.path 

3from dataclasses import dataclass 

4 

5from bookbuilderpy.path import Path 

6from bookbuilderpy.strings import ( 

7 enforce_non_empty_str, 

8 enforce_non_empty_str_without_ws, 

9) 

10from bookbuilderpy.types import type_error 

11 

12 

13@dataclass(frozen=True, init=False, order=True) 

14class File: 

15 """A single file created by a build.""" 

16 

17 #: the path of the file 

18 path: Path 

19 #: the size of the file 

20 size: int 

21 #: the file name suffix 

22 suffix: str 

23 

24 def __init__(self, 

25 path: Path): 

26 """ 

27 Create the build result as a file. 

28 

29 :param path: the path of the file 

30 """ 

31 if not isinstance(path, Path): 

32 raise type_error(path, "path", Path) 

33 path.enforce_file() 

34 object.__setattr__(self, "path", path) 

35 size = os.path.getsize(self.path) 

36 if not isinstance(size, int): 

37 raise type_error(size, f"os.path.getsize({self.path})", int) 

38 if size <= 0: 

39 raise ValueError(f"File size of '{path}' is {size}.") 

40 object.__setattr__(self, "size", size) 

41 _, ext = Path.split_prefix_suffix(os.path.basename(path)) 

42 object.__setattr__(self, "suffix", ext) 

43 

44 

45@dataclass(frozen=True, init=False, order=True) 

46class LangResult: 

47 """All the results created for one language.""" 

48 

49 #: the language code 

50 lang: str | None 

51 #: the language name 

52 lang_name: str | None 

53 #: the directory containing the results 

54 directory: Path 

55 #: the generated files 

56 results: tuple[File, ...] 

57 

58 def __init__(self, 

59 lang: str | None, 

60 lang_name: str | None, 

61 directory: Path, 

62 results: tuple[File, ...]): 

63 """ 

64 Create the build result of a given language. 

65 

66 :param lang: the language 

67 :param lang_name: the language name 

68 :param directory: the path of directory containing all the files 

69 :param results: the result files 

70 """ 

71 if (lang is None) != (lang_name is None): 

72 raise ValueError( 

73 f"lang cannot be '{lang}' if lang name is '{lang_name}'.") 

74 if lang is None: 

75 object.__setattr__(self, "lang", None) 

76 object.__setattr__(self, "lang_name", None) 

77 else: 

78 object.__setattr__(self, "lang", 

79 enforce_non_empty_str_without_ws(lang)) 

80 if lang_name is not None: 

81 object.__setattr__(self, "lang_name", 

82 enforce_non_empty_str(lang_name)) 

83 else: 

84 object.__setattr__(self, "lang_name", None) 

85 if not isinstance(directory, Path): 

86 raise type_error(directory, "directory", Path) 

87 directory.enforce_dir() 

88 object.__setattr__(self, "directory", directory) 

89 

90 if not isinstance(results, tuple): 

91 raise type_error(results, "results", tuple) 

92 if len(results) <= 0: 

93 raise ValueError("results list cannot be empty.") 

94 for f in results: 

95 self.directory.enforce_contains(f.path) 

96 object.__setattr__(self, "results", results) 

97 

98 def __iter__(self): 

99 """Iterate over this result list.""" 

100 return iter(self.results)