Browse Source

publish muk_utils - 12.0

pull/9/head
MuK IT GmbH 6 years ago
parent
commit
6341803b1e
  1. 54
      muk_utils/tools/cache.py
  2. 4
      muk_utils/tools/file.py
  3. 4
      muk_utils/tools/json.py
  4. 4
      muk_utils/tools/types.py

54
muk_utils/tools/cache.py

@ -19,40 +19,42 @@
import time import time
import logging import logging
import datetime
import functools import functools
_logger = logging.getLogger(__name__) _logger = logging.getLogger(__name__)
class memoize(object):
_caches = {}
_timeouts = {}
#----------------------------------------------------------
# Properties
#----------------------------------------------------------
def __init__(self, timeout=2):
self.timeout = timeout
class cached_property(object):
def collect(self):
for func in self._caches:
cleaned_cache = {}
current_time = time.time()
for key in self._caches[func]:
if (current_time - self._caches[func][key][1]) < self._timeouts[func]:
cleaned_cache[key] = self._caches[func][key]
self._caches[func] = cleaned_cache
def __init__(self, timeout=None):
self.timeout = timeout
def __call__(self, func): def __call__(self, func):
self.cache = self._caches[func] = {}
self._timeouts[func] = self.timeout
@functools.wraps(func)
def wrapper(*args, **kwargs):
current_time = time.time()
kw = sorted(kwargs.items())
key = (args, tuple(kw))
return functools.update_wrapper(self, func)
def __get__(self, obj, cls):
if obj is None:
return self
try: try:
value = self.cache[key]
if (current_time - value[1]) > self.timeout:
raise KeyError
value, last_updated = obj.__dict__[self.__name__]
except KeyError: except KeyError:
value = self.cache[key] = (func(*args,**kwargs), current_time)
return value[0]
return wrapper
pass
else:
if self.timeout is None:
return value
elif self.timeout >= time.time() - last_updated:
return value
value = self.__wrapped__(obj)
obj.__dict__[self.__name__] = (value, time.time())
return value
def __delete__(self, obj):
obj.__dict__.pop(self.__name__, None)
def __set__(self, obj, value):
obj.__dict__[self.__name__] = (value, time())

4
muk_utils/tools/file.py

@ -30,6 +30,10 @@ from odoo.tools.mimetypes import guess_mimetype
_logger = logging.getLogger(__name__) _logger = logging.getLogger(__name__)
#----------------------------------------------------------
# File Helper
#----------------------------------------------------------
def unique_name(name, names, escape_suffix=False): def unique_name(name, names, escape_suffix=False):
def compute_name(name, suffix, escape_suffix): def compute_name(name, suffix, escape_suffix):
if escape_suffix: if escape_suffix:

4
muk_utils/tools/json.py

@ -25,6 +25,10 @@ from odoo import models, tools
_logger = logging.getLogger(__name__) _logger = logging.getLogger(__name__)
#----------------------------------------------------------
# JSON Encoder
#----------------------------------------------------------
class ResponseEncoder(json.JSONEncoder): class ResponseEncoder(json.JSONEncoder):
def default(self, obj): def default(self, obj):

4
muk_utils/tools/types.py

@ -21,6 +21,10 @@ import logging
_logger = logging.getLogger(__name__) _logger = logging.getLogger(__name__)
#----------------------------------------------------------
# Meta Classes
#----------------------------------------------------------
class Singleton(type): class Singleton(type):
_instances = {} _instances = {}

Loading…
Cancel
Save