Coverage for src/somesy/mkdocs/writer.py: 85%

60 statements  

« prev     ^ index     » next       coverage.py v7.6.0, created at 2025-03-10 14:56 +0000

1"""Project documentation with Markdown (MkDocs) parser and saver.""" 

2 

3import logging 

4from pathlib import Path 

5from typing import List, Optional, Union 

6 

7from rich.pretty import pretty_repr 

8from ruamel.yaml import YAML 

9 

10from somesy.core.models import Entity, Person, ProjectMetadata 

11from somesy.core.writer import FieldKeyMapping, IgnoreKey, ProjectMetadataWriter 

12from somesy.mkdocs.models import MkDocsConfig 

13 

14logger = logging.getLogger("somesy") 

15 

16 

17class MkDocs(ProjectMetadataWriter): 

18 """Project documentation with Markdown (MkDocs) parser and saver.""" 

19 

20 def __init__( 

21 self, 

22 path: Path, 

23 create_if_not_exists: bool = False, 

24 pass_validation: Optional[bool] = False, 

25 ): 

26 """Project documentation with Markdown (MkDocs) parser. 

27 

28 See [somesy.core.writer.ProjectMetadataWriter.__init__][]. 

29 """ 

30 self._yaml = YAML() 

31 self._yaml.preserve_quotes = True 

32 

33 mappings: FieldKeyMapping = { 

34 "name": ["site_name"], 

35 "description": ["site_description"], 

36 "homepage": ["site_url"], 

37 "repository": ["repo_url"], 

38 "authors": ["site_author"], 

39 "documentation": IgnoreKey(), 

40 "version": IgnoreKey(), 

41 "maintainers": IgnoreKey(), 

42 "license": IgnoreKey(), 

43 "keywords": IgnoreKey(), 

44 } 

45 super().__init__( 

46 path, 

47 create_if_not_exists=create_if_not_exists, 

48 direct_mappings=mappings, 

49 pass_validation=pass_validation, 

50 ) 

51 

52 def _load(self): 

53 """Load the MkDocs file.""" 

54 with open(self.path) as f: 

55 self._data = self._yaml.load(f) 

56 

57 def _validate(self) -> None: 

58 """Validate the MkDocs file.""" 

59 if self.pass_validation: 

60 return 

61 config = dict(self._get_property([])) 

62 logger.debug( 

63 f"Validating config using {MkDocsConfig.__name__}: {pretty_repr(config)}" 

64 ) 

65 MkDocsConfig(**config) 

66 

67 def save(self, path: Optional[Path] = None) -> None: 

68 """Save the MkDocs object to a file.""" 

69 path = path or self.path 

70 self._yaml.dump(self._data, path) 

71 

72 @property 

73 def authors(self): 

74 """Return the only author from the source file as list.""" 

75 authors = self._get_property(self._get_key("authors")) 

76 if authors is None or self._to_person(authors) is None: 

77 return [] 

78 else: 

79 return [authors] 

80 

81 @authors.setter 

82 def authors(self, authors: List[Union[Entity, Person]]) -> None: 

83 """Set the authors of the project.""" 

84 authors = self._from_person(authors[0]) 

85 self._set_property(self._get_key("authors"), authors) 

86 

87 @staticmethod 

88 def _from_person(person: Union[Entity, Person]): 

89 """MkDocs Person is a string with full name.""" 

90 return person.to_name_email_string() 

91 

92 @staticmethod 

93 def _to_person(person: str) -> Optional[Union[Entity, Person]]: 

94 """MkDocs Person is a string with full name.""" 

95 try: 

96 return Person.from_name_email_string(person) 

97 except (ValueError, AttributeError): 

98 logger.info(f"Cannot convert {person} to Person object, trying Entity.") 

99 

100 try: 

101 return Entity.from_name_email_string(person) 

102 except (ValueError, AttributeError): 

103 logger.warning(f"Cannot convert {person} to Entity.") 

104 return None 

105 

106 def sync(self, metadata: ProjectMetadata) -> None: 

107 """Sync the MkDocs object with the ProjectMetadata object.""" 

108 self.name = metadata.name 

109 self.description = metadata.description 

110 # no author merge since it is a free text field 

111 self.authors = metadata.authors() 

112 if metadata.homepage: 

113 self.homepage = str(metadata.homepage) 

114 if metadata.repository: 

115 self.repository = str(metadata.repository) 

116 self.repo_name = metadata.repository.path