30
30
class ConfigSet :
31
31
"""A set of configuration that belong together (such as a tox environment settings, core tox settings)"""
32
32
33
- def __init__ (self , conf : "Config" , name : Optional [str ]):
34
- self ._name = name
33
+ def __init__ (self , conf : "Config" ):
35
34
self ._conf = conf
36
35
self .loaders : List [Loader [Any ]] = []
37
36
self ._defined : Dict [str , ConfigDefinition [Any ]] = {}
@@ -59,7 +58,7 @@ def add_config(
59
58
:return: the new dynamic config definition
60
59
"""
61
60
keys_ = self ._make_keys (keys )
62
- definition = ConfigDynamicDefinition (keys_ , desc , self . _name , of_type , default , post_process , kwargs )
61
+ definition = ConfigDynamicDefinition (keys_ , desc , of_type , default , post_process , kwargs )
63
62
result = self ._add_conf (keys_ , definition )
64
63
return cast (ConfigDynamicDefinition [V ], result )
65
64
@@ -73,7 +72,7 @@ def add_constant(self, keys: Union[str, Sequence[str]], desc: str, value: V) ->
73
72
:return: the new constant config value
74
73
"""
75
74
keys_ = self ._make_keys (keys )
76
- definition = ConfigConstantDefinition (keys_ , desc , self . _name , value )
75
+ definition = ConfigConstantDefinition (keys_ , desc , value )
77
76
result = self ._add_conf (keys_ , definition )
78
77
return cast (ConfigConstantDefinition [V ], result )
79
78
@@ -84,12 +83,7 @@ def _make_keys(keys: Union[str, Sequence[str]]) -> Sequence[str]:
84
83
def _add_conf (self , keys : Sequence [str ], definition : ConfigDefinition [V ]) -> ConfigDefinition [V ]:
85
84
key = keys [0 ]
86
85
if key in self ._defined :
87
- earlier = self ._defined [key ]
88
- # core definitions may be defined multiple times as long as all their options match, first defined wins
89
- if self ._name is None and definition == earlier :
90
- definition = earlier
91
- else :
92
- raise ValueError (f"config { key } already defined" )
86
+ self ._on_duplicate_conf (key , definition )
93
87
else :
94
88
self ._keys [key ] = None
95
89
for item in keys :
@@ -98,6 +92,11 @@ def _add_conf(self, keys: Sequence[str], definition: ConfigDefinition[V]) -> Con
98
92
self ._defined [key ] = definition
99
93
return definition
100
94
95
+ def _on_duplicate_conf (self , key : str , definition : ConfigDefinition [V ]) -> None :
96
+ earlier = self ._defined [key ]
97
+ if definition != earlier : # pragma: no branch
98
+ raise ValueError (f"config { key } already defined" )
99
+
101
100
def __getitem__ (self , item : str ) -> Any :
102
101
"""
103
102
Get the config value for a given key (will materialize in case of dynamic config).
@@ -116,18 +115,14 @@ def load(self, item: str, chain: Optional[List[str]] = None) -> Any:
116
115
:return: the configuration value
117
116
"""
118
117
config_definition = self ._defined [item ]
119
- if chain is None :
120
- chain = []
121
- env_name = "tox" if self ._name is None else f"testenv:{ self ._name } "
122
- key = f"{ env_name } .{ item } "
123
- if key in chain :
124
- raise ValueError (f"circular chain detected { ', ' .join (chain [chain .index (key ):])} " )
125
- chain .append (key )
126
- return config_definition (self ._conf , item , self .loaders , chain )
118
+ return config_definition .__call__ (self ._conf , self .loaders , self .name , chain )
119
+
120
+ @property
121
+ def name (self ) -> Optional [str ]:
122
+ return None
127
123
128
124
def __repr__ (self ) -> str :
129
- values = (v for v in (f"name={ self ._name !r} " if self ._name else "" , f"loaders={ self .loaders !r} " ) if v )
130
- return f"{ self .__class__ .__name__ } ({ ', ' .join (values )} )"
125
+ return f"{ self .__class__ .__name__ } (loaders={ self .loaders !r} )"
131
126
132
127
def __iter__ (self ) -> Iterator [str ]:
133
128
""":return: iterate through the defined config keys (primary keys used)"""
@@ -166,8 +161,9 @@ def primary_key(self, key: str) -> str:
166
161
class CoreConfigSet (ConfigSet ):
167
162
"""Configuration set for the core tox config"""
168
163
169
- def __init__ (self , conf : "Config" , root : Path ) -> None :
170
- super ().__init__ (conf , name = None )
164
+ def __init__ (self , conf : "Config" , root : Path , src_path : Path ) -> None :
165
+ super ().__init__ (conf )
166
+ self .add_constant (keys = ["config_file_path" ], desc = "path to the configuration file" , value = src_path )
171
167
self .add_config (
172
168
keys = ["tox_root" , "toxinidir" ],
173
169
of_type = Path ,
@@ -198,12 +194,16 @@ def work_dir_builder(conf: "Config", env_name: Optional[str]) -> Path: # noqa
198
194
desc = "define environments to automatically run" ,
199
195
)
200
196
197
+ def _on_duplicate_conf (self , key : str , definition : ConfigDefinition [V ]) -> None : # noqa: U100
198
+ pass # core definitions may be defined multiple times as long as all their options match, first defined wins
199
+
201
200
202
201
class EnvConfigSet (ConfigSet ):
203
202
"""Configuration set for a tox environment"""
204
203
205
- def __init__ (self , conf : "Config" , name : Optional [str ]):
206
- super ().__init__ (conf , name = name )
204
+ def __init__ (self , conf : "Config" , name : str ):
205
+ self ._name = name
206
+ super ().__init__ (conf )
207
207
self .default_set_env_loader : Callable [[], Mapping [str , str ]] = lambda : {}
208
208
209
209
def set_env_post_process (values : SetEnv ) -> SetEnv :
@@ -220,7 +220,10 @@ def set_env_post_process(values: SetEnv) -> SetEnv:
220
220
221
221
@property
222
222
def name (self ) -> str :
223
- return self ._name # type: ignore
223
+ return self ._name
224
+
225
+ def __repr__ (self ) -> str :
226
+ return f"{ self .__class__ .__name__ } (name={ self ._name !r} , loaders={ self .loaders !r} )"
224
227
225
228
226
229
__all__ = (
0 commit comments