Unify json functions (#2102)

Co-authored-by: Daniel Girtler <girtler.daniel@gmail.com>
This commit is contained in:
Daniel Girtler 2023-09-24 18:14:02 +10:00 committed by GitHub
parent 877e34de63
commit 360a1b4f33
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 29 additions and 31 deletions

View File

@ -49,10 +49,10 @@ class DiskLayoutConfiguration:
if self.config_type == DiskLayoutType.Pre_mount and self.relative_mountpoint is None: if self.config_type == DiskLayoutType.Pre_mount and self.relative_mountpoint is None:
raise ValueError('Must set a relative mountpoint when layout type is pre-mount"') raise ValueError('Must set a relative mountpoint when layout type is pre-mount"')
def __dump__(self) -> Dict[str, Any]: def json(self) -> Dict[str, Any]:
return { return {
'config_type': self.config_type.value, 'config_type': self.config_type.value,
'device_modifications': [mod.__dump__() for mod in self.device_modifications] 'device_modifications': [mod.json() for mod in self.device_modifications]
} }
@classmethod @classmethod
@ -171,12 +171,12 @@ class Size:
raise ValueError('Percent unit size must specify a total size') raise ValueError('Percent unit size must specify a total size')
return self.total_size # type: ignore return self.total_size # type: ignore
def __dump__(self) -> Dict[str, Any]: def json(self) -> Dict[str, Any]:
return { return {
'value': self.value, 'value': self.value,
'unit': self.unit.name, 'unit': self.unit.name,
'sector_size': self.sector_size.__dump__() if self.sector_size else None, 'sector_size': self.sector_size.json() if self.sector_size else None,
'total_size': self._total_size.__dump__() if self._total_size else None 'total_size': self._total_size.json() if self._total_size else None
} }
@classmethod @classmethod
@ -457,7 +457,7 @@ class SubvolumeModification:
return self.mountpoint == Path('/') return self.mountpoint == Path('/')
return False return False
def __dump__(self) -> Dict[str, Any]: def json(self) -> Dict[str, Any]:
return { return {
'name': str(self.name), 'name': str(self.name),
'mountpoint': str(self.mountpoint), 'mountpoint': str(self.mountpoint),
@ -466,12 +466,7 @@ class SubvolumeModification:
} }
def table_data(self) -> Dict[str, Any]: def table_data(self) -> Dict[str, Any]:
return { return self.json()
'name': str(self.name),
'mountpoint': str(self.mountpoint),
'compress': self.compress,
'nodatacow': self.nodatacow
}
class DeviceGeometry: class DeviceGeometry:
@ -755,14 +750,14 @@ class PartitionModification:
'obj_id': self.obj_id, 'obj_id': self.obj_id,
'status': self.status.value, 'status': self.status.value,
'type': self.type.value, 'type': self.type.value,
'start': self.start.__dump__(), 'start': self.start.json(),
'length': self.length.__dump__(), 'length': self.length.json(),
'fs_type': self.fs_type.value if self.fs_type else '', 'fs_type': self.fs_type.value if self.fs_type else '',
'mountpoint': str(self.mountpoint) if self.mountpoint else None, 'mountpoint': str(self.mountpoint) if self.mountpoint else None,
'mount_options': self.mount_options, 'mount_options': self.mount_options,
'flags': [f.name for f in self.flags], 'flags': [f.name for f in self.flags],
'dev_path': str(self.dev_path) if self.dev_path else None, 'dev_path': str(self.dev_path) if self.dev_path else None,
'btrfs': [vol.__dump__() for vol in self.btrfs_subvols] 'btrfs': [vol.json() for vol in self.btrfs_subvols]
} }
def table_data(self) -> Dict[str, Any]: def table_data(self) -> Dict[str, Any]:
@ -830,7 +825,7 @@ class DeviceModification:
filtered = filter(lambda x: x.is_root(relative_path), self.partitions) filtered = filter(lambda x: x.is_root(relative_path), self.partitions)
return next(filtered, None) return next(filtered, None)
def __dump__(self) -> Dict[str, Any]: def json(self) -> Dict[str, Any]:
""" """
Called when generating configuration files Called when generating configuration files
""" """
@ -922,6 +917,13 @@ class Fido2Device:
'product': self.product 'product': self.product
} }
def table_data(self) -> Dict[str, str]:
return {
'Path': str(self.path),
'Manufacturer': self.manufacturer,
'Product': self.product
}
@classmethod @classmethod
def parse_arg(cls, arg: Dict[str, str]) -> 'Fido2Device': def parse_arg(cls, arg: Dict[str, str]) -> 'Fido2Device':
return Fido2Device( return Fido2Device(

View File

@ -69,8 +69,6 @@ def jsonify(obj: Any, safe: bool = True) -> Any:
# a dictionary representation of the object so that it can be # a dictionary representation of the object so that it can be
# processed by the json library. # processed by the json library.
return jsonify(obj.json(), safe) return jsonify(obj.json(), safe)
if hasattr(obj, '__dump__'):
return obj.__dump__()
if isinstance(obj, (datetime, date)): if isinstance(obj, (datetime, date)):
return obj.isoformat() return obj.isoformat()
if isinstance(obj, (list, set, tuple)): if isinstance(obj, (list, set, tuple)):

View File

@ -24,7 +24,7 @@ class Audio(Enum):
class AudioConfiguration: class AudioConfiguration:
audio: Audio audio: Audio
def __dump__(self) -> Dict[str, Any]: def json(self) -> Dict[str, Any]:
return { return {
'audio': self.audio.value 'audio': self.audio.value
} }

View File

@ -14,12 +14,12 @@ class Bootloader(Enum):
Efistub = 'Efistub' Efistub = 'Efistub'
Limine = 'Limine' Limine = 'Limine'
def json(self): def json(self) -> str:
return self.value return self.value
@classmethod @staticmethod
def values(cls) -> List[str]: def values() -> List[str]:
return [e.value for e in cls] return [e.value for e in Bootloader]
@classmethod @classmethod
def get_default(cls) -> Bootloader: def get_default(cls) -> Bootloader:

View File

@ -42,7 +42,7 @@ class Nic:
'dns': self.dns 'dns': self.dns
} }
def __dump__(self) -> Dict[str, Any]: def json(self) -> Dict[str, Any]:
return { return {
'iface': self.iface, 'iface': self.iface,
'ip': self.ip, 'ip': self.ip,
@ -94,10 +94,10 @@ class NetworkConfiguration:
type: NicType type: NicType
nics: List[Nic] = field(default_factory=list) nics: List[Nic] = field(default_factory=list)
def __dump__(self) -> Dict[str, Any]: def json(self) -> Dict[str, Any]:
config: Dict[str, Any] = {'type': self.type.value} config: Dict[str, Any] = {'type': self.type.value}
if self.nics: if self.nics:
config['nics'] = [n.__dump__() for n in self.nics] config['nics'] = [n.json() for n in self.nics]
return config return config

View File

@ -38,8 +38,6 @@ class FormattedOutput:
raise ValueError('Unsupported formatting call') raise ValueError('Unsupported formatting call')
elif hasattr(o, 'table_data'): elif hasattr(o, 'table_data'):
return o.table_data() return o.table_data()
elif hasattr(o, 'json'):
return o.json()
elif is_dataclass(o): elif is_dataclass(o):
return asdict(o) return asdict(o)
else: else: