clan_uri: Support all other formats by just differentiating between remote and local

This commit is contained in:
Qubasa
2024-01-02 14:55:20 +01:00
parent cce293fab5
commit d7c5850f2f
3 changed files with 15 additions and 49 deletions

View File

@@ -10,38 +10,24 @@ from typing import Self
from .errors import ClanError
def url_ok(url: str) -> None:
# Create a request object with the URL and the HEAD method
req = urllib.request.Request(url, method="HEAD")
try:
# Open the URL and get the response object
res = urllib.request.urlopen(req)
# Return True if the status code is 200 (OK)
if not res.getcode() == 200:
raise ClanError(f"URL has status code: {res.status_code}")
except urllib.error.URLError as ex:
raise ClanError(f"URL error: {ex}")
# Define an enum with different members that have different values
class ClanScheme(Enum):
# Use the dataclass decorator to add fields and methods to the members
@member
@dataclass
class HTTP:
class REMOTE:
url: str # The url field holds the HTTP URL
def __str__(self) -> str:
return f"HTTP({self.url})" # The __str__ method returns a custom string representation
return f"REMOTE({self.url})" # The __str__ method returns a custom string representation
@member
@dataclass
class FILE:
class LOCAL:
path: Path # The path field holds the local path
def __str__(self) -> str:
return f"FILE({self.path})" # The __str__ method returns a custom string representation
return f"LOCAL({self.path})" # The __str__ method returns a custom string representation
# Parameters defined here will be DELETED from the nested uri
@@ -96,26 +82,16 @@ class ClanURI:
)
match comb:
case ("http" | "https", _, _, _, _, _):
self.scheme = ClanScheme.HTTP.value(self._components.geturl()) # type: ignore
case ("file", "", path, "", "", "") | ("", "", path, "", "", ""): # type: ignore
self.scheme = ClanScheme.FILE.value(Path(path))
self.scheme = ClanScheme.LOCAL.value(Path(path).resolve()) # type: ignore
case _:
raise ClanError(f"Unsupported uri components: {comb}")
def check_exits(self) -> None:
match self.scheme:
case ClanScheme.FILE.value(path):
if not path.exists():
raise ClanError(f"File does not exist: {path}")
case ClanScheme.HTTP.value(url):
return url_ok(url)
self.scheme = ClanScheme.REMOTE.value(self._components.geturl()) # type: ignore
def get_internal(self) -> str:
match self.scheme:
case ClanScheme.FILE.value(path):
case ClanScheme.LOCAL.value(path):
return str(path)
case ClanScheme.HTTP.value(url):
case ClanScheme.REMOTE.value(url):
return url
case _:
raise ClanError(f"Unsupported uri components: {self.scheme}")

View File

@@ -65,7 +65,6 @@ def list_history() -> list[HistoryEntry]:
# TODO: Add all vm entries to history
def add_history(uri: ClanURI) -> list[HistoryEntry]:
uri.check_exits()
user_history_file().parent.mkdir(parents=True, exist_ok=True)
logs = list_history()
found = False

View File

@@ -1,9 +1,6 @@
from pathlib import Path
import pytest
from clan_cli.clan_uri import ClanParameters, ClanScheme, ClanURI
from clan_cli.errors import ClanError
def test_get_internal() -> None:
@@ -25,24 +22,18 @@ def test_local_uri() -> None:
# Create a ClanURI object from a local URI
uri = ClanURI("clan://file:///home/user/Downloads")
match uri.scheme:
case ClanScheme.FILE.value(path):
case ClanScheme.LOCAL.value(path):
assert path == Path("/home/user/Downloads") # type: ignore
case _:
assert False
def test_unsupported_schema() -> None:
with pytest.raises(ClanError, match="Unsupported uri components: .*"):
# Create a ClanURI object from an unsupported URI
ClanURI("clan://ftp://ftp.example.com")
def test_is_remote() -> None:
# Create a ClanURI object from a remote URI
uri = ClanURI("clan://https://example.com")
match uri.scheme:
case ClanScheme.HTTP.value(url):
case ClanScheme.REMOTE.value(url):
assert url == "https://example.com" # type: ignore
case _:
assert False
@@ -67,7 +58,7 @@ def test_remote_with_clanparams() -> None:
assert uri.params.flake_attr == "defaultVM"
match uri.scheme:
case ClanScheme.HTTP.value(url):
case ClanScheme.REMOTE.value(url):
assert url == "https://example.com" # type: ignore
case _:
assert False
@@ -81,7 +72,7 @@ def test_from_path_with_custom() -> None:
assert uri.params.flake_attr == "myVM"
match uri.scheme:
case ClanScheme.FILE.value(path):
case ClanScheme.LOCAL.value(path):
assert path == Path("/home/user/Downloads") # type: ignore
case _:
assert False
@@ -95,7 +86,7 @@ def test_from_path_with_default() -> None:
assert uri.params.flake_attr == "defaultVM"
match uri.scheme:
case ClanScheme.FILE.value(path):
case ClanScheme.LOCAL.value(path):
assert path == Path("/home/user/Downloads") # type: ignore
case _:
assert False
@@ -109,7 +100,7 @@ def test_from_str() -> None:
assert uri.params.flake_attr == "myVM"
match uri.scheme:
case ClanScheme.HTTP.value(url):
case ClanScheme.REMOTE.value(url):
assert url == "https://example.com?password=asdasd&test=1234" # type: ignore
case _:
assert False
@@ -137,7 +128,7 @@ def test_remote_with_all_params() -> None:
assert uri.params.flake_attr == "myVM"
match uri.scheme:
case ClanScheme.HTTP.value(url):
case ClanScheme.REMOTE.value(url):
assert url == "https://example.com?password=1234" # type: ignore
case _:
assert False