clan_cli: URI parser now only has HTTP and FILE. Also clan:///home/user or clan://~/Downloads is supported

This commit is contained in:
Qubasa
2023-12-08 13:46:21 +01:00
parent 6f80cdb5eb
commit b8bc7a3fcc
4 changed files with 82 additions and 31 deletions

View File

@@ -20,14 +20,6 @@ class ClanScheme(Enum):
def __str__(self) -> str:
return f"HTTP({self.url})" # The __str__ method returns a custom string representation
@member
@dataclass
class HTTPS:
url: str # The url field holds the HTTPS URL
def __str__(self) -> str:
return f"HTTPS({self.url})" # The __str__ method returns a custom string representation
@member
@dataclass
class FILE:
@@ -78,22 +70,30 @@ class ClanURI:
self._components = self._components._replace(query=new_query)
self.params = ClanParameters(**params)
# Use the match statement to check the scheme and create a ClanScheme member with the value
match self._components.scheme:
case "http":
comb = (
self._components.scheme,
self._components.netloc,
self._components.path,
self._components.params,
self._components.query,
self._components.fragment,
)
match comb:
case ("http" | "https", _, _, _, _, _):
self.scheme = ClanScheme.HTTP.value(self._components.geturl()) # type: ignore
case "https":
self.scheme = ClanScheme.HTTPS.value(self._components.geturl()) # type: ignore
case "file":
self.scheme = ClanScheme.FILE.value(Path(self._components.path)) # type: ignore
case ("file", "", path, "", "", "") | ("", "", path, "", "", ""): # type: ignore
self.scheme = ClanScheme.FILE.value(Path(path)) # type: ignore
case _:
raise ClanError(f"Unsupported scheme: {self._components.scheme}")
raise ClanError(f"Unsupported uri components: {comb}")
def get_internal(self) -> str:
return self._nested_uri
@classmethod
def from_path(cls, path: Path, params: ClanParameters) -> Self: # noqa
urlparams = urllib.parse.urlencode(params.__dict__)
return cls(f"clan://file://{path}?{urlparams}")
return cls(f"clan://{path}?{urlparams}")
def __str__(self) -> str:
return f"ClanURI({self._components.geturl()})"
return f"ClanURI({self._components.geturl()})"

View File

@@ -82,6 +82,13 @@ def nix_eval(flags: list[str]) -> list[str]:
return default_flags + flags
def nix_metadata(flake: str) -> dict[str, Any]:
cmd = nix_command(["flake", "metadata", "--json", flake])
proc = subprocess.run(cmd, check=True, text=True, stdout=subprocess.PIPE)
data = json.loads(proc.stdout)
return data
@deal.raises(ClanError)
def nix_shell(packages: list[str], cmd: list[str]) -> list[str]:
# we cannot use nix-shell inside the nix sandbox