Coverage for C: \ Users \ peaco \ OneDrive \ Documents \ GitHub \ mt_metadata \ mt_metadata \ common \ provenance.py: 96%

26 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-01-10 00:11 -0800

1# ===================================================== 

2# Imports 

3# ===================================================== 

4from typing import Annotated 

5 

6import numpy as np 

7import pandas as pd 

8from pydantic import Field, field_validator, PrivateAttr, ValidationInfo 

9 

10from mt_metadata.base import MetadataBase 

11from mt_metadata.common import AuthorPerson, Comment, Person, Software 

12from mt_metadata.common.mttime import MTime 

13 

14 

15# ===================================================== 

16class Provenance(MetadataBase): 

17 _skip_equals: list[str] = PrivateAttr(["creation_time"]) 

18 creation_time: Annotated[ 

19 MTime | str | float | int | np.datetime64 | pd.Timestamp, 

20 Field( 

21 default_factory=lambda: MTime(time_stamp=None), 

22 description="Date and time the file was created.", 

23 alias=None, 

24 json_schema_extra={ 

25 "units": None, 

26 "required": True, 

27 "examples": "2020-02-08T12:23:40.324600+00:00", 

28 "type": "string", 

29 }, 

30 ), 

31 ] 

32 

33 comments: Annotated[ 

34 Comment, 

35 Field( 

36 default_factory=Comment, 

37 description="Any comments on provenance of the data.", 

38 alias=None, 

39 json_schema_extra={ 

40 "units": None, 

41 "required": False, 

42 "examples": "all good", 

43 "type": "string", 

44 }, 

45 ), 

46 ] 

47 

48 log: Annotated[ 

49 str | None, 

50 Field( 

51 default=None, 

52 description="A history of changes made to the data.", 

53 alias=None, 

54 json_schema_extra={ 

55 "units": None, 

56 "required": False, 

57 "examples": "2020-02-10T14:24:45+00:00 updated metadata", 

58 "type": "string", 

59 }, 

60 ), 

61 ] 

62 

63 creator: Annotated[ 

64 AuthorPerson, 

65 Field( 

66 default_factory=AuthorPerson, 

67 description="Person who created the data.", 

68 alias=None, 

69 json_schema_extra={ 

70 "units": None, 

71 "required": False, 

72 "examples": "Person(name=J. Pedantic, email=jped@mt.com)", 

73 }, 

74 ), 

75 ] 

76 

77 submitter: Annotated[ 

78 AuthorPerson, 

79 Field( 

80 default_factory=AuthorPerson, 

81 description="Person who submitted the data.", 

82 alias=None, 

83 json_schema_extra={ 

84 "units": None, 

85 "required": False, 

86 "examples": "Person(name=submitter_name, email=submitter@email)", 

87 }, 

88 ), 

89 ] 

90 

91 archive: Annotated[ 

92 Person, 

93 Field( 

94 default_factory=Person, 

95 description="Archive from which the data was downloaded from.", 

96 alias=None, 

97 json_schema_extra={ 

98 "units": None, 

99 "required": False, 

100 "examples": "Person(name=archive_name, url=https://archive.url)", 

101 }, 

102 ), 

103 ] 

104 

105 software: Annotated[ 

106 Software, 

107 Field( 

108 default_factory=Software, 

109 description="Software used to create the data.", 

110 alias=None, 

111 json_schema_extra={ 

112 "units": None, 

113 "required": False, 

114 "examples": "Software(name=mt_metadata, version=0.1)", 

115 }, 

116 ), 

117 ] 

118 

119 @field_validator("creation_time", mode="before") 

120 @classmethod 

121 def validate_creation_time( 

122 cls, field_value: MTime | float | int | np.datetime64 | pd.Timestamp | str 

123 ): 

124 return MTime(time_stamp=field_value) 

125 

126 @field_validator("comments", mode="before") 

127 @classmethod 

128 def validate_comments(cls, value, info: ValidationInfo) -> Comment: 

129 """ 

130 Validate that the value is a valid comment. 

131 """ 

132 if isinstance(value, str): 

133 return Comment(value=value) 

134 return value