2022-01-24 04:07:52 +00:00
|
|
|
import abc
|
2018-09-17 00:27:00 +00:00
|
|
|
import collections
|
2022-01-24 04:07:52 +00:00
|
|
|
import re
|
|
|
|
import threading
|
|
|
|
from typing import MutableMapping
|
|
|
|
from typing import MutableSet
|
|
|
|
|
|
|
|
import stevedore
|
2018-09-17 00:27:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
def coerce_string_conf(d):
|
|
|
|
result = {}
|
|
|
|
for k, v in d.items():
|
2022-01-24 04:07:52 +00:00
|
|
|
if not isinstance(v, str):
|
2018-09-17 00:27:00 +00:00
|
|
|
result[k] = v
|
|
|
|
continue
|
|
|
|
|
|
|
|
v = v.strip()
|
2022-01-24 04:07:52 +00:00
|
|
|
if re.match(r"^[-+]?\d+$", v):
|
2018-09-17 00:27:00 +00:00
|
|
|
result[k] = int(v)
|
2022-01-24 04:07:52 +00:00
|
|
|
elif re.match(r"^[-+]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][-+]?\d+)?$", v):
|
2018-09-17 00:27:00 +00:00
|
|
|
result[k] = float(v)
|
2022-01-24 04:07:52 +00:00
|
|
|
elif v.lower() in ("false", "true"):
|
|
|
|
result[k] = v.lower() == "true"
|
|
|
|
elif v == "None":
|
2018-09-17 00:27:00 +00:00
|
|
|
result[k] = None
|
|
|
|
else:
|
|
|
|
result[k] = v
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
class PluginLoader:
|
2018-09-17 00:27:00 +00:00
|
|
|
def __init__(self, group):
|
|
|
|
self.group = group
|
2022-01-24 04:07:52 +00:00
|
|
|
self.impls = {} # loaded plugins
|
|
|
|
self._mgr = None # lazily defined stevedore manager
|
|
|
|
self._unloaded = {} # plugins registered but not loaded
|
2018-09-17 00:27:00 +00:00
|
|
|
|
|
|
|
def load(self, name):
|
2022-01-24 04:07:52 +00:00
|
|
|
if name in self._unloaded:
|
|
|
|
self.impls[name] = self._unloaded[name]()
|
|
|
|
return self.impls[name]
|
2018-09-17 00:27:00 +00:00
|
|
|
if name in self.impls:
|
2022-01-24 04:07:52 +00:00
|
|
|
return self.impls[name]
|
2018-09-17 00:27:00 +00:00
|
|
|
else: # pragma NO COVERAGE
|
2022-01-24 04:07:52 +00:00
|
|
|
if self._mgr is None:
|
|
|
|
self._mgr = stevedore.ExtensionManager(self.group)
|
|
|
|
try:
|
|
|
|
self.impls[name] = self._mgr[name].plugin
|
|
|
|
return self.impls[name]
|
|
|
|
except KeyError:
|
2018-09-17 00:27:00 +00:00
|
|
|
raise self.NotFound(
|
|
|
|
"Can't load plugin %s %s" % (self.group, name)
|
|
|
|
)
|
|
|
|
|
|
|
|
def register(self, name, modulepath, objname):
|
|
|
|
def load():
|
|
|
|
mod = __import__(modulepath, fromlist=[objname])
|
|
|
|
return getattr(mod, objname)
|
2022-01-24 04:07:52 +00:00
|
|
|
|
|
|
|
self._unloaded[name] = load
|
2018-09-17 00:27:00 +00:00
|
|
|
|
|
|
|
class NotFound(Exception):
|
|
|
|
"""The specified plugin could not be found."""
|
|
|
|
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
class memoized_property:
|
2018-09-17 00:27:00 +00:00
|
|
|
"""A read-only @property that is only evaluated once."""
|
2022-01-24 04:07:52 +00:00
|
|
|
|
2018-09-17 00:27:00 +00:00
|
|
|
def __init__(self, fget, doc=None):
|
|
|
|
self.fget = fget
|
|
|
|
self.__doc__ = doc or fget.__doc__
|
|
|
|
self.__name__ = fget.__name__
|
|
|
|
|
|
|
|
def __get__(self, obj, cls):
|
|
|
|
if obj is None:
|
|
|
|
return self
|
|
|
|
obj.__dict__[self.__name__] = result = self.fget(obj)
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
def to_list(x, default=None):
|
|
|
|
"""Coerce to a list."""
|
|
|
|
if x is None:
|
|
|
|
return default
|
|
|
|
if not isinstance(x, (list, tuple)):
|
|
|
|
return [x]
|
|
|
|
else:
|
|
|
|
return x
|
|
|
|
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
class Mutex(abc.ABC):
|
|
|
|
@abc.abstractmethod
|
|
|
|
def acquire(self, wait: bool = True) -> bool:
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
|
|
|
def release(self) -> None:
|
|
|
|
raise NotImplementedError()
|
2018-09-17 00:27:00 +00:00
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
|
|
|
|
class KeyReentrantMutex:
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
key: str,
|
|
|
|
mutex: Mutex,
|
|
|
|
keys: MutableMapping[int, MutableSet[str]],
|
|
|
|
):
|
2018-09-17 00:27:00 +00:00
|
|
|
self.key = key
|
|
|
|
self.mutex = mutex
|
|
|
|
self.keys = keys
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def factory(cls, mutex):
|
|
|
|
# this collection holds zero or one
|
|
|
|
# thread idents as the key; a set of
|
|
|
|
# keynames held as the value.
|
2022-01-24 04:07:52 +00:00
|
|
|
keystore: MutableMapping[
|
|
|
|
int, MutableSet[str]
|
|
|
|
] = collections.defaultdict(set)
|
2018-09-17 00:27:00 +00:00
|
|
|
|
|
|
|
def fac(key):
|
|
|
|
return KeyReentrantMutex(key, mutex, keystore)
|
2022-01-24 04:07:52 +00:00
|
|
|
|
2018-09-17 00:27:00 +00:00
|
|
|
return fac
|
|
|
|
|
|
|
|
def acquire(self, wait=True):
|
2022-01-24 04:07:52 +00:00
|
|
|
current_thread = threading.get_ident()
|
2018-09-17 00:27:00 +00:00
|
|
|
keys = self.keys.get(current_thread)
|
2022-01-24 04:07:52 +00:00
|
|
|
if keys is not None and self.key not in keys:
|
2018-09-17 00:27:00 +00:00
|
|
|
# current lockholder, new key. add it in
|
|
|
|
keys.add(self.key)
|
|
|
|
return True
|
|
|
|
elif self.mutex.acquire(wait=wait):
|
|
|
|
# after acquire, create new set and add our key
|
|
|
|
self.keys[current_thread].add(self.key)
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def release(self):
|
2022-01-24 04:07:52 +00:00
|
|
|
current_thread = threading.get_ident()
|
2018-09-17 00:27:00 +00:00
|
|
|
keys = self.keys.get(current_thread)
|
|
|
|
assert keys is not None, "this thread didn't do the acquire"
|
|
|
|
assert self.key in keys, "No acquire held for key '%s'" % self.key
|
|
|
|
keys.remove(self.key)
|
|
|
|
if not keys:
|
|
|
|
# when list of keys empty, remove
|
|
|
|
# the thread ident and unlock.
|
|
|
|
del self.keys[current_thread]
|
|
|
|
self.mutex.release()
|
2022-01-24 04:07:52 +00:00
|
|
|
|
|
|
|
def locked(self):
|
|
|
|
current_thread = threading.get_ident()
|
|
|
|
keys = self.keys.get(current_thread)
|
|
|
|
if keys is None:
|
|
|
|
return False
|
|
|
|
return self.key in keys
|