From ef064299416c6818adabcb22adb3cff205e547a5 Mon Sep 17 00:00:00 2001 From: mathieui Date: Sat, 3 Apr 2021 19:12:59 +0200 Subject: slixmpp.util: type things Fix a bug in the SASL implementation as well. (some special chars would make things crash instead of being escaped) --- slixmpp/util/cache.py | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) (limited to 'slixmpp/util/cache.py') diff --git a/slixmpp/util/cache.py b/slixmpp/util/cache.py index 23592404..b7042a56 100644 --- a/slixmpp/util/cache.py +++ b/slixmpp/util/cache.py @@ -1,4 +1,3 @@ - # Slixmpp: The Slick XMPP Library # Copyright (C) 2018 Emmanuel Gil Peyrot # This file is part of Slixmpp. @@ -6,8 +5,11 @@ import os import logging +from typing import Callable, Optional, Any + log = logging.getLogger(__name__) + class Cache: def retrieve(self, key): raise NotImplementedError @@ -16,7 +18,8 @@ class Cache: raise NotImplementedError def remove(self, key): - raise NotImplemented + raise NotImplementedError + class PerJidCache: def retrieve_by_jid(self, jid, key): @@ -28,6 +31,7 @@ class PerJidCache: def remove_by_jid(self, jid, key): raise NotImplementedError + class MemoryCache(Cache): def __init__(self): self.cache = {} @@ -44,6 +48,7 @@ class MemoryCache(Cache): del self.cache[key] return True + class MemoryPerJidCache(PerJidCache): def __init__(self): self.cache = {} @@ -65,14 +70,15 @@ class MemoryPerJidCache(PerJidCache): del cache[key] return True + class FileSystemStorage: - def __init__(self, encode, decode, binary): + def __init__(self, encode: Optional[Callable[[Any], str]], decode: Optional[Callable[[str], Any]], binary: bool): self.encode = encode if encode is not None else lambda x: x self.decode = decode if decode is not None else lambda x: x self.read = 'rb' if binary else 'r' self.write = 'wb' if binary else 'w' - def _retrieve(self, directory, key): + def _retrieve(self, directory: str, key: str): filename = os.path.join(directory, key.replace('/', '_')) try: with open(filename, self.read) as cache_file: @@ -86,7 +92,7 @@ class FileSystemStorage: log.debug('Removing %s entry', key) self._remove(directory, key) - def _store(self, directory, key, value): + def _store(self, directory: str, key: str, value): filename = os.path.join(directory, key.replace('/', '_')) try: os.makedirs(directory, exist_ok=True) @@ -99,7 +105,7 @@ class FileSystemStorage: except Exception: log.debug('Failed to encode %s to cache:', key, exc_info=True) - def _remove(self, directory, key): + def _remove(self, directory: str, key: str): filename = os.path.join(directory, key.replace('/', '_')) try: os.remove(filename) @@ -108,8 +114,9 @@ class FileSystemStorage: return False return True + class FileSystemCache(Cache, FileSystemStorage): - def __init__(self, directory, cache_type, *, encode=None, decode=None, binary=False): + def __init__(self, directory: str, cache_type: str, *, encode=None, decode=None, binary=False): FileSystemStorage.__init__(self, encode, decode, binary) self.base_dir = os.path.join(directory, cache_type) @@ -122,8 +129,9 @@ class FileSystemCache(Cache, FileSystemStorage): def remove(self, key): return self._remove(self.base_dir, key) + class FileSystemPerJidCache(PerJidCache, FileSystemStorage): - def __init__(self, directory, cache_type, *, encode=None, decode=None, binary=False): + def __init__(self, directory: str, cache_type: str, *, encode=None, decode=None, binary=False): FileSystemStorage.__init__(self, encode, decode, binary) self.base_dir = os.path.join(directory, cache_type) -- cgit v1.2.3