Coverage for src/somesy/julia/writer.py: 100%

33 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2024-04-30 09:42 +0000

1"""Julia writer.""" 

2import logging 

3from pathlib import Path 

4from typing import Optional 

5 

6import tomlkit 

7from rich.pretty import pretty_repr 

8 

9from somesy.core.models import Person, ProjectMetadata 

10from somesy.core.writer import ProjectMetadataWriter 

11 

12from .models import JuliaConfig 

13 

14logger = logging.getLogger("somesy") 

15 

16 

17class Julia(ProjectMetadataWriter): 

18 """Julia config file handler parsed from Project.toml.""" 

19 

20 def __init__(self, path: Path): 

21 """Julia config file handler parsed from Project.toml. 

22 

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

24 """ 

25 super().__init__(path, create_if_not_exists=False) 

26 

27 def _load(self) -> None: 

28 """Load Project.toml file.""" 

29 with open(self.path) as f: 

30 self._data = tomlkit.load(f) 

31 

32 def _validate(self) -> None: 

33 """Validate poetry config using pydantic class. 

34 

35 In order to preserve toml comments and structure, tomlkit library is used. 

36 Pydantic class only used for validation. 

37 """ 

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

39 logger.debug( 

40 f"Validating config using {JuliaConfig.__name__}: {pretty_repr(config)}" 

41 ) 

42 JuliaConfig(**config) 

43 

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

45 """Save the julia file.""" 

46 path = path or self.path 

47 with open(path, "w") as f: 

48 tomlkit.dump(self._data, f) 

49 

50 @staticmethod 

51 def _from_person(person: Person): 

52 """Convert project metadata person object to a name+email string.""" 

53 return person.to_name_email_string() 

54 

55 @staticmethod 

56 def _to_person(person_obj: str) -> Person: 

57 """Parse name+email string to a Person.""" 

58 return Person.from_name_email_string(person_obj) 

59 

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

61 """Sync output file with other metadata files.""" 

62 # overridden to not sync fields that are not present in the Project.toml file 

63 self.name = metadata.name 

64 self.version = metadata.version 

65 

66 self._sync_authors(metadata)