2023-12-05 17:08:27 +00:00
|
|
|
# Import the urllib.parse, enum and dataclasses modules
|
2023-12-05 17:16:51 +00:00
|
|
|
import dataclasses
|
2023-12-05 15:17:15 +00:00
|
|
|
import urllib.parse
|
2023-12-19 17:02:06 +00:00
|
|
|
import urllib.request
|
2023-12-05 17:08:27 +00:00
|
|
|
from dataclasses import dataclass
|
|
|
|
from pathlib import Path
|
2024-03-06 19:24:36 +00:00
|
|
|
from typing import Any
|
2023-12-05 17:16:51 +00:00
|
|
|
|
2023-12-05 15:17:15 +00:00
|
|
|
from .errors import ClanError
|
2023-12-05 17:16:51 +00:00
|
|
|
|
2023-12-05 15:17:15 +00:00
|
|
|
|
2024-03-08 16:23:18 +00:00
|
|
|
@dataclass
|
2024-03-08 16:47:27 +00:00
|
|
|
class FlakeId:
|
|
|
|
_value: str | Path
|
2024-03-07 12:04:48 +00:00
|
|
|
|
2024-03-08 16:23:18 +00:00
|
|
|
def __str__(self) -> str:
|
2024-03-08 16:47:27 +00:00
|
|
|
return f"{self._value}" # The __str__ method returns a custom string representation
|
|
|
|
|
|
|
|
@property
|
|
|
|
def path(self) -> Path:
|
|
|
|
assert isinstance(self._value, Path)
|
|
|
|
return self._value
|
|
|
|
|
|
|
|
@property
|
|
|
|
def url(self) -> str:
|
|
|
|
assert isinstance(self._value, str)
|
|
|
|
return self._value
|
2023-12-05 17:16:51 +00:00
|
|
|
|
2024-03-08 16:23:18 +00:00
|
|
|
def __repr__(self) -> str:
|
2024-03-08 16:47:27 +00:00
|
|
|
return f"ClanUrl({self._value})"
|
2023-12-05 17:16:51 +00:00
|
|
|
|
2024-03-08 16:23:18 +00:00
|
|
|
def is_local(self) -> bool:
|
2024-03-08 16:47:27 +00:00
|
|
|
return isinstance(self._value, Path)
|
2024-03-07 12:04:48 +00:00
|
|
|
|
2024-03-08 16:23:18 +00:00
|
|
|
def is_remote(self) -> bool:
|
2024-03-08 16:47:27 +00:00
|
|
|
return isinstance(self._value, str)
|
2023-12-05 17:08:27 +00:00
|
|
|
|
2023-12-05 15:17:15 +00:00
|
|
|
|
2023-12-05 17:08:27 +00:00
|
|
|
# Parameters defined here will be DELETED from the nested uri
|
|
|
|
# so make sure there are no conflicts with other webservices
|
|
|
|
@dataclass
|
2024-03-06 19:24:36 +00:00
|
|
|
class MachineParams:
|
|
|
|
dummy_opt: str = "dummy"
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class MachineData:
|
2024-03-08 16:47:27 +00:00
|
|
|
flake_id: FlakeId
|
2024-03-06 19:24:36 +00:00
|
|
|
name: str = "defaultVM"
|
|
|
|
params: MachineParams = dataclasses.field(default_factory=MachineParams)
|
2023-12-05 15:17:15 +00:00
|
|
|
|
2024-03-07 12:04:48 +00:00
|
|
|
def get_id(self) -> str:
|
2024-03-08 16:47:27 +00:00
|
|
|
return f"{self.flake_id}#{self.name}"
|
2024-03-07 12:04:48 +00:00
|
|
|
|
2023-12-05 17:16:51 +00:00
|
|
|
|
2023-12-05 15:17:15 +00:00
|
|
|
# Define the ClanURI class
|
|
|
|
class ClanURI:
|
2024-03-06 19:24:36 +00:00
|
|
|
_orig_uri: str
|
|
|
|
_components: urllib.parse.ParseResult
|
2024-03-08 16:47:27 +00:00
|
|
|
flake_id: FlakeId
|
2024-03-07 12:04:48 +00:00
|
|
|
_machines: list[MachineData]
|
2024-03-06 19:24:36 +00:00
|
|
|
|
2023-12-05 15:17:15 +00:00
|
|
|
# Initialize the class with a clan:// URI
|
|
|
|
def __init__(self, uri: str) -> None:
|
2024-03-07 12:04:48 +00:00
|
|
|
self._machines = []
|
2024-03-06 19:24:36 +00:00
|
|
|
|
2024-01-04 15:30:26 +00:00
|
|
|
# users might copy whitespace along with the uri
|
|
|
|
uri = uri.strip()
|
2024-03-06 19:24:36 +00:00
|
|
|
self._orig_uri = uri
|
2024-01-16 16:11:26 +00:00
|
|
|
|
2023-12-05 17:08:27 +00:00
|
|
|
# Check if the URI starts with clan://
|
2024-01-16 16:11:26 +00:00
|
|
|
# If it does, remove the clan:// prefix
|
2023-12-05 15:17:15 +00:00
|
|
|
if uri.startswith("clan://"):
|
2024-03-08 16:23:18 +00:00
|
|
|
nested_uri = uri[7:]
|
2023-12-05 15:17:15 +00:00
|
|
|
else:
|
2024-03-06 19:24:36 +00:00
|
|
|
raise ClanError(f"Invalid uri: expected clan://, got {uri}")
|
2023-12-05 15:17:15 +00:00
|
|
|
|
|
|
|
# Parse the URI into components
|
2024-03-06 19:24:36 +00:00
|
|
|
# url://netloc/path;parameters?query#fragment
|
2024-03-08 16:23:18 +00:00
|
|
|
self._components = urllib.parse.urlparse(nested_uri)
|
2023-12-05 17:08:27 +00:00
|
|
|
|
2024-03-06 19:24:36 +00:00
|
|
|
# Replace the query string in the components with the new query string
|
|
|
|
clean_comps = self._components._replace(
|
|
|
|
query=self._components.query, fragment=""
|
|
|
|
)
|
2023-12-05 17:08:27 +00:00
|
|
|
|
2024-03-06 19:24:36 +00:00
|
|
|
# Parse the URL into a ClanUrl object
|
2024-03-08 16:47:27 +00:00
|
|
|
self.flake_id = self._parse_url(clean_comps)
|
2024-01-16 16:11:26 +00:00
|
|
|
|
2024-03-06 19:24:36 +00:00
|
|
|
# Parse the fragment into a list of machine queries
|
|
|
|
# Then parse every machine query into a MachineParameters object
|
|
|
|
machine_frags = list(
|
|
|
|
filter(lambda x: len(x) > 0, self._components.fragment.split("#"))
|
|
|
|
)
|
|
|
|
for machine_frag in machine_frags:
|
|
|
|
machine = self._parse_machine_query(machine_frag)
|
2024-03-07 12:04:48 +00:00
|
|
|
self._machines.append(machine)
|
2024-01-16 16:11:26 +00:00
|
|
|
|
2024-03-06 19:24:36 +00:00
|
|
|
# If there are no machine fragments, add a default machine
|
|
|
|
if len(machine_frags) == 0:
|
2024-03-08 16:47:27 +00:00
|
|
|
default_machine = MachineData(flake_id=self.flake_id)
|
2024-03-07 12:04:48 +00:00
|
|
|
self._machines.append(default_machine)
|
2023-12-05 17:08:27 +00:00
|
|
|
|
2024-03-08 16:47:27 +00:00
|
|
|
def _parse_url(self, comps: urllib.parse.ParseResult) -> FlakeId:
|
2023-12-08 12:46:21 +00:00
|
|
|
comb = (
|
2024-03-06 19:24:36 +00:00
|
|
|
comps.scheme,
|
|
|
|
comps.netloc,
|
|
|
|
comps.path,
|
|
|
|
comps.params,
|
|
|
|
comps.query,
|
|
|
|
comps.fragment,
|
2023-12-08 12:46:21 +00:00
|
|
|
)
|
|
|
|
match comb:
|
2024-03-06 19:24:36 +00:00
|
|
|
case ("file", "", path, "", "", _) | ("", "", path, "", "", _): # type: ignore
|
2024-03-08 16:47:27 +00:00
|
|
|
flake_id = FlakeId(Path(path).expanduser().resolve())
|
2023-12-05 17:08:27 +00:00
|
|
|
case _:
|
2024-03-08 16:47:27 +00:00
|
|
|
flake_id = FlakeId(comps.geturl())
|
2023-12-19 16:51:43 +00:00
|
|
|
|
2024-03-08 16:47:27 +00:00
|
|
|
return flake_id
|
2024-03-06 19:24:36 +00:00
|
|
|
|
|
|
|
def _parse_machine_query(self, machine_frag: str) -> MachineData:
|
|
|
|
comp = urllib.parse.urlparse(machine_frag)
|
2023-12-12 20:31:47 +00:00
|
|
|
query = urllib.parse.parse_qs(comp.query)
|
2024-03-06 19:24:36 +00:00
|
|
|
machine_name = comp.path
|
2024-01-16 16:11:26 +00:00
|
|
|
|
2024-03-06 19:24:36 +00:00
|
|
|
machine_params: dict[str, Any] = {}
|
|
|
|
for dfield in dataclasses.fields(MachineParams):
|
|
|
|
if dfield.name in query:
|
|
|
|
values = query[dfield.name]
|
|
|
|
if len(values) > 1:
|
|
|
|
raise ClanError(f"Multiple values for parameter: {dfield.name}")
|
|
|
|
machine_params[dfield.name] = values[0]
|
|
|
|
|
|
|
|
# Remove the field from the query dictionary
|
|
|
|
# clan uri and nested uri share one namespace for query parameters
|
|
|
|
# we need to make sure there are no conflicts
|
|
|
|
del query[dfield.name]
|
|
|
|
params = MachineParams(**machine_params)
|
2024-03-08 16:47:27 +00:00
|
|
|
machine = MachineData(flake_id=self.flake_id, name=machine_name, params=params)
|
2024-03-06 19:24:36 +00:00
|
|
|
return machine
|
|
|
|
|
2024-03-07 12:04:48 +00:00
|
|
|
@property
|
|
|
|
def machine(self) -> MachineData:
|
|
|
|
return self._machines[0]
|
|
|
|
|
2024-03-06 19:24:36 +00:00
|
|
|
def get_orig_uri(self) -> str:
|
|
|
|
return self._orig_uri
|
2024-01-16 16:11:26 +00:00
|
|
|
|
2024-03-06 19:24:36 +00:00
|
|
|
def get_url(self) -> str:
|
2024-03-08 16:47:27 +00:00
|
|
|
return str(self.flake_id)
|
2024-03-08 16:23:18 +00:00
|
|
|
|
2024-03-07 12:04:48 +00:00
|
|
|
@classmethod
|
|
|
|
def from_str(
|
|
|
|
cls, # noqa
|
|
|
|
url: str,
|
|
|
|
machine_name: str | None = None,
|
|
|
|
) -> "ClanURI":
|
|
|
|
clan_uri = ""
|
|
|
|
if not url.startswith("clan://"):
|
|
|
|
clan_uri += "clan://"
|
|
|
|
|
|
|
|
clan_uri += url
|
|
|
|
|
|
|
|
if machine_name:
|
|
|
|
clan_uri += f"#{machine_name}"
|
|
|
|
|
|
|
|
return cls(clan_uri)
|
2023-12-08 11:18:55 +00:00
|
|
|
|
|
|
|
def __str__(self) -> str:
|
2024-03-06 19:24:36 +00:00
|
|
|
return self.get_orig_uri()
|
2023-12-31 13:13:11 +00:00
|
|
|
|
|
|
|
def __repr__(self) -> str:
|
2024-03-06 19:24:36 +00:00
|
|
|
return f"ClanURI({self})"
|