Created a __dump__ function in BlockDevice. Can be used to safely dump the class into a JSON object. Building a json_encoder which checks if the object hasattr(obj, '__dump__') before calling would be ideal

This commit is contained in:
Anton Hvornum 2020-07-21 08:17:31 +02:00
parent 3c0f4cebce
commit 2b14aa68d0
1 changed files with 15 additions and 7 deletions

View File

@ -17,6 +17,21 @@ class BlockDevice():
self.info = info
self.part_cache = OrderedDict()
def __repr__(self, *args, **kwargs):
return f"BlockDevice({self.device})"
def __getitem__(self, key, *args, **kwargs):
if not key in self.info:
raise KeyError(f'{self} does not contain information: "{key}"')
return self.info[key]
def __dump__(self):
return {
'path' : self.path,
'info' : self.info,
'partition_cache' : self.part_cache
}
@property
def device(self):
"""
@ -69,13 +84,6 @@ class BlockDevice():
all_partitions = self.partitions
return [all_partitions[k] for k in all_partitions]
def __repr__(self, *args, **kwargs):
return f"BlockDevice({self.device})"
def __getitem__(self, key, *args, **kwargs):
if not key in self.info:
raise KeyError(f'{self} does not contain information: "{key}"')
return self.info[key]
class Partition():
def __init__(self, path, part_id=None, size=-1, filesystem=None, mountpoint=None, encrypted=False):