mirror of https://github.com/muk-it/muk_base
MuK IT GmbH
6 years ago
8 changed files with 296 additions and 109 deletions
-
2muk_utils/__manifest__.py
-
137muk_utils/i18n/de.po
-
100muk_utils/i18n/muk_utils.pot
-
2muk_utils/models/res_groups.py
-
3muk_utils/tools/__init__.py
-
53muk_utils/tools/cache.py
-
57muk_utils/tools/file.py
-
45muk_utils/tools/json.py
@ -0,0 +1,53 @@ |
|||
################################################################################### |
|||
# |
|||
# Copyright (C) 2018 MuK IT GmbH |
|||
# |
|||
# This program is free software: you can redistribute it and/or modify |
|||
# it under the terms of the GNU Affero General Public License as |
|||
# published by the Free Software Foundation, either version 3 of the |
|||
# License, or (at your option) any later version. |
|||
# |
|||
# This program is distributed in the hope that it will be useful, |
|||
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
# GNU Affero General Public License for more details. |
|||
# |
|||
# You should have received a copy of the GNU Affero General Public License |
|||
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
|||
# |
|||
################################################################################### |
|||
|
|||
import time |
|||
|
|||
class memoize(object): |
|||
_caches = {} |
|||
_timeouts = {} |
|||
|
|||
def __init__(self, timeout=2): |
|||
self.timeout = timeout |
|||
|
|||
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 __call__(self, func): |
|||
self.cache = self._caches[func] = {} |
|||
self._timeouts[func] = self.timeout |
|||
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] |
|||
wrapper.func_name = func.__name__ |
|||
return wrapper |
@ -0,0 +1,57 @@ |
|||
################################################################################### |
|||
# |
|||
# Copyright (C) 2018 MuK IT GmbH |
|||
# |
|||
# This program is free software: you can redistribute it and/or modify |
|||
# it under the terms of the GNU Affero General Public License as |
|||
# published by the Free Software Foundation, either version 3 of the |
|||
# License, or (at your option) any later version. |
|||
# |
|||
# This program is distributed in the hope that it will be useful, |
|||
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
# GNU Affero General Public License for more details. |
|||
# |
|||
# You should have received a copy of the GNU Affero General Public License |
|||
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
|||
# |
|||
################################################################################### |
|||
|
|||
import os |
|||
import io |
|||
import base64 |
|||
import shutil |
|||
import urllib |
|||
import logging |
|||
import tempfile |
|||
import mimetypes |
|||
|
|||
from odoo.tools.mimetypes import guess_mimetype |
|||
|
|||
_logger = logging.getLogger(__name__) |
|||
|
|||
def unique_name(name, names, escape_suffix=False): |
|||
def compute_name(name, suffix, escape_suffix): |
|||
if escape_suffix: |
|||
name, extension = os.path.splitext(name) |
|||
return "%s(%s)%s" % (name, suffix, extension) |
|||
else: |
|||
return "%s(%s)" % (name, suffix) |
|||
if not name in names: |
|||
return name |
|||
else: |
|||
suffix = 1 |
|||
name = compute_name(name, suffix, escape_suffix) |
|||
while name in names: |
|||
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: |
|||
extension = mimetypes.guess_extension(mimetype)[1:].strip().lower() |
|||
if not extension and binary: |
|||
mimetype = guess_mimetype(binary, default="") |
|||
extension = mimetypes.guess_extension(mimetype)[1:].strip().lower() |
|||
return extension |
@ -0,0 +1,45 @@ |
|||
################################################################################### |
|||
# |
|||
# Copyright (C) 2018 MuK IT GmbH |
|||
# |
|||
# This program is free software: you can redistribute it and/or modify |
|||
# it under the terms of the GNU Affero General Public License as |
|||
# published by the Free Software Foundation, either version 3 of the |
|||
# License, or (at your option) any later version. |
|||
# |
|||
# This program is distributed in the hope that it will be useful, |
|||
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
# GNU Affero General Public License for more details. |
|||
# |
|||
# You should have received a copy of the GNU Affero General Public License |
|||
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
|||
# |
|||
################################################################################### |
|||
|
|||
import json |
|||
import logging |
|||
import datetime |
|||
|
|||
from odoo import models, tools |
|||
|
|||
_logger = logging.getLogger(__name__) |
|||
|
|||
class ResponseEncoder(json.JSONEncoder): |
|||
|
|||
def default(self, obj): |
|||
if isinstance(obj, datetime.date): |
|||
return obj.strftime(tools.DEFAULT_SERVER_DATE_FORMAT) |
|||
if isinstance(obj, datetime.datetime): |
|||
return obj.strftime(tools.DEFAULT_SERVER_DATETIME_FORMAT) |
|||
if isinstance(obj, (bytes, bytearray)): |
|||
return obj.decode() |
|||
return json.JSONEncoder.default(self, obj) |
|||
|
|||
class RecordEncoder(ResponseEncoder): |
|||
|
|||
def default(self, obj): |
|||
if isinstance(obj, models.BaseModel): |
|||
return obj.name_get() |
|||
return ResponseEncoder.default(self, obj) |
|||
|
Write
Preview
Loading…
Cancel
Save
Reference in new issue