From d7c5850f2fcc2b7db354f19dea0680c304c17bcd Mon Sep 17 00:00:00 2001 From: Qubasa Date: Tue, 2 Jan 2024 14:55:20 +0100 Subject: [PATCH] clan_uri: Support all other formats by just differentiating between remote and local --- pkgs/clan-cli/clan_cli/clan_uri.py | 40 ++++++--------------------- pkgs/clan-cli/clan_cli/history/add.py | 1 - pkgs/clan-cli/tests/test_clan_uri.py | 23 +++++---------- 3 files changed, 15 insertions(+), 49 deletions(-) diff --git a/pkgs/clan-cli/clan_cli/clan_uri.py b/pkgs/clan-cli/clan_cli/clan_uri.py index 264d911d3..276562814 100644 --- a/pkgs/clan-cli/clan_cli/clan_uri.py +++ b/pkgs/clan-cli/clan_cli/clan_uri.py @@ -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}") diff --git a/pkgs/clan-cli/clan_cli/history/add.py b/pkgs/clan-cli/clan_cli/history/add.py index c753b6f3b..9f4cc5be6 100644 --- a/pkgs/clan-cli/clan_cli/history/add.py +++ b/pkgs/clan-cli/clan_cli/history/add.py @@ -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 diff --git a/pkgs/clan-cli/tests/test_clan_uri.py b/pkgs/clan-cli/tests/test_clan_uri.py index 8a0b9d040..9302d91ca 100644 --- a/pkgs/clan-cli/tests/test_clan_uri.py +++ b/pkgs/clan-cli/tests/test_clan_uri.py @@ -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