Coverage for src/somesy/fortran/models.py: 92%

25 statements  

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

1"""Pyproject models.""" 

2 

3from typing import Optional, Set 

4 

5from packaging.version import parse as parse_version 

6from pydantic import ( 

7 BaseModel, 

8 Field, 

9 field_validator, 

10) 

11from typing_extensions import Annotated 

12 

13from somesy.core.types import HttpUrlStr 

14 

15 

16class FortranConfig(BaseModel): 

17 """Fortran configuration model.""" 

18 

19 model_config = dict(use_enum_values=True) 

20 

21 name: Annotated[ 

22 str, 

23 Field(pattern=r"^[A-Za-z0-9]+([_-][A-Za-z0-9]+)*$", description="Package name"), 

24 ] 

25 version: Annotated[ 

26 Optional[str], 

27 Field( 

28 pattern=r"^\d+(\.\d+)*((a|b|rc)\d+)?(post\d+)?(dev\d+)?$", 

29 description="Package version", 

30 ), 

31 ] = None 

32 description: Annotated[Optional[str], Field(description="Package description")] = ( 

33 None 

34 ) 

35 license: Annotated[ 

36 Optional[str], 

37 Field(description="SPDX license identifier(s)."), 

38 ] = None 

39 author: Annotated[ 

40 Optional[str], Field(description="Package author information") 

41 ] = None 

42 maintainer: Annotated[ 

43 Optional[str], Field(description="Package maintainer information") 

44 ] = None 

45 copyright: Annotated[Optional[str], Field(description="Package copyright text")] = ( 

46 None 

47 ) 

48 homepage: Annotated[Optional[HttpUrlStr], Field(description="Package homepage")] = ( 

49 None 

50 ) 

51 keywords: Annotated[ 

52 Optional[Set[str]], Field(description="Keywords that describe the package") 

53 ] = None 

54 categories: Annotated[ 

55 Optional[Set[str]], Field(description="Categories that package falls into") 

56 ] = None 

57 

58 @field_validator("version") 

59 @classmethod 

60 def validate_version(cls, v): 

61 """Validate version using PEP 440.""" 

62 try: 

63 _ = parse_version(v) 

64 except ValueError as err: 

65 raise ValueError("Invalid version") from err 

66 return v