1
- """Models for the response of the configuration object."""
2
- from pydantic import BaseModel
3
-
4
- from readthedocs .config .utils import to_dict
5
-
6
-
7
- class Base :
8
-
9
- """
10
- Base class for every configuration.
11
-
12
- Each inherited class should define
13
- its attributes in the `__slots__` attribute.
14
-
15
- We are using `__slots__` so we can't add more attributes by mistake,
16
- this is similar to a namedtuple.
17
- """
18
-
19
- def __init__ (self , ** kwargs ):
20
- for name in self .__slots__ :
21
- setattr (self , name , kwargs [name ])
1
+ """
2
+ Models for the response of the configuration object.
22
3
23
- def as_dict ( self ):
24
- return { name : to_dict ( getattr ( self , name )) for name in self . __slots__ }
4
+ We make use of pydantic to define the models/dataclasses for all the
5
+ options that the user can define in the configuration file.
25
6
7
+ Pydantic does runtime type checking and validation,
8
+ but we aren't using it yet, and instead we are doing the validation
9
+ in a separate step.
10
+ """
11
+ from typing import Literal
26
12
27
- # TODO: rename this class to `Build`
28
- class BuildWithOs (Base ):
29
- __slots__ = ("os" , "tools" , "jobs" , "apt_packages" , "commands" )
30
-
31
- def __init__ (self , ** kwargs ):
32
- kwargs .setdefault ("apt_packages" , [])
33
- kwargs .setdefault ("commands" , [])
34
- super ().__init__ (** kwargs )
13
+ from pydantic import BaseModel
35
14
36
15
37
- class BuildTool (Base ):
38
- __slots__ = ("version" , "full_version" )
16
+ class BuildTool (BaseModel ):
17
+ version : str
18
+ full_version : str
39
19
40
20
41
21
class BuildJobsBuildTypes (BaseModel ):
42
-
43
22
"""Object used for `build.jobs.build` key."""
44
23
45
24
html : list [str ] | None = None
46
25
pdf : list [str ] | None = None
47
26
epub : list [str ] | None = None
48
27
htmlzip : list [str ] | None = None
49
28
50
- def as_dict (self ):
51
- # Just to keep compatibility with the old implementation.
52
- return self .model_dump ()
53
-
54
29
55
30
class BuildJobs (BaseModel ):
56
-
57
31
"""Object used for `build.jobs` key."""
58
32
59
33
pre_checkout : list [str ] = []
@@ -70,42 +44,58 @@ class BuildJobs(BaseModel):
70
44
build : BuildJobsBuildTypes = BuildJobsBuildTypes ()
71
45
post_build : list [str ] = []
72
46
73
- def as_dict (self ):
74
- # Just to keep compatibility with the old implementation.
75
- return self .model_dump ()
47
+
48
+ # TODO: rename this class to `Build`
49
+ class BuildWithOs (BaseModel ):
50
+ os : str
51
+ tools : dict [str , BuildTool ]
52
+ jobs : BuildJobs = BuildJobs ()
53
+ apt_packages : list [str ] = []
54
+ commands : list [str ] = []
76
55
77
56
78
- class Python ( Base ):
79
- __slots__ = ( "install" ,)
57
+ class PythonInstallRequirements ( BaseModel ):
58
+ requirements : str
80
59
81
60
82
- class PythonInstallRequirements (Base ):
83
- __slots__ = ("requirements" ,)
61
+ class PythonInstall (BaseModel ):
62
+ path : str
63
+ method : Literal ["pip" , "setuptools" ] = "pip"
64
+ extra_requirements : list [str ] = []
84
65
85
66
86
- class PythonInstall (Base ):
87
- __slots__ = (
88
- "path" ,
89
- "method" ,
90
- "extra_requirements" ,
91
- )
67
+ class Python (BaseModel ):
68
+ install : list [PythonInstall | PythonInstallRequirements ] = []
92
69
93
70
94
- class Conda (Base ):
95
- __slots__ = ( " environment" ,)
71
+ class Conda (BaseModel ):
72
+ environment : str
96
73
97
74
98
- class Sphinx (Base ):
99
- __slots__ = ("builder" , "configuration" , "fail_on_warning" )
75
+ class Sphinx (BaseModel ):
76
+ configuration : str | None
77
+ # NOTE: This is how we save the object in the DB,
78
+ # the actual options for users are "html", "htmldir", "singlehtml".
79
+ builder : Literal ["sphinx" , "sphinx_htmldir" , "sphinx_singlehtml" ] = "sphinx"
80
+ fail_on_warning : bool = False
100
81
101
82
102
- class Mkdocs (Base ):
103
- __slots__ = ("configuration" , "fail_on_warning" )
83
+ class Mkdocs (BaseModel ):
84
+ configuration : str | None
85
+ fail_on_warning : bool = False
104
86
105
87
106
- class Submodules (Base ):
107
- __slots__ = ("include" , "exclude" , "recursive" )
88
+ class Submodules (BaseModel ):
89
+ include : list [str ] | Literal ["all" ] = []
90
+ exclude : list [str ] | Literal ["all" ] = []
91
+ recursive : bool = False
108
92
109
93
110
- class Search (Base ):
111
- __slots__ = ("ranking" , "ignore" )
94
+ class Search (BaseModel ):
95
+ ranking : dict [str , int ] = {}
96
+ ignore : list [str ] = [
97
+ "search.html" ,
98
+ "search/index.html" ,
99
+ "404.html" ,
100
+ "404/index.html" ,
101
+ ]
0 commit comments