nixos-rebuild-ng: create instance for dataclass from Self

This commit is contained in:
Thiago Kenji Okada 2024-11-17 18:36:24 +00:00
parent 6c6d08dc4f
commit e47b17e239
6 changed files with 11 additions and 23 deletions

View File

@ -1,5 +1,3 @@
from __future__ import annotations
import argparse
import json
import os

View File

@ -1,12 +1,10 @@
from __future__ import annotations
import os
import platform
import re
from dataclasses import dataclass
from enum import Enum
from pathlib import Path
from typing import Any, ClassVar, TypedDict, override
from typing import Any, ClassVar, Self, TypedDict, override
class NRError(Exception):
@ -54,15 +52,15 @@ class Flake:
return f"{self.path}#{self.attr}"
@classmethod
def parse(cls, flake_str: str, hostname: str | None = None) -> Flake:
def parse(cls, flake_str: str, hostname: str | None = None) -> Self:
m = cls._re.match(flake_str)
assert m is not None, f"got no matches for {flake_str}"
attr = m.group("attr")
nixos_attr = f"nixosConfigurations.{attr or hostname or "default"}"
return Flake(Path(m.group("path")), nixos_attr)
return cls(Path(m.group("path")), nixos_attr)
@classmethod
def from_arg(cls, flake_arg: Any) -> Flake | None:
def from_arg(cls, flake_arg: Any) -> Self | None:
hostname = platform.node()
match flake_arg:
case str(s):
@ -106,15 +104,15 @@ class Profile:
name: str
path: Path
@staticmethod
def from_name(name: str = "system") -> Profile:
@classmethod
def from_name(cls, name: str = "system") -> Self:
match name:
case "system":
return Profile(name, Path("/nix/var/nix/profiles/system"))
return cls(name, Path("/nix/var/nix/profiles/system"))
case _:
path = Path("/nix/var/nix/profiles/system-profiles") / name
path.parent.mkdir(mode=0o755, parents=True, exist_ok=True)
return Profile(name, path)
return cls(name, path)
@dataclass(frozen=True)
@ -122,10 +120,10 @@ class SSH:
host: str
opts: list[str]
@staticmethod
def from_arg(host: str | None) -> SSH | None:
@classmethod
def from_arg(cls, host: str | None) -> Self | None:
if host:
opts = os.getenv("SSH_OPTS", "").split()
return SSH(host, opts)
return cls(host, opts)
else:
return None

View File

@ -1,5 +1,3 @@
from __future__ import annotations
import os
import shutil
from datetime import datetime

View File

@ -1,5 +1,3 @@
from __future__ import annotations
import os
import subprocess
from typing import Any, Sequence

View File

@ -1,5 +1,3 @@
from __future__ import annotations
import argparse
import sys
from functools import partial

View File

@ -38,8 +38,6 @@ ignore_missing_imports = true
extend-select = [
# ensure imports are sorted
"I",
# require 'from __future__ import annotations'
"FA102",
# require `check` argument for `subprocess.run`
"PLW1510",
]