Skip to content

Commit

Permalink
Unify json functions (#2102)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel Girtler <girtler.daniel@gmail.com>
  • Loading branch information
svartkanin and svartkanin committed Sep 24, 2023
1 parent 877e34d commit 360a1b4
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 31 deletions.
34 changes: 18 additions & 16 deletions archinstall/lib/disk/device_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ def __post_init__(self):
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"')

def __dump__(self) -> Dict[str, Any]:
def json(self) -> Dict[str, Any]:
return {
'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
Expand Down Expand Up @@ -171,12 +171,12 @@ def _total_size(self) -> Size:
raise ValueError('Percent unit size must specify a total size')
return self.total_size # type: ignore

def __dump__(self) -> Dict[str, Any]:
def json(self) -> Dict[str, Any]:
return {
'value': self.value,
'unit': self.unit.name,
'sector_size': self.sector_size.__dump__() if self.sector_size else None,
'total_size': self._total_size.__dump__() if self._total_size else None
'sector_size': self.sector_size.json() if self.sector_size else None,
'total_size': self._total_size.json() if self._total_size else None
}

@classmethod
Expand Down Expand Up @@ -457,7 +457,7 @@ def is_root(self, relative_mountpoint: Optional[Path] = None) -> bool:
return self.mountpoint == Path('/')
return False

def __dump__(self) -> Dict[str, Any]:
def json(self) -> Dict[str, Any]:
return {
'name': str(self.name),
'mountpoint': str(self.mountpoint),
Expand All @@ -466,12 +466,7 @@ def __dump__(self) -> Dict[str, Any]:
}

def table_data(self) -> Dict[str, Any]:
return {
'name': str(self.name),
'mountpoint': str(self.mountpoint),
'compress': self.compress,
'nodatacow': self.nodatacow
}
return self.json()


class DeviceGeometry:
Expand Down Expand Up @@ -755,14 +750,14 @@ def json(self) -> Dict[str, Any]:
'obj_id': self.obj_id,
'status': self.status.value,
'type': self.type.value,
'start': self.start.__dump__(),
'length': self.length.__dump__(),
'start': self.start.json(),
'length': self.length.json(),
'fs_type': self.fs_type.value if self.fs_type else '',
'mountpoint': str(self.mountpoint) if self.mountpoint else None,
'mount_options': self.mount_options,
'flags': [f.name for f in self.flags],
'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]:
Expand Down Expand Up @@ -830,7 +825,7 @@ def get_root_partition(self, relative_path: Optional[Path]) -> Optional[Partitio
filtered = filter(lambda x: x.is_root(relative_path), self.partitions)
return next(filtered, None)

def __dump__(self) -> Dict[str, Any]:
def json(self) -> Dict[str, Any]:
"""
Called when generating configuration files
"""
Expand Down Expand Up @@ -922,6 +917,13 @@ def json(self) -> Dict[str, str]:
'product': self.product
}

def table_data(self) -> Dict[str, str]:
return {
'Path': str(self.path),
'Manufacturer': self.manufacturer,
'Product': self.product
}

@classmethod
def parse_arg(cls, arg: Dict[str, str]) -> 'Fido2Device':
return Fido2Device(
Expand Down
8 changes: 3 additions & 5 deletions archinstall/lib/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ def jsonify(obj: Any, safe: bool = True) -> Any:
# a dictionary representation of the object so that it can be
# processed by the json library.
return jsonify(obj.json(), safe)
if hasattr(obj, '__dump__'):
return obj.__dump__()
if isinstance(obj, (datetime, date)):
return obj.isoformat()
if isinstance(obj, (list, set, tuple)):
Expand Down Expand Up @@ -462,13 +460,13 @@ def run_custom_user_commands(commands :List[str], installation :Installer) -> No
for index, command in enumerate(commands):
script_path = f"/var/tmp/user-command.{index}.sh"
chroot_path = f"{installation.target}/{script_path}"

info(f'Executing custom command "{command}" ...')
with open(chroot_path, "w") as user_script:
user_script.write(command)

SysCommand(f"arch-chroot {installation.target} bash {script_path}")

os.unlink(chroot_path)


Expand Down
2 changes: 1 addition & 1 deletion archinstall/lib/models/audio_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def no_audio_text() -> str:
class AudioConfiguration:
audio: Audio

def __dump__(self) -> Dict[str, Any]:
def json(self) -> Dict[str, Any]:
return {
'audio': self.audio.value
}
Expand Down
8 changes: 4 additions & 4 deletions archinstall/lib/models/bootloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ class Bootloader(Enum):
Efistub = 'Efistub'
Limine = 'Limine'

def json(self):
def json(self) -> str:
return self.value

@classmethod
def values(cls) -> List[str]:
return [e.value for e in cls]
@staticmethod
def values() -> List[str]:
return [e.value for e in Bootloader]

@classmethod
def get_default(cls) -> Bootloader:
Expand Down
6 changes: 3 additions & 3 deletions archinstall/lib/models/network_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def table_data(self) -> Dict[str, Any]:
'dns': self.dns
}

def __dump__(self) -> Dict[str, Any]:
def json(self) -> Dict[str, Any]:
return {
'iface': self.iface,
'ip': self.ip,
Expand Down Expand Up @@ -94,10 +94,10 @@ class NetworkConfiguration:
type: NicType
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}
if self.nics:
config['nics'] = [n.__dump__() for n in self.nics]
config['nics'] = [n.json() for n in self.nics]

return config

Expand Down
2 changes: 0 additions & 2 deletions archinstall/lib/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ def _get_values(
raise ValueError('Unsupported formatting call')
elif hasattr(o, 'table_data'):
return o.table_data()
elif hasattr(o, 'json'):
return o.json()
elif is_dataclass(o):
return asdict(o)
else:
Expand Down

0 comments on commit 360a1b4

Please sign in to comment.