Browse Source

publish muk_utils - 12.0

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

60
muk_utils/tools/cache.py

@ -19,40 +19,42 @@
import time
import logging
import datetime
import functools
_logger = logging.getLogger(__name__)
class memoize(object):
_caches = {}
_timeouts = {}
def __init__(self, timeout=2):
self.timeout = timeout
#----------------------------------------------------------
# Properties
#----------------------------------------------------------
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):
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))
try:
value = self.cache[key]
if (current_time - value[1]) > self.timeout:
raise KeyError
except KeyError:
value = self.cache[key] = (func(*args,**kwargs), current_time)
return value[0]
return wrapper
return functools.update_wrapper(self, func)
def __get__(self, obj, cls):
if obj is None:
return self
try:
value, last_updated = obj.__dict__[self.__name__]
except KeyError:
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())

6
muk_utils/tools/file.py

@ -30,6 +30,10 @@ from odoo.tools.mimetypes import guess_mimetype
_logger = logging.getLogger(__name__)
#----------------------------------------------------------
# File Helper
#----------------------------------------------------------
def unique_name(name, names, escape_suffix=False):
def compute_name(name, suffix, escape_suffix):
if escape_suffix:
@ -46,7 +50,7 @@ def unique_name(name, names, escape_suffix=False):
suffix += 1
name = compute_name(name, suffix, escape_suffix)
return name
def guess_extension(filename=None, mimetype=None, binary=None):
extension = filename and os.path.splitext(filename)[1][1:].strip().lower()
if not extension and mimetype:

4
muk_utils/tools/json.py

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

4
muk_utils/tools/types.py

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

Loading…
Cancel
Save