comparison configmix/config.py @ 656:2b1c7a68f913

Enable indexed access to lists in the configuration using an access path string representation like "~NNN~"
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 30 May 2022 09:31:29 +0200
parents b74f20e19c01
children 0eff8441c4b9
comparison
equal deleted inserted replaced
655:b74f20e19c01 656:2b1c7a68f913
241 _NS_SEPARATOR = u(b':') 241 _NS_SEPARATOR = u(b':')
242 _FILTER_SEPARATOR = u(b'|') 242 _FILTER_SEPARATOR = u(b'|')
243 _STARTTOK_REF = _STARTTOK + REF_NAMESPACE + _NS_SEPARATOR 243 _STARTTOK_REF = _STARTTOK + REF_NAMESPACE + _NS_SEPARATOR
244 _ENDTOK_REF = _ENDTOK 244 _ENDTOK_REF = _ENDTOK
245 _DOT = u(b'.') 245 _DOT = u(b'.')
246 _TILDE = u(b'~')
246 _QUOTE = u(b'%') 247 _QUOTE = u(b'%')
247 _QUOTE_x = u(b'x') 248 _QUOTE_x = u(b'x')
248 _QUOTE_u = u(b'u') 249 _QUOTE_u = u(b'u')
249 _QUOTE_U = u(b'U') 250 _QUOTE_U = u(b'U')
250 _COMMENT = u(b'#') 251 _COMMENT = u(b'#')
258 0x27: u(b'%x27'), 259 0x27: u(b'%x27'),
259 0x7b: u(b'%x7b'), 260 0x7b: u(b'%x7b'),
260 0x7d: u(b'%x7d'), 261 0x7d: u(b'%x7d'),
261 0x5b: u(b'%x5b'), 262 0x5b: u(b'%x5b'),
262 0x5d: u(b'%x5d'), 263 0x5d: u(b'%x5d'),
264 0x7e: u(b'%x7e'), # tilde `~`
263 } 265 }
264 _QUOTE_SAFE = u(b'abcdefghijklmnopqrstuvwxyz' 266 _QUOTE_SAFE = u(b'abcdefghijklmnopqrstuvwxyz'
265 b'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 267 b'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
266 b'0123456789' 268 b'0123456789'
267 b'-_@!$&/\\()=?*+~;,<>^') 269 b'-_@!$&/\\()=?*+;,<>^')
268 """Mostly used configuration key characters that do not need any quoting 270 """Mostly used configuration key characters that do not need any quoting
269 271
270 """ 272 """
271 273
272 274
279 ``#``; ``'``, ``"``, ``|``, ``{``, ``}``, ``[`` and ``]``. 281 ``#``; ``'``, ``"``, ``|``, ``{``, ``}``, ``[`` and ``]``.
280 282
281 See also the :ref:`quoting` section. 283 See also the :ref:`quoting` section.
282 284
283 """ 285 """
284 # Quick check whether all of the chars are in _QUOTE_SAFE 286 try:
285 if not s.lstrip(_QUOTE_SAFE): 287 # Quick check whether all of the chars are in _QUOTE_SAFE
286 return s 288 if not s.lstrip(_QUOTE_SAFE):
289 return s
290 except AttributeError:
291 #
292 # Check whether s is an index (int) and return the special tag if
293 # it is so
294 #
295 if isinstance(s, int):
296 return "~%d~" % (s, )
297 else:
298 raise
287 299
288 # Slow path 300 # Slow path
289 re_encode = False 301 re_encode = False
290 if PY2: 302 if PY2:
291 # Use the Unicode translation variant in PY2 303 # Use the Unicode translation variant in PY2
310 ``%uNNNN`` or ``%UNNNNNNNN``. 322 ``%uNNNN`` or ``%UNNNNNNNN``.
311 323
312 This is the inverse of :func:`.quote`. 324 This is the inverse of :func:`.quote`.
313 325
314 """ 326 """
327 s_len = len(s)
328 if s_len > 2 and s[0] == _TILDE and s[-1] == _TILDE:
329 try:
330 v = int(s[1:-1], 10)
331 if v // 10 > 3275: # be compatible to the fast C implementation
332 raise OverflowError("index too large")
333 return v
334 except (ValueError, OverflowError):
335 pass
315 if _QUOTE not in s: 336 if _QUOTE not in s:
316 return s 337 return s
317 parts = s.split(_QUOTE) 338 parts = s.split(_QUOTE)
318 res = [parts[0]] 339 res = [parts[0]]
319 res_append = res.append 340 res_append = res.append