mirror of https://github.com/MISP/PyMISP
fix: [Python2] Use LRU cache decorator, fix call to describe_types in PyMISP
parent
7c42a5f748
commit
2785d0027d
|
@ -31,7 +31,7 @@ logger = logging.getLogger('pymisp')
|
||||||
if sys.version_info < (3, 0):
|
if sys.version_info < (3, 0):
|
||||||
from collections import MutableMapping
|
from collections import MutableMapping
|
||||||
import os
|
import os
|
||||||
import cachetools
|
from cachetools import cached, LRUCache
|
||||||
|
|
||||||
resources_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'data')
|
resources_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'data')
|
||||||
misp_objects_path = os.path.join(resources_path, 'misp-objects', 'objects')
|
misp_objects_path = os.path.join(resources_path, 'misp-objects', 'objects')
|
||||||
|
@ -55,24 +55,12 @@ if sys.version_info < (3, 0):
|
||||||
|
|
||||||
class MISPFileCache(object):
|
class MISPFileCache(object):
|
||||||
# cache up to 150 JSON structures in class attribute
|
# cache up to 150 JSON structures in class attribute
|
||||||
__file_cache = cachetools.LFUCache(150)
|
|
||||||
|
|
||||||
@classmethod
|
@staticmethod
|
||||||
def _load_json(cls, path):
|
@cached(cache=LRUCache(maxsize=150))
|
||||||
# use root class attribute as global cache
|
def _load_json(path):
|
||||||
file_cache = cls.__file_cache
|
with open(path, 'r') as f:
|
||||||
# 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)
|
data = load(f)
|
||||||
file_cache[path] = (mtime, data)
|
|
||||||
return data
|
return data
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
@ -91,15 +79,10 @@ else:
|
||||||
@staticmethod
|
@staticmethod
|
||||||
@lru_cache(maxsize=150)
|
@lru_cache(maxsize=150)
|
||||||
def _load_json(path):
|
def _load_json(path):
|
||||||
with path.open('rb') as f:
|
with path.open('r') as f:
|
||||||
data = load(f)
|
data = load(f)
|
||||||
return data
|
return data
|
||||||
|
|
||||||
if (3, 0) <= sys.version_info < (3, 6):
|
|
||||||
OLD_PY3 = True
|
|
||||||
else:
|
|
||||||
OLD_PY3 = False
|
|
||||||
|
|
||||||
|
|
||||||
class Distribution(Enum):
|
class Distribution(Enum):
|
||||||
your_organisation_only = 0
|
your_organisation_only = 0
|
||||||
|
|
|
@ -144,7 +144,7 @@ class PyMISP(MISPFileCache): # pragma: no cover
|
||||||
if remote_describe_types.get('error'):
|
if remote_describe_types.get('error'):
|
||||||
for e in remote_describe_types.get('error'):
|
for e in remote_describe_types.get('error'):
|
||||||
raise PyMISPError('Failed: {}'.format(e))
|
raise PyMISPError('Failed: {}'.format(e))
|
||||||
remote_describe_types = describe_types['result']
|
remote_describe_types = describe_types
|
||||||
if not remote_describe_types.get('sane_defaults'):
|
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.')
|
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
|
return remote_describe_types
|
||||||
|
|
|
@ -6,7 +6,7 @@ try:
|
||||||
except ImportError as e:
|
except ImportError as e:
|
||||||
print(e)
|
print(e)
|
||||||
url = 'https://localhost:8443'
|
url = 'https://localhost:8443'
|
||||||
key = 'K5yV0CcxdnklzDfCKlnPniIxrMX41utQ2dG13zZ3'
|
key = 'd6OmdDFvU3Seau3UjwvHS1y3tFQbaRNhJhDX0tjh'
|
||||||
|
|
||||||
import time
|
import time
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue