Improve utilities

This commit is contained in:
allegroai 2021-01-05 16:58:57 +02:00
parent e848d05677
commit 9103bf7984
2 changed files with 5 additions and 10 deletions

View File

@ -1,7 +1,5 @@
from boltons.dictutils import OneToOne
from apiserver.apierrors import errors
class ParameterKeyEscaper:
"""
@ -14,11 +12,8 @@ class ParameterKeyEscaper:
_mapping = OneToOne({".": "%2E", "$": "%24", "__": "%_%_"})
@classmethod
def escape(cls, value):
def escape(cls, value: str):
""" Quote a parameter key """
if value is None:
raise errors.bad_request.ValidationError("Key cannot be empty")
value = value.strip().replace("%", "%%")
for c, r in cls._mapping.items():
@ -30,13 +25,13 @@ class ParameterKeyEscaper:
return value
@classmethod
def _unescape(cls, value):
def _unescape(cls, value: str):
for c, r in cls._mapping.inv.items():
value = value.replace(c, r)
return value
@classmethod
def unescape(cls, value):
def unescape(cls, value: str):
""" Unquote a quoted parameter key """
value = "%".join(map(cls._unescape, value.split("%%")))

View File

@ -1,15 +1,15 @@
from functools import wraps
from threading import Lock, Thread
from typing import ClassVar
from typing import ClassVar, Callable
class ThreadsManager:
objects = {}
lock = Lock()
request_context_creator: ClassVar[Callable] = None
terminating: ClassVar[bool] = False
def __init__(self, name=None, **threads):
super(ThreadsManager, self).__init__()
self.name = name or self.__class__.__name__
self.objects = {}
self.lock = Lock()