comparison configmix/config.py @ 333:5ec0ae3bb8db

Docs: quoting
author Franz Glasner <fzglas.hg@dom66.de>
date Fri, 07 May 2021 09:31:22 +0200
parents d81d2cdf4925
children a04cd5dbcd2c
comparison
equal deleted inserted replaced
332:5c2c72d26b63 333:5ec0ae3bb8db
77 """Get a variable where the hierarchy is given in `names` as sequence 77 """Get a variable where the hierarchy is given in `names` as sequence
78 and the namespace is given in `namespace`. 78 and the namespace is given in `namespace`.
79 79
80 No variable interpolation is done and no filters are applied. 80 No variable interpolation is done and no filters are applied.
81 81
82 Quoting of `names` and `namespace` is *not* needed and wrong.
83
82 """ 84 """
83 default = kwds.pop("default", _MARKER) 85 default = kwds.pop("default", _MARKER)
84 namespace = kwds.pop("namespace", None) 86 namespace = kwds.pop("namespace", None)
85 try: 87 try:
86 if not namespace: 88 if not namespace:
102 def getvar(self, varname, default=_MARKER): 104 def getvar(self, varname, default=_MARKER):
103 """Get a variable of the form ``[ns:][[key1.]key2.]name`` - including 105 """Get a variable of the form ``[ns:][[key1.]key2.]name`` - including
104 variables from other namespaces. 106 variables from other namespaces.
105 107
106 No variable interpolation is done and no filters are applied. 108 No variable interpolation is done and no filters are applied.
109
110 Special characters (e.g. ``:`` and ``.``) must be quoted when using
111 the default namespace.
112
113 See also :meth:`~.quote`.
107 114
108 """ 115 """
109 varns, varname = self._split_ns(varname) 116 varns, varname = self._split_ns(varname)
110 if not varns: 117 if not varns:
111 varnameparts = [self.unquote(vp) for vp in varname.split(self._HIER_SEPARATOR)] 118 varnameparts = [self.unquote(vp) for vp in varname.split(self._HIER_SEPARATOR)]
373 value = filterfn(self, value) 380 value = filterfn(self, value)
374 return value 381 return value
375 382
376 @classmethod 383 @classmethod
377 def quote(klass, s): 384 def quote(klass, s):
378 """Quote a key to protect all dangerous chars: ``%``, ``.``, ``:``, 385 """Replace important special characters in string `s` by replacing
379 ``#`` and ``|`` 386 them with ``%xNN`` where `NN` are the two hexadecimal digits of the
387 characters unicode codepoint value.
388
389 Handled are the important special chars: ``%``, ``.``, ``:``,
390 ``#``; ``'``, ``"`` and ``|``.
391
392 See also the :ref:`quoting` section.
380 393
381 """ 394 """
382 qc = klass._QUOTE 395 qc = klass._QUOTE
383 s = s.replace(qc, qc + "x25") 396 s = s.replace(qc, qc + "x25")
384 s = s.replace(klass._DOT, qc + "x2e") 397 s = s.replace(klass._DOT, qc + "x2e")
388 s = s.replace('"', qc + "x22") 401 s = s.replace('"', qc + "x22")
389 return s.replace("'", qc + "x27") 402 return s.replace("'", qc + "x27")
390 403
391 @classmethod 404 @classmethod
392 def unquote(klass, s): 405 def unquote(klass, s):
393 """Unquote the content of `s`: handle all patterns ``%xXX``, 406 """Unquote the content of `s`: handle all patterns ``%xNN``,
394 ``%uXXXX`` or `%UXXXXXXXX`` 407 ``%uNNNN`` or ``%UNNNNNNNN``.
408
409 This is the inverse of :meth:`~.quote`.
395 410
396 """ 411 """
397 if klass._QUOTE not in s: 412 if klass._QUOTE not in s:
398 return s 413 return s
399 res = [] 414 res = []