From 970b184cdeddc8f349ad4976c19b8efa07701e7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Vinot?= Date: Tue, 25 Jul 2017 20:27:58 +0200 Subject: [PATCH] Proper exceptions, improve print --- pymispgalaxies/api.py | 22 +++++++++++++++++++--- pymispgalaxies/data/misp-galaxy | 2 +- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/pymispgalaxies/api.py b/pymispgalaxies/api.py index 2655946..db93743 100644 --- a/pymispgalaxies/api.py +++ b/pymispgalaxies/api.py @@ -24,6 +24,16 @@ class EncodeGalaxies(JSONEncoder): return JSONEncoder.default(self, obj) +class PyMISPGalaxiesError(Exception): + def __init__(self, message): + super(PyMISPGalaxiesError, self).__init__(message) + self.message = message + + +class UnableToRevertMachinetag(PyMISPGalaxiesError): + pass + + class Galaxy(): def __init__(self, galaxy): @@ -141,6 +151,8 @@ class ClusterValueMeta(): class ClusterValue(): def __init__(self, v): + if not v['value']: + raise PyMISPGalaxiesError("Invalid cluster (no value): {}".format(v)) self.value = v['value'] self.description = v.get('description') self.meta = self.__init_meta(v.get('meta')) @@ -159,8 +171,12 @@ class ClusterValue(): return to_return def __str__(self): - # TODO: improve that - return '{}\n{}'.format(self.value, self.description) + to_print = '{}\n{}'.format(self.value, self.description) + if self.meta: + to_print += '\n' + for k, v in self.meta._json().items(): + to_print += '- {}:\t{}'.format(k, v) + return to_print class Cluster(): @@ -226,7 +242,7 @@ class Clusters(collections.Mapping): for v in cluster.values: if v.value == cluster_value: return cluster, v - return None + raise UnableToRevertMachinetag('The machinetag {} could not be found.'.format(machinetag)) def __getitem__(self, name): return self.clusters[name] diff --git a/pymispgalaxies/data/misp-galaxy b/pymispgalaxies/data/misp-galaxy index 3f8b2b4..c971b8e 160000 --- a/pymispgalaxies/data/misp-galaxy +++ b/pymispgalaxies/data/misp-galaxy @@ -1 +1 @@ -Subproject commit 3f8b2b4b013f8c8452297dfc8dada045d9462c41 +Subproject commit c971b8e9356548cf42fbd6418f203b045704a6eb