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

View File

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