Coverage for C: \ Users \ peaco \ OneDrive \ Documents \ GitHub \ mt_metadata \ mt_metadata \ common \ funding_source.py: 90%

29 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 

6from pydantic import AnyHttpUrl, EmailStr, Field, field_validator, ValidationInfo 

7 

8from mt_metadata.base import MetadataBase 

9from mt_metadata.common import Comment 

10 

11 

12# ===================================================== 

13class FundingSource(MetadataBase): 

14 name: Annotated[ 

15 list[str] | str | None, 

16 Field( 

17 default=None, 

18 description="Persons name, should be full first and last name.", 

19 alias=None, 

20 json_schema_extra={ 

21 "units": None, 

22 "required": False, 

23 "examples": "person name", 

24 "items": {"type": "string"}, 

25 }, 

26 ), 

27 ] 

28 

29 organization: Annotated[ 

30 list[str] | str | None, 

31 Field( 

32 default=None, 

33 description="Organization full name", 

34 alias=None, 

35 json_schema_extra={ 

36 "units": None, 

37 "required": False, 

38 "examples": "mt gurus", 

39 "items": {"type": "string"}, 

40 }, 

41 ), 

42 ] 

43 

44 email: Annotated[ 

45 list[EmailStr] | EmailStr | None, 

46 Field( 

47 default=None, 

48 description="Email of the contact person", 

49 alias=None, 

50 json_schema_extra={ 

51 "units": None, 

52 "required": False, 

53 "examples": "mt.guru@em.org", 

54 "items": {"type": "string"}, 

55 }, 

56 ), 

57 ] 

58 

59 url: Annotated[ 

60 list[AnyHttpUrl] | AnyHttpUrl | None, 

61 Field( 

62 default=None, 

63 description="URL of the contact person", 

64 alias=None, 

65 json_schema_extra={ 

66 "units": None, 

67 "required": False, 

68 "examples": "em.org", 

69 "items": {"type": "string"}, 

70 }, 

71 ), 

72 ] 

73 

74 comments: Annotated[ 

75 Comment, 

76 Field( 

77 default_factory=lambda: Comment(), 

78 description="Any comments about the person", 

79 alias=None, 

80 json_schema_extra={ 

81 "units": None, 

82 "required": False, 

83 "examples": "expert digger", 

84 }, 

85 ), 

86 ] 

87 

88 grant_id: Annotated[ 

89 list[str] | str | None, 

90 Field( 

91 default=None, 

92 description="Grant ID number or name", 

93 alias=None, 

94 json_schema_extra={ 

95 "units": None, 

96 "required": False, 

97 "examples": "MT-01-2020", 

98 "items": {"type": "string"}, 

99 }, 

100 ), 

101 ] 

102 

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

104 @classmethod 

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

106 if isinstance(value, str): 

107 return Comment(value=value) 

108 return value 

109 

110 @field_validator("name", "organization", "email", "url", "grant_id", mode="before") 

111 @classmethod 

112 def validate_input(cls, value) -> list: 

113 """ 

114 make sure the inputs are lists 

115 

116 Parameters 

117 ---------- 

118 value : _type_ 

119 _description_ 

120 

121 Returns 

122 ------- 

123 list 

124 _description_ 

125 """ 

126 

127 if isinstance(value, (list, tuple)): 

128 return list(value) 

129 

130 elif isinstance(value, (EmailStr, AnyHttpUrl)): 

131 return [value] 

132 

133 elif isinstance(value, str): 

134 return [item.strip() for item in value.split(",")] 

135 

136 elif value is None: 

137 return None 

138 

139 else: 

140 raise TypeError(f"Cannot form a list from types {type(value)}.")