Source code for package_name_to_import_with.data_using_module
1"""Define package contents."""
2
3import importlib.resources
4import json
5import re
6
7import pydantic
8
9from .utils import CustomPydanticBaseModel
10
11
[docs]
12class PackageMetadata(CustomPydanticBaseModel):
13 """Define keys and types of corresponding values for package metadata.
14
15 Attributes
16 ----------
17 Name : str
18 name of the package
19 Version : str
20 version of the package
21 Description : str
22 description of the package
23 Keywords : list[str]
24 keywords associated with the package
25 License : str
26 license of the package
27 Maintainers : list[str]
28 maintainers of the package
29 Authors : list[str]
30 authors of the package
31 Links : dict[str, pydantic.HttpUrl]
32 links associated with the package
33 """
34
35 Name: str = pydantic.Field(description="name of the package")
36 Version: str = pydantic.Field(description="version of the package")
37 Description: str = pydantic.Field(description="description of the package")
38 Keywords: list[str] = pydantic.Field(description="keywords associated with the package")
39 License: str = pydantic.Field(description="license of the package")
40 Maintainers: list[str] = pydantic.Field(description="maintainers of the package")
41 Authors: list[str] = pydantic.Field(description="authors of the package")
42 Links: dict[str, pydantic.HttpUrl] = pydantic.Field(
43 description="links associated with the package"
44 )
45
[docs]
46 @pydantic.field_validator("Version")
47 @classmethod
48 def validate_version(cls: type["PackageMetadata"], version: str) -> str:
49 """Validate if specified version adhere to semantic versioning.
50
51 Parameters
52 ----------
53 version : str
54 specified value for version
55
56 Returns
57 -------
58 str
59 unchanged `version` if validation passes
60
61 Raises
62 ------
63 ValueError
64 if `version` is not a valid semantic version
65
66 Notes
67 -----
68 #. Only checks for MAJOR.MINOR.PATCH format.
69
70 References
71 ----------
72 `Semantic Versioning 2.0.0 <https://semver.org/>`_.
73 """
74 del cls # skipcq: PTC-W0043
75
76 if re.fullmatch(r"(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)", version) is None:
77 raise ValueError(f"Invalid semantic version: {version}")
78
79 return version
80
81
82METADATA_CONTENTS: str = (
83 importlib.resources.files("package_name_to_import_with").joinpath("metadata.json").read_text()
84)
85METADATA = PackageMetadata(**json.loads(METADATA_CONTENTS))