From d76c6082ab65207af94a4e0b0b2d6510c1152030 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Vinot?= Date: Fri, 23 Feb 2018 13:46:48 +0100 Subject: [PATCH] chg: allow to pass a list of galaxies as parameter --- pymispgalaxies/api.py | 34 ++++++++++++++++++++------------- pymispgalaxies/data/misp-galaxy | 2 +- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/pymispgalaxies/api.py b/pymispgalaxies/api.py index 054264e..4bd4bd2 100644 --- a/pymispgalaxies/api.py +++ b/pymispgalaxies/api.py @@ -22,6 +22,7 @@ class EncodeGalaxies(JSONEncoder): return obj.to_dict() return JSONEncoder.default(self, obj) + class EncodeClusters(JSONEncoder): def default(self, obj): if isinstance(obj, (Cluster, ClusterValue, ClusterValueMeta)): @@ -60,13 +61,17 @@ class Galaxy(): class Galaxies(collections.Mapping): - def __init__(self): - self.root_dir_galaxies = os.path.join(os.path.abspath(os.path.dirname(sys.modules['pymispgalaxies'].__file__)), - 'data', 'misp-galaxy', 'galaxies') + def __init__(self, galaxies=[]): + if not galaxies: + galaxies = [] + self.root_dir_galaxies = os.path.join(os.path.abspath(os.path.dirname(sys.modules['pymispgalaxies'].__file__)), + 'data', 'misp-galaxy', 'galaxies') + for galaxy_file in glob(os.path.join(self.root_dir_galaxies, '*.json')): + with open(galaxy_file, 'r') as f: + galaxies.append(json.load(f)) + self.galaxies = {} - for galaxy_file in glob(os.path.join(self.root_dir_galaxies, '*.json')): - with open(galaxy_file, 'r') as f: - galaxy = json.load(f) + for galaxy in galaxies: self.galaxies[galaxy['name']] = Galaxy(galaxy) def validate_with_schema(self): @@ -239,13 +244,16 @@ class Cluster(collections.Mapping): class Clusters(collections.Mapping): - def __init__(self): - self.root_dir_clusters = os.path.join(os.path.abspath(os.path.dirname(sys.modules['pymispgalaxies'].__file__)), - 'data', 'misp-galaxy', 'clusters') + def __init__(self, clusters=[]): + if not clusters: + clusters = [] + self.root_dir_clusters = os.path.join(os.path.abspath(os.path.dirname(sys.modules['pymispgalaxies'].__file__)), + 'data', 'misp-galaxy', 'clusters') + for cluster_file in glob(os.path.join(self.root_dir_clusters, '*.json')): + with open(cluster_file, 'r') as f: + clusters.append(json.load(f)) self.clusters = {} - for cluster_file in glob(os.path.join(self.root_dir_clusters, '*.json')): - with open(cluster_file, 'r') as f: - cluster = json.load(f) + for cluster in clusters: self.clusters[cluster['type']] = Cluster(cluster) def validate_with_schema(self): @@ -267,7 +275,7 @@ class Clusters(collections.Mapping): cluster = self.get(cluster_type) value = cluster[cluster_value] return cluster, value - except: + except Exception: raise UnableToRevertMachinetag('The machinetag {} could not be found.'.format(machinetag)) def search(self, query): diff --git a/pymispgalaxies/data/misp-galaxy b/pymispgalaxies/data/misp-galaxy index 9cfba28..0eb6ab4 160000 --- a/pymispgalaxies/data/misp-galaxy +++ b/pymispgalaxies/data/misp-galaxy @@ -1 +1 @@ -Subproject commit 9cfba28d3612621943b282ca434b26845f011c55 +Subproject commit 0eb6ab483280c943bfb3a6c19ce5ba1320f25c83