clan_cli: Added ClanURI class parser
This commit is contained in:
parent
0cadbe0f1d
commit
63c820ed86
50
pkgs/clan-cli/clan_cli/clan_uri.py
Normal file
50
pkgs/clan-cli/clan_cli/clan_uri.py
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
# Import the urllib.parse module
|
||||||
|
import urllib.parse
|
||||||
|
from enum import Enum
|
||||||
|
|
||||||
|
from .errors import ClanError
|
||||||
|
|
||||||
|
|
||||||
|
class ClanScheme(Enum):
|
||||||
|
HTTP = "http"
|
||||||
|
HTTPS = "https"
|
||||||
|
FILE = "file"
|
||||||
|
|
||||||
|
|
||||||
|
# Define the ClanURI class
|
||||||
|
class ClanURI:
|
||||||
|
# Initialize the class with a clan:// URI
|
||||||
|
def __init__(self, uri: str) -> None:
|
||||||
|
if uri.startswith("clan://"):
|
||||||
|
uri = uri[7:]
|
||||||
|
else:
|
||||||
|
raise ClanError("Invalid scheme: expected clan://, got {}".format(uri))
|
||||||
|
|
||||||
|
# Parse the URI into components
|
||||||
|
self.components = urllib.parse.urlparse(uri)
|
||||||
|
|
||||||
|
try:
|
||||||
|
self.scheme = ClanScheme(self.components.scheme)
|
||||||
|
except ValueError:
|
||||||
|
raise ClanError("Unsupported scheme: {}".format(self.components.scheme))
|
||||||
|
|
||||||
|
# Define a method to get the path of the URI
|
||||||
|
@property
|
||||||
|
def path(self) -> str:
|
||||||
|
return self.components.path
|
||||||
|
|
||||||
|
@property
|
||||||
|
def url(self) -> str:
|
||||||
|
return self.components.geturl()
|
||||||
|
|
||||||
|
# Define a method to check if the URI is a remote HTTP URL
|
||||||
|
def is_remote(self) -> bool:
|
||||||
|
match self.scheme:
|
||||||
|
case ClanScheme.HTTP | ClanScheme.HTTPS:
|
||||||
|
return True
|
||||||
|
case ClanScheme.FILE:
|
||||||
|
return False
|
||||||
|
|
||||||
|
# Define a method to check if the URI is a local path
|
||||||
|
def is_local(self) -> bool:
|
||||||
|
return not self.is_remote()
|
34
pkgs/clan-cli/tests/test_clan_uri.py
Normal file
34
pkgs/clan-cli/tests/test_clan_uri.py
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
import pytest
|
||||||
|
|
||||||
|
from clan_cli.clan_uri import ClanURI
|
||||||
|
from clan_cli.errors import ClanError
|
||||||
|
|
||||||
|
|
||||||
|
def test_local_uri() -> None:
|
||||||
|
# Create a ClanURI object from a local URI
|
||||||
|
uri = ClanURI("clan://file:///home/user/Downloads")
|
||||||
|
# Check that the URI is local
|
||||||
|
assert uri.is_local()
|
||||||
|
# Check that the URI is not remote
|
||||||
|
assert not uri.is_remote()
|
||||||
|
# Check that the URI path is correct
|
||||||
|
assert uri.path == "/home/user/Downloads"
|
||||||
|
|
||||||
|
|
||||||
|
def test_unsupported_schema() -> None:
|
||||||
|
with pytest.raises(ClanError, match="Unsupported scheme: ftp"):
|
||||||
|
# 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")
|
||||||
|
# Check that the URI is remote
|
||||||
|
assert uri.is_remote()
|
||||||
|
# Check that the URI is not local
|
||||||
|
assert not uri.is_local()
|
||||||
|
# Check that the URI path is correct
|
||||||
|
assert uri.path == ""
|
||||||
|
|
||||||
|
assert uri.url == "https://example.com"
|
Loading…
Reference in New Issue
Block a user