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 argparse
import json import json
import os import os

View File

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

View File

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

View File

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

View File

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

View File

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