clan_uri: Support all other formats by just differentiating between remote and local
This commit is contained in:
@@ -10,38 +10,24 @@ from typing import Self
|
|||||||
from .errors import ClanError
|
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
|
# Define an enum with different members that have different values
|
||||||
class ClanScheme(Enum):
|
class ClanScheme(Enum):
|
||||||
# Use the dataclass decorator to add fields and methods to the members
|
# Use the dataclass decorator to add fields and methods to the members
|
||||||
@member
|
@member
|
||||||
@dataclass
|
@dataclass
|
||||||
class HTTP:
|
class REMOTE:
|
||||||
url: str # The url field holds the HTTP URL
|
url: str # The url field holds the HTTP URL
|
||||||
|
|
||||||
def __str__(self) -> str:
|
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
|
@member
|
||||||
@dataclass
|
@dataclass
|
||||||
class FILE:
|
class LOCAL:
|
||||||
path: Path # The path field holds the local path
|
path: Path # The path field holds the local path
|
||||||
|
|
||||||
def __str__(self) -> str:
|
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
|
# Parameters defined here will be DELETED from the nested uri
|
||||||
@@ -96,26 +82,16 @@ class ClanURI:
|
|||||||
)
|
)
|
||||||
|
|
||||||
match comb:
|
match comb:
|
||||||
case ("http" | "https", _, _, _, _, _):
|
|
||||||
self.scheme = ClanScheme.HTTP.value(self._components.geturl()) # type: ignore
|
|
||||||
case ("file", "", path, "", "", "") | ("", "", path, "", "", ""): # 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 _:
|
case _:
|
||||||
raise ClanError(f"Unsupported uri components: {comb}")
|
self.scheme = ClanScheme.REMOTE.value(self._components.geturl()) # type: ignore
|
||||||
|
|
||||||
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)
|
|
||||||
|
|
||||||
def get_internal(self) -> str:
|
def get_internal(self) -> str:
|
||||||
match self.scheme:
|
match self.scheme:
|
||||||
case ClanScheme.FILE.value(path):
|
case ClanScheme.LOCAL.value(path):
|
||||||
return str(path)
|
return str(path)
|
||||||
case ClanScheme.HTTP.value(url):
|
case ClanScheme.REMOTE.value(url):
|
||||||
return url
|
return url
|
||||||
case _:
|
case _:
|
||||||
raise ClanError(f"Unsupported uri components: {self.scheme}")
|
raise ClanError(f"Unsupported uri components: {self.scheme}")
|
||||||
|
|||||||
@@ -65,7 +65,6 @@ def list_history() -> list[HistoryEntry]:
|
|||||||
|
|
||||||
# TODO: Add all vm entries to history
|
# TODO: Add all vm entries to history
|
||||||
def add_history(uri: ClanURI) -> list[HistoryEntry]:
|
def add_history(uri: ClanURI) -> list[HistoryEntry]:
|
||||||
uri.check_exits()
|
|
||||||
user_history_file().parent.mkdir(parents=True, exist_ok=True)
|
user_history_file().parent.mkdir(parents=True, exist_ok=True)
|
||||||
logs = list_history()
|
logs = list_history()
|
||||||
found = False
|
found = False
|
||||||
|
|||||||
@@ -1,9 +1,6 @@
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
import pytest
|
|
||||||
|
|
||||||
from clan_cli.clan_uri import ClanParameters, ClanScheme, ClanURI
|
from clan_cli.clan_uri import ClanParameters, ClanScheme, ClanURI
|
||||||
from clan_cli.errors import ClanError
|
|
||||||
|
|
||||||
|
|
||||||
def test_get_internal() -> None:
|
def test_get_internal() -> None:
|
||||||
@@ -25,24 +22,18 @@ def test_local_uri() -> None:
|
|||||||
# Create a ClanURI object from a local URI
|
# Create a ClanURI object from a local URI
|
||||||
uri = ClanURI("clan://file:///home/user/Downloads")
|
uri = ClanURI("clan://file:///home/user/Downloads")
|
||||||
match uri.scheme:
|
match uri.scheme:
|
||||||
case ClanScheme.FILE.value(path):
|
case ClanScheme.LOCAL.value(path):
|
||||||
assert path == Path("/home/user/Downloads") # type: ignore
|
assert path == Path("/home/user/Downloads") # type: ignore
|
||||||
case _:
|
case _:
|
||||||
assert False
|
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:
|
def test_is_remote() -> None:
|
||||||
# Create a ClanURI object from a remote URI
|
# Create a ClanURI object from a remote URI
|
||||||
uri = ClanURI("clan://https://example.com")
|
uri = ClanURI("clan://https://example.com")
|
||||||
|
|
||||||
match uri.scheme:
|
match uri.scheme:
|
||||||
case ClanScheme.HTTP.value(url):
|
case ClanScheme.REMOTE.value(url):
|
||||||
assert url == "https://example.com" # type: ignore
|
assert url == "https://example.com" # type: ignore
|
||||||
case _:
|
case _:
|
||||||
assert False
|
assert False
|
||||||
@@ -67,7 +58,7 @@ def test_remote_with_clanparams() -> None:
|
|||||||
assert uri.params.flake_attr == "defaultVM"
|
assert uri.params.flake_attr == "defaultVM"
|
||||||
|
|
||||||
match uri.scheme:
|
match uri.scheme:
|
||||||
case ClanScheme.HTTP.value(url):
|
case ClanScheme.REMOTE.value(url):
|
||||||
assert url == "https://example.com" # type: ignore
|
assert url == "https://example.com" # type: ignore
|
||||||
case _:
|
case _:
|
||||||
assert False
|
assert False
|
||||||
@@ -81,7 +72,7 @@ def test_from_path_with_custom() -> None:
|
|||||||
assert uri.params.flake_attr == "myVM"
|
assert uri.params.flake_attr == "myVM"
|
||||||
|
|
||||||
match uri.scheme:
|
match uri.scheme:
|
||||||
case ClanScheme.FILE.value(path):
|
case ClanScheme.LOCAL.value(path):
|
||||||
assert path == Path("/home/user/Downloads") # type: ignore
|
assert path == Path("/home/user/Downloads") # type: ignore
|
||||||
case _:
|
case _:
|
||||||
assert False
|
assert False
|
||||||
@@ -95,7 +86,7 @@ def test_from_path_with_default() -> None:
|
|||||||
assert uri.params.flake_attr == "defaultVM"
|
assert uri.params.flake_attr == "defaultVM"
|
||||||
|
|
||||||
match uri.scheme:
|
match uri.scheme:
|
||||||
case ClanScheme.FILE.value(path):
|
case ClanScheme.LOCAL.value(path):
|
||||||
assert path == Path("/home/user/Downloads") # type: ignore
|
assert path == Path("/home/user/Downloads") # type: ignore
|
||||||
case _:
|
case _:
|
||||||
assert False
|
assert False
|
||||||
@@ -109,7 +100,7 @@ def test_from_str() -> None:
|
|||||||
assert uri.params.flake_attr == "myVM"
|
assert uri.params.flake_attr == "myVM"
|
||||||
|
|
||||||
match uri.scheme:
|
match uri.scheme:
|
||||||
case ClanScheme.HTTP.value(url):
|
case ClanScheme.REMOTE.value(url):
|
||||||
assert url == "https://example.com?password=asdasd&test=1234" # type: ignore
|
assert url == "https://example.com?password=asdasd&test=1234" # type: ignore
|
||||||
case _:
|
case _:
|
||||||
assert False
|
assert False
|
||||||
@@ -137,7 +128,7 @@ def test_remote_with_all_params() -> None:
|
|||||||
assert uri.params.flake_attr == "myVM"
|
assert uri.params.flake_attr == "myVM"
|
||||||
|
|
||||||
match uri.scheme:
|
match uri.scheme:
|
||||||
case ClanScheme.HTTP.value(url):
|
case ClanScheme.REMOTE.value(url):
|
||||||
assert url == "https://example.com?password=1234" # type: ignore
|
assert url == "https://example.com?password=1234" # type: ignore
|
||||||
case _:
|
case _:
|
||||||
assert False
|
assert False
|
||||||
|
|||||||
Reference in New Issue
Block a user