Change staticmethods to classmethods (#4115)
This commit is contained in:
parent
b1290672bb
commit
bde3b0ed6e
|
|
@ -1,6 +1,6 @@
|
|||
from dataclasses import dataclass
|
||||
from enum import StrEnum, auto
|
||||
from typing import Any, NotRequired, TypedDict
|
||||
from typing import Any, NotRequired, Self, TypedDict
|
||||
|
||||
|
||||
class PowerManagement(StrEnum):
|
||||
|
|
@ -63,9 +63,9 @@ class AudioConfiguration:
|
|||
'audio': self.audio.value,
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
def parse_arg(arg: dict[str, Any]) -> 'AudioConfiguration':
|
||||
return AudioConfiguration(
|
||||
@classmethod
|
||||
def parse_arg(cls, arg: dict[str, Any]) -> Self:
|
||||
return cls(
|
||||
Audio(arg['audio']),
|
||||
)
|
||||
|
||||
|
|
@ -77,9 +77,9 @@ class BluetoothConfiguration:
|
|||
def json(self) -> BluetoothConfigSerialization:
|
||||
return {'enabled': self.enabled}
|
||||
|
||||
@staticmethod
|
||||
def parse_arg(arg: BluetoothConfigSerialization) -> 'BluetoothConfiguration':
|
||||
return BluetoothConfiguration(arg['enabled'])
|
||||
@classmethod
|
||||
def parse_arg(cls, arg: BluetoothConfigSerialization) -> Self:
|
||||
return cls(arg['enabled'])
|
||||
|
||||
|
||||
@dataclass
|
||||
|
|
@ -91,9 +91,9 @@ class PowerManagementConfiguration:
|
|||
'power_management': self.power_management.value,
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
def parse_arg(arg: PowerManagementConfigSerialization) -> 'PowerManagementConfiguration':
|
||||
return PowerManagementConfiguration(
|
||||
@classmethod
|
||||
def parse_arg(cls, arg: PowerManagementConfigSerialization) -> Self:
|
||||
return cls(
|
||||
PowerManagement(arg['power_management']),
|
||||
)
|
||||
|
||||
|
|
@ -105,9 +105,9 @@ class PrintServiceConfiguration:
|
|||
def json(self) -> PrintServiceConfigSerialization:
|
||||
return {'enabled': self.enabled}
|
||||
|
||||
@staticmethod
|
||||
def parse_arg(arg: PrintServiceConfigSerialization) -> 'PrintServiceConfiguration':
|
||||
return PrintServiceConfiguration(arg['enabled'])
|
||||
@classmethod
|
||||
def parse_arg(cls, arg: PrintServiceConfigSerialization) -> Self:
|
||||
return cls(arg['enabled'])
|
||||
|
||||
|
||||
@dataclass
|
||||
|
|
@ -119,9 +119,9 @@ class FirewallConfiguration:
|
|||
'firewall': self.firewall.value,
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
def parse_arg(arg: dict[str, Any]) -> 'FirewallConfiguration':
|
||||
return FirewallConfiguration(
|
||||
@classmethod
|
||||
def parse_arg(cls, arg: dict[str, Any]) -> Self:
|
||||
return cls(
|
||||
Firewall(arg['firewall']),
|
||||
)
|
||||
|
||||
|
|
@ -131,14 +131,14 @@ class ZramConfiguration:
|
|||
enabled: bool
|
||||
algorithm: ZramAlgorithm = ZramAlgorithm.ZSTD
|
||||
|
||||
@staticmethod
|
||||
def parse_arg(arg: bool | dict[str, Any]) -> 'ZramConfiguration':
|
||||
@classmethod
|
||||
def parse_arg(cls, arg: bool | dict[str, Any]) -> Self:
|
||||
if isinstance(arg, bool):
|
||||
return ZramConfiguration(enabled=arg)
|
||||
return cls(enabled=arg)
|
||||
|
||||
enabled = arg.get('enabled', True)
|
||||
algo = arg.get('algorithm', arg.get('algo', ZramAlgorithm.ZSTD.value))
|
||||
return ZramConfiguration(enabled=enabled, algorithm=ZramAlgorithm(algo))
|
||||
return cls(enabled=enabled, algorithm=ZramAlgorithm(algo))
|
||||
|
||||
|
||||
@dataclass
|
||||
|
|
@ -149,12 +149,13 @@ class ApplicationConfiguration:
|
|||
print_service_config: PrintServiceConfiguration | None = None
|
||||
firewall_config: FirewallConfiguration | None = None
|
||||
|
||||
@staticmethod
|
||||
@classmethod
|
||||
def parse_arg(
|
||||
cls,
|
||||
args: dict[str, Any] | None = None,
|
||||
old_audio_config: dict[str, Any] | None = None,
|
||||
) -> 'ApplicationConfiguration':
|
||||
app_config = ApplicationConfiguration()
|
||||
) -> Self:
|
||||
app_config = cls()
|
||||
|
||||
if args and (bluetooth_config := args.get('bluetooth_config')) is not None:
|
||||
app_config.bluetooth_config = BluetoothConfiguration.parse_arg(bluetooth_config)
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
from dataclasses import dataclass, field
|
||||
from enum import Enum
|
||||
from typing import Any, NotRequired, TypedDict
|
||||
from typing import Any, NotRequired, Self, TypedDict
|
||||
|
||||
from archinstall.lib.models.users import Password, User
|
||||
from archinstall.lib.translationhandler import tr
|
||||
|
|
@ -40,14 +40,14 @@ class U2FLoginConfiguration:
|
|||
'passwordless_sudo': self.passwordless_sudo,
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
def parse_arg(args: U2FLoginConfigSerialization) -> 'U2FLoginConfiguration | None':
|
||||
@classmethod
|
||||
def parse_arg(cls, args: U2FLoginConfigSerialization) -> Self | None:
|
||||
u2f_login_method = args.get('u2f_login_method')
|
||||
|
||||
if not u2f_login_method:
|
||||
return None
|
||||
|
||||
u2f_config = U2FLoginConfiguration(u2f_login_method=U2FLoginMethod(u2f_login_method))
|
||||
u2f_config = cls(u2f_login_method=U2FLoginMethod(u2f_login_method))
|
||||
|
||||
u2f_config.u2f_login_method = U2FLoginMethod(u2f_login_method)
|
||||
|
||||
|
|
@ -63,9 +63,9 @@ class AuthenticationConfiguration:
|
|||
users: list[User] = field(default_factory=list)
|
||||
u2f_config: U2FLoginConfiguration | None = None
|
||||
|
||||
@staticmethod
|
||||
def parse_arg(args: dict[str, Any]) -> 'AuthenticationConfiguration':
|
||||
auth_config = AuthenticationConfiguration()
|
||||
@classmethod
|
||||
def parse_arg(cls, args: dict[str, Any]) -> Self:
|
||||
auth_config = cls()
|
||||
|
||||
if (u2f_config := args.get('u2f_config')) is not None:
|
||||
auth_config.u2f_config = U2FLoginConfiguration.parse_arg(u2f_config)
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ import uuid
|
|||
from dataclasses import dataclass, field
|
||||
from enum import Enum
|
||||
from pathlib import Path
|
||||
from typing import NotRequired, TypedDict, override
|
||||
from typing import NotRequired, Self, TypedDict, override
|
||||
from uuid import UUID
|
||||
|
||||
import parted
|
||||
|
|
@ -254,17 +254,17 @@ class Unit(Enum):
|
|||
|
||||
sectors = 'sectors' # size in sector
|
||||
|
||||
@staticmethod
|
||||
def get_all_units() -> list[str]:
|
||||
return [u.name for u in Unit]
|
||||
@classmethod
|
||||
def get_all_units(cls) -> list[str]:
|
||||
return [u.name for u in cls]
|
||||
|
||||
@staticmethod
|
||||
def get_si_units() -> list[Unit]:
|
||||
return [u for u in Unit if 'i' not in u.name and u.name != 'sectors']
|
||||
@classmethod
|
||||
def get_si_units(cls) -> list[Self]:
|
||||
return [u for u in cls if 'i' not in u.name and u.name != 'sectors']
|
||||
|
||||
@staticmethod
|
||||
def get_binary_units() -> list[Unit]:
|
||||
return [u for u in Unit if 'i' in u.name or u.name == 'B']
|
||||
@classmethod
|
||||
def get_binary_units(cls) -> list[Self]:
|
||||
return [u for u in cls if 'i' in u.name or u.name == 'B']
|
||||
|
||||
|
||||
class _SectorSizeSerialization(TypedDict):
|
||||
|
|
@ -282,9 +282,9 @@ class SectorSize:
|
|||
case Unit.sectors:
|
||||
raise ValueError('Unit type sector not allowed for SectorSize')
|
||||
|
||||
@staticmethod
|
||||
def default() -> SectorSize:
|
||||
return SectorSize(512, Unit.B)
|
||||
@classmethod
|
||||
def default(cls) -> Self:
|
||||
return cls(512, Unit.B)
|
||||
|
||||
def json(self) -> _SectorSizeSerialization:
|
||||
return {
|
||||
|
|
@ -1087,15 +1087,15 @@ class LvmVolumeGroup:
|
|||
'volumes': [vol.json() for vol in self.volumes],
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
def parse_arg(arg: _LvmVolumeGroupSerialization, disk_config: DiskLayoutConfiguration) -> LvmVolumeGroup:
|
||||
@classmethod
|
||||
def parse_arg(cls, arg: _LvmVolumeGroupSerialization, disk_config: DiskLayoutConfiguration) -> Self:
|
||||
lvm_pvs = []
|
||||
for mod in disk_config.device_modifications:
|
||||
for part in mod.partitions:
|
||||
if part.obj_id in arg.get('lvm_pvs', []):
|
||||
lvm_pvs.append(part)
|
||||
|
||||
return LvmVolumeGroup(
|
||||
return cls(
|
||||
arg['name'],
|
||||
lvm_pvs,
|
||||
[LvmVolume.parse_arg(vol) for vol in arg['volumes']],
|
||||
|
|
@ -1191,9 +1191,9 @@ class LvmVolume:
|
|||
|
||||
raise ValueError('Mountpoint is not specified')
|
||||
|
||||
@staticmethod
|
||||
def parse_arg(arg: _LvmVolumeSerialization) -> LvmVolume:
|
||||
volume = LvmVolume(
|
||||
@classmethod
|
||||
def parse_arg(cls, arg: _LvmVolumeSerialization) -> Self:
|
||||
volume = cls(
|
||||
status=LvmVolumeStatus(arg['status']),
|
||||
name=arg['name'],
|
||||
fs_type=FilesystemType(arg['fs_type']),
|
||||
|
|
@ -1296,8 +1296,8 @@ class LvmConfiguration:
|
|||
'vol_groups': [vol_gr.json() for vol_gr in self.vol_groups],
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
def parse_arg(arg: _LvmConfigurationSerialization, disk_config: DiskLayoutConfiguration) -> LvmConfiguration:
|
||||
@classmethod
|
||||
def parse_arg(cls, arg: _LvmConfigurationSerialization, disk_config: DiskLayoutConfiguration) -> Self:
|
||||
lvm_pvs = []
|
||||
for mod in disk_config.device_modifications:
|
||||
for part in mod.partitions:
|
||||
|
|
@ -1305,7 +1305,7 @@ class LvmConfiguration:
|
|||
if part.obj_id in arg.get('lvm_pvs', []): # type: ignore[operator]
|
||||
lvm_pvs.append(part)
|
||||
|
||||
return LvmConfiguration(
|
||||
return cls(
|
||||
config_type=LvmLayoutType(arg['config_type']),
|
||||
vol_groups=[LvmVolumeGroup.parse_arg(vol_group, disk_config) for vol_group in arg['vol_groups']],
|
||||
)
|
||||
|
|
@ -1354,9 +1354,9 @@ class SnapshotConfig:
|
|||
def json(self) -> _SnapshotConfigSerialization:
|
||||
return {'type': self.snapshot_type.value}
|
||||
|
||||
@staticmethod
|
||||
def parse_args(args: _SnapshotConfigSerialization) -> SnapshotConfig:
|
||||
return SnapshotConfig(SnapshotType(args['type']))
|
||||
@classmethod
|
||||
def parse_args(cls, args: _SnapshotConfigSerialization) -> Self:
|
||||
return cls(SnapshotType(args['type']))
|
||||
|
||||
|
||||
@dataclass
|
||||
|
|
@ -1366,12 +1366,12 @@ class BtrfsOptions:
|
|||
def json(self) -> _BtrfsOptionsSerialization:
|
||||
return {'snapshot_config': self.snapshot_config.json() if self.snapshot_config else None}
|
||||
|
||||
@staticmethod
|
||||
def parse_arg(arg: _BtrfsOptionsSerialization) -> BtrfsOptions | None:
|
||||
@classmethod
|
||||
def parse_arg(cls, arg: _BtrfsOptionsSerialization) -> Self | None:
|
||||
snapshot_args = arg.get('snapshot_config')
|
||||
if snapshot_args:
|
||||
snapshot_config = SnapshotConfig.parse_args(snapshot_args)
|
||||
return BtrfsOptions(snapshot_config)
|
||||
return cls(snapshot_config)
|
||||
|
||||
return None
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
from dataclasses import dataclass
|
||||
from typing import Any
|
||||
from typing import Any, Self
|
||||
|
||||
from archinstall.lib.translationhandler import tr
|
||||
|
||||
|
|
@ -12,12 +12,12 @@ class LocaleConfiguration:
|
|||
sys_lang: str
|
||||
sys_enc: str
|
||||
|
||||
@staticmethod
|
||||
def default() -> 'LocaleConfiguration':
|
||||
@classmethod
|
||||
def default(cls) -> Self:
|
||||
layout = get_kb_layout()
|
||||
if layout == '':
|
||||
layout = 'us'
|
||||
return LocaleConfiguration(layout, 'en_US.UTF-8', 'UTF-8')
|
||||
return cls(layout, 'en_US.UTF-8', 'UTF-8')
|
||||
|
||||
def json(self) -> dict[str, str]:
|
||||
return {
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ from __future__ import annotations
|
|||
import re
|
||||
from dataclasses import dataclass, field
|
||||
from enum import Enum
|
||||
from typing import TYPE_CHECKING, NotRequired, TypedDict, override
|
||||
from typing import TYPE_CHECKING, NotRequired, Self, TypedDict, override
|
||||
|
||||
from archinstall.lib.output import debug
|
||||
from archinstall.lib.translationhandler import tr
|
||||
|
|
@ -66,9 +66,9 @@ class Nic:
|
|||
'dns': self.dns,
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
def parse_arg(arg: _NicSerialization) -> Nic:
|
||||
return Nic(
|
||||
@classmethod
|
||||
def parse_arg(cls, arg: _NicSerialization) -> Self:
|
||||
return cls(
|
||||
iface=arg.get('iface', None),
|
||||
ip=arg.get('ip', None),
|
||||
dhcp=arg.get('dhcp', True),
|
||||
|
|
@ -121,22 +121,22 @@ class NetworkConfiguration:
|
|||
|
||||
return config
|
||||
|
||||
@staticmethod
|
||||
def parse_arg(config: _NetworkConfigurationSerialization) -> NetworkConfiguration | None:
|
||||
@classmethod
|
||||
def parse_arg(cls, config: _NetworkConfigurationSerialization) -> Self | None:
|
||||
nic_type = config.get('type', None)
|
||||
if not nic_type:
|
||||
return None
|
||||
|
||||
match NicType(nic_type):
|
||||
case NicType.ISO:
|
||||
return NetworkConfiguration(NicType.ISO)
|
||||
return cls(NicType.ISO)
|
||||
case NicType.NM:
|
||||
return NetworkConfiguration(NicType.NM)
|
||||
return cls(NicType.NM)
|
||||
case NicType.MANUAL:
|
||||
nics_arg = config.get('nics', [])
|
||||
if nics_arg:
|
||||
nics = [Nic.parse_arg(n) for n in nics_arg]
|
||||
return NetworkConfiguration(NicType.MANUAL, nics)
|
||||
return cls(NicType.MANUAL, nics)
|
||||
|
||||
return None
|
||||
|
||||
|
|
@ -199,9 +199,9 @@ class WifiNetwork:
|
|||
'BSSID': self.bssid,
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
def from_wpa(results: str) -> list[WifiNetwork]:
|
||||
entries: list[WifiNetwork] = []
|
||||
@classmethod
|
||||
def from_wpa(cls, results: str) -> list[Self]:
|
||||
entries = []
|
||||
|
||||
for line in results.splitlines():
|
||||
line = line.strip()
|
||||
|
|
@ -212,7 +212,7 @@ class WifiNetwork:
|
|||
if len(parts) != 5:
|
||||
continue
|
||||
|
||||
wifi = WifiNetwork(bssid=parts[0], frequency=parts[1], signal_level=parts[2], flags=parts[3], ssid=parts[4])
|
||||
wifi = cls(bssid=parts[0], frequency=parts[1], signal_level=parts[2], flags=parts[3], ssid=parts[4])
|
||||
entries.append(wifi)
|
||||
|
||||
return entries
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
from dataclasses import dataclass, field
|
||||
from enum import Enum
|
||||
from functools import cached_property
|
||||
from typing import Any, override
|
||||
from typing import Any, Self, override
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
|
@ -47,9 +47,9 @@ class PackageSearchResult:
|
|||
makedepends: list[str]
|
||||
checkdepends: list[str]
|
||||
|
||||
@staticmethod
|
||||
def from_json(data: dict[str, Any]) -> 'PackageSearchResult':
|
||||
return PackageSearchResult(**data)
|
||||
@classmethod
|
||||
def from_json(cls, data: dict[str, Any]) -> Self:
|
||||
return cls(**data)
|
||||
|
||||
@property
|
||||
def pkg_version(self) -> str:
|
||||
|
|
@ -75,11 +75,11 @@ class PackageSearch:
|
|||
page: int
|
||||
results: list[PackageSearchResult]
|
||||
|
||||
@staticmethod
|
||||
def from_json(data: dict[str, Any]) -> 'PackageSearch':
|
||||
@classmethod
|
||||
def from_json(cls, data: dict[str, Any]) -> Self:
|
||||
results = [PackageSearchResult.from_json(r) for r in data['results']]
|
||||
|
||||
return PackageSearch(
|
||||
return cls(
|
||||
version=data['version'],
|
||||
limit=data['limit'],
|
||||
valid=data['valid'],
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ from collections.abc import Callable
|
|||
from dataclasses import dataclass, field
|
||||
from enum import Enum
|
||||
from functools import cached_property
|
||||
from typing import Any, ClassVar
|
||||
from typing import Any, ClassVar, Self
|
||||
|
||||
from archinstall.lib.translationhandler import tr
|
||||
|
||||
|
|
@ -113,21 +113,22 @@ class MenuItemGroup:
|
|||
def get_enabled_items(self) -> list[MenuItem]:
|
||||
return [it for it in self.items if self.is_enabled(it)]
|
||||
|
||||
@staticmethod
|
||||
def yes_no() -> 'MenuItemGroup':
|
||||
return MenuItemGroup(
|
||||
@classmethod
|
||||
def yes_no(cls) -> Self:
|
||||
return cls(
|
||||
[MenuItem.yes(), MenuItem.no()],
|
||||
sort_items=True,
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
@classmethod
|
||||
def from_enum(
|
||||
cls,
|
||||
enum_cls: type[Enum],
|
||||
sort_items: bool = False,
|
||||
preset: Enum | None = None,
|
||||
) -> 'MenuItemGroup':
|
||||
) -> Self:
|
||||
items = [MenuItem(elem.value, value=elem) for elem in enum_cls]
|
||||
group = MenuItemGroup(items, sort_items=sort_items)
|
||||
group = cls(items, sort_items=sort_items)
|
||||
|
||||
if preset is not None:
|
||||
group.set_selected_by_value(preset)
|
||||
|
|
|
|||
Loading…
Reference in New Issue