Coverage for src/somesy/commands/sync.py: 40%

45 statements  

« prev     ^ index     » next       coverage.py v7.6.0, created at 2024-07-29 07:42 +0000

1"""Sync selected metadata files with given input file.""" 

2 

3import logging 

4from pathlib import Path 

5from typing import Type 

6 

7from rich.pretty import pretty_repr 

8 

9from somesy.cff.writer import CFF 

10from somesy.codemeta import CodeMeta 

11from somesy.core.models import ProjectMetadata, SomesyInput 

12from somesy.core.writer import ProjectMetadataWriter 

13from somesy.fortran.writer import Fortran 

14from somesy.julia.writer import Julia 

15from somesy.mkdocs import MkDocs 

16from somesy.package_json.writer import PackageJSON 

17from somesy.pom_xml.writer import POM 

18from somesy.pyproject.writer import Pyproject 

19from somesy.rust import Rust 

20 

21logger = logging.getLogger("somesy") 

22 

23 

24def _sync_file( 

25 metadata: ProjectMetadata, file: Path, writer_cls: Type[ProjectMetadataWriter] 

26): 

27 """Sync metadata to a file using the provided writer.""" 

28 logger.verbose(f"Loading '{file.name}' ...") 

29 writer = writer_cls(file) 

30 logger.verbose(f"Syncing '{file.name}' ...") 

31 writer.sync(metadata) 

32 writer.save(file) 

33 logger.verbose(f"Saved synced '{file.name}'.\n") 

34 

35 

36def sync(somesy_input: SomesyInput): 

37 """Sync selected metadata files with given input file.""" 

38 conf, metadata = somesy_input.config, somesy_input.project 

39 

40 pp_metadata = pretty_repr(metadata.model_dump(exclude_defaults=True)) 

41 logger.debug(f"Project metadata: {pp_metadata}") 

42 

43 # update these only if they exist: 

44 

45 if conf.pyproject_file.is_file() and not conf.no_sync_pyproject: 

46 _sync_file(metadata, conf.pyproject_file, Pyproject) 

47 

48 if conf.package_json_file.is_file() and not conf.no_sync_package_json: 

49 _sync_file(metadata, conf.package_json_file, PackageJSON) 

50 

51 if conf.julia_file.is_file() and not conf.no_sync_julia: 

52 _sync_file(metadata, conf.julia_file, Julia) 

53 

54 if conf.fortran_file.is_file() and not conf.no_sync_fortran: 

55 _sync_file(metadata, conf.fortran_file, Fortran) 

56 

57 if conf.pom_xml_file.is_file() and not conf.no_sync_pom_xml: 

58 _sync_file(metadata, conf.pom_xml_file, POM) 

59 

60 if conf.mkdocs_file.is_file() and not conf.no_sync_mkdocs: 

61 _sync_file(metadata, conf.mkdocs_file, MkDocs) 

62 

63 if conf.rust_file.is_file() and not conf.no_sync_rust: 

64 _sync_file(metadata, conf.rust_file, Rust) 

65 

66 # create these by default if they are missing: 

67 if not conf.no_sync_cff: 

68 _sync_file(metadata, conf.cff_file, CFF) 

69 

70 if not conf.no_sync_codemeta: 

71 _sync_file(metadata, conf.codemeta_file, CodeMeta)