Proper exceptions, improve print

pull/3/head
Raphaël Vinot 2017-07-25 20:27:58 +02:00
parent 27a2f8e7d9
commit 970b184cde
2 changed files with 20 additions and 4 deletions

View File

@ -24,6 +24,16 @@ class EncodeGalaxies(JSONEncoder):
return JSONEncoder.default(self, obj) 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(): class Galaxy():
def __init__(self, galaxy): def __init__(self, galaxy):
@ -141,6 +151,8 @@ class ClusterValueMeta():
class ClusterValue(): class ClusterValue():
def __init__(self, v): def __init__(self, v):
if not v['value']:
raise PyMISPGalaxiesError("Invalid cluster (no value): {}".format(v))
self.value = v['value'] self.value = v['value']
self.description = v.get('description') self.description = v.get('description')
self.meta = self.__init_meta(v.get('meta')) self.meta = self.__init_meta(v.get('meta'))
@ -159,8 +171,12 @@ class ClusterValue():
return to_return return to_return
def __str__(self): def __str__(self):
# TODO: improve that to_print = '{}\n{}'.format(self.value, self.description)
return '{}\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(): class Cluster():
@ -226,7 +242,7 @@ class Clusters(collections.Mapping):
for v in cluster.values: for v in cluster.values:
if v.value == cluster_value: if v.value == cluster_value:
return cluster, v return cluster, v
return None raise UnableToRevertMachinetag('The machinetag {} could not be found.'.format(machinetag))
def __getitem__(self, name): def __getitem__(self, name):
return self.clusters[name] return self.clusters[name]

@ -1 +1 @@
Subproject commit 3f8b2b4b013f8c8452297dfc8dada045d9462c41 Subproject commit c971b8e9356548cf42fbd6418f203b045704a6eb