fix: [Python2] Use LRU cache decorator, fix call to describe_types in PyMISP

pull/470/head
Raphaël Vinot 2019-10-10 10:15:23 +02:00
parent 7c42a5f748
commit 2785d0027d
3 changed files with 9 additions and 26 deletions

View File

@ -31,7 +31,7 @@ logger = logging.getLogger('pymisp')
if sys.version_info < (3, 0):
from collections import MutableMapping
import os
import cachetools
from cachetools import cached, LRUCache
resources_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'data')
misp_objects_path = os.path.join(resources_path, 'misp-objects', 'objects')
@ -55,24 +55,12 @@ if sys.version_info < (3, 0):
class MISPFileCache(object):
# cache up to 150 JSON structures in class attribute
__file_cache = cachetools.LFUCache(150)
@classmethod
def _load_json(cls, path):
# use root class attribute as global cache
file_cache = cls.__file_cache
# use modified time with path as cache key
mtime = os.path.getmtime(path)
if path in file_cache:
ctime, data = file_cache[path]
if ctime == mtime:
return data
with open(path, 'rb') as f:
if OLD_PY3:
data = loads(f.read().decode())
else:
data = load(f)
file_cache[path] = (mtime, data)
@staticmethod
@cached(cache=LRUCache(maxsize=150))
def _load_json(path):
with open(path, 'r') as f:
data = load(f)
return data
else:
@ -91,15 +79,10 @@ else:
@staticmethod
@lru_cache(maxsize=150)
def _load_json(path):
with path.open('rb') as f:
with path.open('r') as f:
data = load(f)
return data
if (3, 0) <= sys.version_info < (3, 6):
OLD_PY3 = True
else:
OLD_PY3 = False
class Distribution(Enum):
your_organisation_only = 0

View File

@ -144,7 +144,7 @@ class PyMISP(MISPFileCache): # pragma: no cover
if remote_describe_types.get('error'):
for e in remote_describe_types.get('error'):
raise PyMISPError('Failed: {}'.format(e))
remote_describe_types = describe_types['result']
remote_describe_types = describe_types
if not remote_describe_types.get('sane_defaults'):
raise PyMISPError('The MISP server your are trying to reach is outdated (<2.4.52). Please use PyMISP v2.4.51.1 (pip install -I PyMISP==v2.4.51.1) and/or contact your administrator.')
return remote_describe_types

View File

@ -6,7 +6,7 @@ try:
except ImportError as e:
print(e)
url = 'https://localhost:8443'
key = 'K5yV0CcxdnklzDfCKlnPniIxrMX41utQ2dG13zZ3'
key = 'd6OmdDFvU3Seau3UjwvHS1y3tFQbaRNhJhDX0tjh'
import time