Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor CLI components #28

Merged
merged 8 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,084 changes: 446 additions & 638 deletions hera_librarian/cli.py

Large diffs are not rendered by default.

23 changes: 23 additions & 0 deletions hera_librarian/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from .models.ping import PingRequest, PingResponse
from .models.uploads import (UploadCompletionRequest, UploadInitiationRequest,
UploadInitiationResponse)
from .settings import ClientInfo
from .utils import get_md5_from_path, get_size_from_path

if TYPE_CHECKING:
Expand Down Expand Up @@ -53,6 +54,28 @@ def __init__(self, host: str, port: int, user: str):
def __repr__(self):
return f"Librarian Client ({self.user}) for {self.host}:{self.port}"

@classmethod
def from_info(cls, client_info: ClientInfo):
"""
Create a LibrarianClient from a ClientInfo object.

Parameters
----------
client_info : ClientInfo
The ClientInfo object.

Returns
-------
LibrarianClient
The LibrarianClient.
"""

return cls(
host=client_info.host,
port=client_info.port,
user=client_info.user,
)

@property
def hostname(self):
return f"{self.host}:{self.port}/api/v2"
Expand Down
7 changes: 7 additions & 0 deletions hera_librarian/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,10 @@ def __init__(self, url, status_code, reason, suggested_remedy):
class LibrarianError(Exception):
def __init__(self, message):
super(LibrarianError, self).__init__(message)


class LibrarianClientRemovedFunctionality(Exception):
def __Init__(self, name, message):
super(LibrarianClientRemovedFunctionality, self).__init__(
f"{name} is no longer avaialble in Librarian v2.0. {message}"
)
89 changes: 89 additions & 0 deletions hera_librarian/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
"""
Client settings.
"""

import os
from pathlib import Path

from pydantic import BaseModel
from pydantic_settings import BaseSettings

from typing import TYPE_CHECKING

if TYPE_CHECKING:
client_settings: "ClientSettings"


class ClientInfo(BaseModel):
"""
Information for an individual client.
"""

user: str
"Your username on this librarian"
port: int
"The port of this librarian server"
host: str
"The hostname of this librarian server"


class ClientSettings(BaseSettings):
connections: dict[str, ClientInfo] = {}

@classmethod
def from_file(cls, config_path: Path | str) -> "ClientSettings":
"""
Loads the settings from the given path.
"""

with open(config_path, "r") as handle:
return cls.model_validate_json(handle.read())


# Automatically create a settings object on use.

_settings = None


def load_settings() -> ClientSettings:
"""
Load the settings from the config file.
"""

global _settings

try_paths = [
os.environ.get("HL_CLIENT_CONFIG", None),
Path.home() / ".hl_client.cfg",
Path.home() / ".hl_client.json",
]

for path in try_paths:
if path is not None:
path = Path(path)
else:
continue

if path.exists():
_settings = ClientSettings.from_file(path)
return _settings

_settings = ClientSettings()

return _settings


def __getattr__(name):
"""
Try to load the settings if they haven't been loaded yet.
"""

if name == "client_settings":
global _settings

if _settings is not None:
return _settings

return load_settings()

raise AttributeError(f"module '{__name__}' has no attribute '{name}'")
30 changes: 0 additions & 30 deletions hera_librarian/tests/__init__.py

This file was deleted.

217 changes: 0 additions & 217 deletions hera_librarian/tests/test_base_store.py

This file was deleted.

Binary file not shown.
Binary file not shown.

This file was deleted.

Loading