|
3 | 3 | """ |
4 | 4 |
|
5 | 5 | import importlib |
| 6 | +import logging |
| 7 | +import os |
| 8 | +import yaml |
| 9 | +from pydantic import BaseModel, Field, model_validator |
6 | 10 |
|
| 11 | +# Default configuration file paths to look for DataSHIELD login information, in order of precedence |
| 12 | +CONFIG_FILES = ["~/.config/datashield/config.yaml", "./.datashield/config.yaml"] |
7 | 13 |
|
8 | | -class DSLoginInfo: |
| 14 | + |
| 15 | +class DSLoginInfo(BaseModel): |
9 | 16 | """ |
10 | 17 | Helper class with DataSHIELD login details. |
11 | 18 | """ |
12 | 19 |
|
13 | | - def __init__( |
14 | | - self, |
15 | | - name: str, |
16 | | - url: str, |
17 | | - user: str = None, |
18 | | - password: str = None, |
19 | | - token: str = None, |
20 | | - profile: str = "default", |
21 | | - driver: str = "datashield_opal.OpalDriver", |
22 | | - ): |
23 | | - self.items = [] |
24 | | - self.name = name |
25 | | - self.url = url |
26 | | - self.user = user |
27 | | - self.password = password |
28 | | - self.token = token |
29 | | - self.profile = profile if profile is not None else "default" |
30 | | - self.driver = driver if driver is not None else "datashield_opal.OpalDriver" |
| 20 | + name: str |
| 21 | + url: str |
| 22 | + user: str | None = None |
| 23 | + password: str | None = None |
| 24 | + token: str | None = None |
| 25 | + profile: str = "default" |
| 26 | + driver: str = "datashield_opal.OpalDriver" |
| 27 | + |
| 28 | + model_config = {"extra": "forbid"} |
| 29 | + |
| 30 | + @model_validator(mode="after") |
| 31 | + def validate_credentials(self) -> "DSLoginInfo": |
| 32 | + if self.user is None and self.token is None: |
| 33 | + raise ValueError("Either user or token must be provided") |
| 34 | + return self |
| 35 | + |
| 36 | + |
| 37 | +class DSConfig(BaseModel): |
| 38 | + """ |
| 39 | + Helper class with DataSHIELD configuration details. |
| 40 | + """ |
| 41 | + |
| 42 | + servers: list[DSLoginInfo] = Field(default_factory=list) |
| 43 | + |
| 44 | + model_config = {"extra": "forbid"} |
| 45 | + |
| 46 | + @classmethod |
| 47 | + def load(cls) -> "DSConfig": |
| 48 | + """ |
| 49 | + Load the DataSHIELD configuration from the default configuration files. |
| 50 | + Each file must contain a list of servers with their login details. |
| 51 | + All readable configuration files listed in ``CONFIG_FILES`` are processed in |
| 52 | + order. Their configurations are merged, with servers identified by their |
| 53 | + ``name`` field. If the same server name appears in multiple files, the |
| 54 | + definition from the later file in the list takes precedence and replaces |
| 55 | + the earlier one. Servers that are only present in earlier files are kept. |
| 56 | +
|
| 57 | + :return: The DataSHIELD configuration object |
| 58 | + """ |
| 59 | + merged_config = None |
| 60 | + for config_file in CONFIG_FILES: |
| 61 | + try: |
| 62 | + # check file exists and is readable, if not, silently ignore |
| 63 | + path = os.path.expanduser(config_file) |
| 64 | + if not os.path.exists(path): |
| 65 | + continue |
| 66 | + if not os.access(path, os.R_OK): |
| 67 | + continue |
| 68 | + config = cls.load_from_file(path) |
| 69 | + if merged_config is None: |
| 70 | + merged_config = config |
| 71 | + else: |
| 72 | + # merge servers by name, new ones replacing existing ones, and keep the rest of existing ones |
| 73 | + existing_servers = {x.name: x for x in merged_config.servers} |
| 74 | + for server in config.servers: |
| 75 | + existing_servers[server.name] = server |
| 76 | + merged_config.servers = list(existing_servers.values()) |
| 77 | + except Exception: |
| 78 | + # log and ignore errors, e.g. file not found or invalid format |
| 79 | + logging.error(f"Failed to load login information from {config_file}") |
| 80 | + return merged_config if merged_config else cls() |
| 81 | + |
| 82 | + @classmethod |
| 83 | + def load_from_file(cls, file: str) -> "DSConfig": |
| 84 | + """ |
| 85 | + Load the DataSHIELD configuration from a YAML file. The file must contain a list of servers with their login details. |
| 86 | +
|
| 87 | + :param file: The path to the YAML file containing the DataSHIELD configuration |
| 88 | + :return: The DataSHIELD configuration object |
| 89 | + """ |
| 90 | + with open(file) as f: |
| 91 | + config_data = yaml.safe_load(f) |
| 92 | + |
| 93 | + if config_data is None: |
| 94 | + config_data = {} |
| 95 | + |
| 96 | + return cls.model_validate(config_data) |
31 | 97 |
|
32 | 98 |
|
33 | 99 | class DSResult: |
@@ -409,7 +475,7 @@ def new_connection(cls, args: DSLoginInfo, restore: str = None) -> DSConnection: |
409 | 475 | raise NotImplementedError("DSConnection function not available") |
410 | 476 |
|
411 | 477 | @classmethod |
412 | | - def load_class(cls, name: str) -> any: |
| 478 | + def load_class(cls, name: str) -> type["DSDriver"]: |
413 | 479 | """ |
414 | 480 | Load a class from its fully qualified name (dot separated). |
415 | 481 |
|
|
0 commit comments