Chore(clan/clan_uri): Remove ClanURI class from clan_cli

This commit is contained in:
Johannes Kirschbauer
2025-04-23 16:53:11 +02:00
parent cd5942d675
commit 9b96f87160
9 changed files with 9 additions and 9 deletions

View File

@@ -0,0 +1,61 @@
# Import the urllib.parse, enum and dataclasses modules
import urllib.parse
import urllib.request
from dataclasses import dataclass
from pathlib import Path
from clan_cli.flake import Flake
# Define the ClanURI class
@dataclass
class ClanURI:
flake: Flake
machine_name: str
def get_url(self) -> str:
return str(self.flake)
@classmethod
def from_str(
cls,
url: str,
machine_name: str | None = None,
) -> "ClanURI":
uri = url
if machine_name:
uri += f"#{machine_name}"
# Users might copy whitespace along with the URI
uri = uri.strip()
# Check if the URI starts with clan://
# If it does, remove the clan:// prefix
prefix = "clan://"
if uri.startswith(prefix):
uri = uri[len(prefix) :]
# Fix missing colon (caused by browsers like Firefox)
if "//" in uri and ":" not in uri.split("//", 1)[0]:
# If there's a `//` but no colon before it, add one before the `//`
parts = uri.split("//", 1)
uri = f"{parts[0]}://{parts[1]}"
# Parse the URI into components
# url://netloc/path;parameters?query#fragment
components: urllib.parse.ParseResult = urllib.parse.urlparse(uri)
# Replace the query string in the components with the new query string
clean_comps = components._replace(query=components.query, fragment="")
# Parse the URL into a ClanUrl object
if clean_comps.path and Path(clean_comps.path).exists():
flake = Flake(clean_comps.path)
else:
flake = Flake(clean_comps.geturl())
machine_name = "defaultVM"
if components.fragment:
machine_name = components.fragment
return cls(flake, machine_name)

View File

@@ -13,12 +13,12 @@ from typing import IO, ClassVar
import gi
from clan_cli import vms
from clan_cli.clan_uri import ClanURI
from clan_cli.dirs import vm_state_dir
from clan_cli.machines.machines import Machine
from clan_cli.vms.inspect import inspect_vm
from clan_cli.vms.qemu import QMPWrapper
from clan_vm_manager.clan_uri import ClanURI
from clan_vm_manager.components.executor import MPProcess, spawn
from clan_vm_manager.history import HistoryEntry
from clan_vm_manager.singletons.toast import (

View File

@@ -6,12 +6,13 @@ import logging
from typing import Any
from clan_cli.clan.inspect import FlakeConfig, inspect_flake
from clan_cli.clan_uri import ClanURI
from clan_cli.dirs import user_history_file
from clan_cli.errors import ClanError
from clan_cli.locked_open import read_history_file, write_history_file
from clan_cli.machines.list import list_nixos_machines
from clan_vm_manager.clan_uri import ClanURI
log = logging.getLogger(__name__)

View File

@@ -4,9 +4,9 @@ from collections.abc import Callable
from typing import Any, ClassVar, cast
import gi
from clan_cli.clan_uri import ClanURI
from clan_cli.machines.machines import Machine
from clan_vm_manager.clan_uri import ClanURI
from clan_vm_manager.components.gkvstore import GKVStore
from clan_vm_manager.history import HistoryEntry, add_history
from clan_vm_manager.singletons.use_vms import ClanStore

View File

@@ -4,11 +4,11 @@ from pathlib import Path
from typing import Any, ClassVar
import gi
from clan_cli.clan_uri import ClanURI
from clan_cli.flake import Flake
from clan_cli.machines.machines import Machine
from clan_vm_manager import assets
from clan_vm_manager.clan_uri import ClanURI
from clan_vm_manager.components.gkvstore import GKVStore
from clan_vm_manager.components.vmobj import VMObject
from clan_vm_manager.history import HistoryEntry

View File

@@ -5,9 +5,9 @@ from functools import partial
from typing import Any, TypeVar
import gi
from clan_cli.clan_uri import ClanURI
from clan_cli.errors import ClanError
from clan_vm_manager.clan_uri import ClanURI
from clan_vm_manager.components.gkvstore import GKVStore
from clan_vm_manager.components.interfaces import ClanConfig
from clan_vm_manager.components.list_splash import EmptySplash