MCPcopy Create free account
hub / github.com/python-hyper/uritemplate / URIVariable

Class URIVariable

uritemplate/variable.py:188–531  ·  view source on GitHub ↗

This object validates everything inside the URITemplate object. It validates template expansions and will truncate length as decided by the template. Please note that just like the :class:`URITemplate `, this object's ``__str__`` and ``__repr__`` methods do not return

Source from the content-addressed store, hash-verified

186
187
188class URIVariable:
189 """This object validates everything inside the URITemplate object.
190
191 It validates template expansions and will truncate length as decided by
192 the template.
193
194 Please note that just like the :class:`URITemplate <URITemplate>`, this
195 object&#x27;s ``__str__`` and ``__repr__`` methods do not return the same
196 information. Calling ``str(var)`` will return the original variable.
197
198 This object does the majority of the heavy lifting. The ``URITemplate``
199 object finds the variables in the URI and then creates ``URIVariable``
200 objects. Expansions of the URI are handled by each ``URIVariable``
201 object. ``URIVariable.expand()`` returns a dictionary of the original
202 variable and the expanded value. Check that method&#x27;s documentation for
203 more information.
204
205 """
206
207 def __init__(self, var: str):
208 #: The original string that comes through with the variable
209 self.original: str = var
210 #: The operator for the variable
211 self.operator: Operator = Operator.default
212 #: List of variables in this variable
213 self.variables: t.List[t.Tuple[str, t.MutableMapping[str, t.Any]]] = (
214 []
215 )
216 #: List of variable names
217 self.variable_names: t.List[str] = []
218 #: List of defaults passed in
219 self.defaults: t.MutableMapping[str, ScalarVariableValue] = {}
220 # Parse the variable itself.
221 self.parse()
222
223 def __repr__(self) -> str:
224 return "URIVariable(%s)" % self
225
226 def __str__(self) -> str:
227 return self.original
228
229 def parse(self) -> None:
230 """Parse the variable.
231
232 This finds the:
233 - operator,
234 - set of safe characters,
235 - variables, and
236 - defaults.
237
238 """
239 var_list_str = self.original
240 if (operator_str := self.original[0]) in _operators:
241 self.operator = Operator.from_string(operator_str)
242 var_list_str = self.original[1:]
243
244 var_list = var_list_str.split(",")
245

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…