|
| 1 | +# SPDX-License-Identifier: BSD-3-Clause |
| 2 | +# Copyright (c) 2025 SciCat Project (https://github.com/SciCatProject/scitacean) |
| 3 | +"""Profiles for connecting to SciCat.""" |
| 4 | + |
| 5 | +from dataclasses import dataclass |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | +import tomli |
| 9 | + |
| 10 | +from .transfer.copy import CopyFileTransfer |
| 11 | +from .transfer.link import LinkFileTransfer |
| 12 | +from .transfer.select import SelectFileTransfer |
| 13 | +from .transfer.sftp import SFTPFileTransfer |
| 14 | +from .typing import FileTransfer |
| 15 | + |
| 16 | + |
| 17 | +@dataclass(frozen=True, slots=True) |
| 18 | +class Profile: |
| 19 | + """Parameters for connecting to a specific instance of SciCat. |
| 20 | +
|
| 21 | + The fields of a profile correspond to the arguments of the constructors |
| 22 | + of :class:`Client`. |
| 23 | + They can be overridden by the explicit constructor arguments. |
| 24 | +
|
| 25 | + When constructing a client from a profile, the ``profile`` argument may be one of: |
| 26 | +
|
| 27 | + - If ``profile`` is a :class:`Profile`, it is returned as is. |
| 28 | + - If ``profile`` is a :class:`pathlib.Path`, a profile is loaded from |
| 29 | + the file at that path. |
| 30 | + - If ``profile`` is a :class:`str` |
| 31 | + * and ``profile`` matches the name of a builtin profile, |
| 32 | + that profile is returned. |
| 33 | + * Otherwise, a profile is loaded from a file with this path, potentially |
| 34 | + by appending ``".profile.toml"`` to the name. |
| 35 | + """ |
| 36 | + |
| 37 | + url: str |
| 38 | + """URL of the SciCat API. |
| 39 | +
|
| 40 | + Note that this is the URL to the API, *not* the web interface. |
| 41 | + For example, at ESS, the web interface URL is ``"https://scicat.ess.eu"``. |
| 42 | + But the API URL is ``"https://scicat.ess.eu/api/v3"`` (at the time of writing). |
| 43 | + """ |
| 44 | + file_transfer: FileTransfer | None |
| 45 | + """A file transfer object for uploading and downloading files. |
| 46 | +
|
| 47 | + See the `File transfer <../../reference/index.rst#file-transfer>`_. section for |
| 48 | + implementations provided by Scitacean. |
| 49 | + """ |
| 50 | + |
| 51 | + |
| 52 | +def locate_profile(spec: str | Path | Profile) -> Profile: |
| 53 | + """Find and return a specified profile.""" |
| 54 | + if isinstance(spec, Profile): |
| 55 | + return spec |
| 56 | + |
| 57 | + if isinstance(spec, Path): |
| 58 | + return _load_profile_from_file(spec) |
| 59 | + |
| 60 | + try: |
| 61 | + return _builtin_profile(spec) |
| 62 | + except KeyError: |
| 63 | + pass |
| 64 | + |
| 65 | + try: |
| 66 | + return _load_profile_from_file(spec) |
| 67 | + except FileNotFoundError: |
| 68 | + if spec.endswith(".profile.toml"): |
| 69 | + raise ValueError(f"Unknown profile: {spec}") from None |
| 70 | + |
| 71 | + try: |
| 72 | + return _load_profile_from_file(f"{spec}.profile.toml") |
| 73 | + except FileNotFoundError: |
| 74 | + raise ValueError(f"Unknown profile: {spec}") from None |
| 75 | + |
| 76 | + |
| 77 | +def _builtin_profile(name: str) -> Profile: |
| 78 | + match name: |
| 79 | + case "ess": |
| 80 | + return Profile( |
| 81 | + url="https://scicat.ess.eu/api/v3", file_transfer=_ess_file_transfer() |
| 82 | + ) |
| 83 | + case "staging.ess": |
| 84 | + return Profile( |
| 85 | + url="https://staging.scicat.ess.eu/api/v3", |
| 86 | + file_transfer=_ess_file_transfer(), |
| 87 | + ) |
| 88 | + raise KeyError(f"Unknown builtin profile: {name}") |
| 89 | + |
| 90 | + |
| 91 | +def _ess_file_transfer() -> FileTransfer: |
| 92 | + return SelectFileTransfer( |
| 93 | + [ |
| 94 | + LinkFileTransfer(), |
| 95 | + CopyFileTransfer(), |
| 96 | + SFTPFileTransfer(host="login.esss.dk"), |
| 97 | + ] |
| 98 | + ) |
| 99 | + |
| 100 | + |
| 101 | +def _load_profile_from_file(name: str | Path) -> Profile: |
| 102 | + with open(name, "rb") as file: |
| 103 | + contents = tomli.load(file) |
| 104 | + return Profile(url=contents["url"], file_transfer=None) |
| 105 | + |
| 106 | + |
| 107 | +@dataclass(frozen=True, slots=True) |
| 108 | +class ClientParams: |
| 109 | + """Parameters for creating a client.""" |
| 110 | + |
| 111 | + url: str |
| 112 | + file_transfer: FileTransfer | None |
| 113 | + |
| 114 | + |
| 115 | +def make_client_params( |
| 116 | + *, |
| 117 | + profile: str | Path | Profile | None, |
| 118 | + url: str | None, |
| 119 | + file_transfer: FileTransfer | None, |
| 120 | +) -> ClientParams: |
| 121 | + """Return parameters for creating a client.""" |
| 122 | + p = locate_profile(profile) if profile is not None else None |
| 123 | + url = _get_url(p, url) |
| 124 | + file_transfer = _get_file_transfer(p, file_transfer) |
| 125 | + return ClientParams(url=url, file_transfer=file_transfer) |
| 126 | + |
| 127 | + |
| 128 | +def _get_url(profile: Profile | None, url: str | None) -> str: |
| 129 | + match (profile, url): |
| 130 | + case (None, None): |
| 131 | + raise TypeError("Either `profile` or `url` must be provided") |
| 132 | + case (p, None): |
| 133 | + return p.url # type: ignore[union-attr] |
| 134 | + case _: |
| 135 | + return url # type: ignore[return-value] |
| 136 | + |
| 137 | + |
| 138 | +def _get_file_transfer( |
| 139 | + profile: Profile | None, |
| 140 | + file_transfer: FileTransfer | None, |
| 141 | +) -> FileTransfer | None: |
| 142 | + if profile is None: |
| 143 | + return file_transfer |
| 144 | + if file_transfer is None: |
| 145 | + return profile.file_transfer |
| 146 | + return file_transfer |
0 commit comments