|
3 | 3 | """ |
4 | 4 |
|
5 | 5 | import importlib |
| 6 | +import logging |
| 7 | +import os |
| 8 | +import yaml |
| 9 | +from pydantic import BaseModel, Field |
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 | + |
| 31 | +class DSConfig(BaseModel): |
| 32 | + """ |
| 33 | + Helper class with DataSHIELD configuration details. |
| 34 | + """ |
| 35 | + |
| 36 | + servers: list[DSLoginInfo] = Field(default_factory=list) |
| 37 | + |
| 38 | + model_config = {"extra": "forbid"} |
| 39 | + |
| 40 | + @classmethod |
| 41 | + def load(cls) -> "DSConfig": |
| 42 | + """ |
| 43 | + Load the DataSHIELD configuration from default configuration files. The file must contain |
| 44 | + a list of servers with their login details. The configuration from the first file found will be loaded, |
| 45 | + in order of precedence. If multiple files are found, the configurations will be merged, with new server |
| 46 | + details replacing existing ones by name. |
| 47 | +
|
| 48 | + :return: The DataSHIELD configuration object |
| 49 | + """ |
| 50 | + merged_config = None |
| 51 | + for config_file in CONFIG_FILES: |
| 52 | + try: |
| 53 | + # check file exists and is readable, if not, silently ignore |
| 54 | + if not os.path.exists(config_file): |
| 55 | + continue |
| 56 | + if not os.access(config_file, os.R_OK): |
| 57 | + continue |
| 58 | + config = cls.load_from_file(config_file) |
| 59 | + if merged_config is None: |
| 60 | + merged_config = config |
| 61 | + else: |
| 62 | + # merge servers by name, new ones replacing existing ones, and keep the rest of existing ones |
| 63 | + existing_servers = {x.name: x for x in merged_config.servers} |
| 64 | + for server in config.servers: |
| 65 | + existing_servers[server.name] = server |
| 66 | + merged_config.servers = list(existing_servers.values()) |
| 67 | + except Exception as e: |
| 68 | + # silently ignore errors, e.g. file not found or invalid format |
| 69 | + logging.error(f"Failed to load login information from {config_file}: {e}") |
| 70 | + return merged_config if merged_config else cls() |
| 71 | + |
| 72 | + @classmethod |
| 73 | + def load_from_file(cls, file: str) -> "DSConfig": |
| 74 | + """ |
| 75 | + Load the DataSHIELD configuration from a YAML file. The file must contain a list of servers with their login details. |
| 76 | +
|
| 77 | + :param file: The path to the YAML file containing the DataSHIELD configuration |
| 78 | + :return: The DataSHIELD configuration object |
| 79 | + """ |
| 80 | + with open(file) as f: |
| 81 | + config_data = yaml.safe_load(f) |
| 82 | + |
| 83 | + if config_data is None: |
| 84 | + config_data = {} |
| 85 | + |
| 86 | + return cls.model_validate(config_data) |
31 | 87 |
|
32 | 88 |
|
33 | 89 | class DSResult: |
@@ -409,7 +465,7 @@ def new_connection(cls, args: DSLoginInfo, restore: str = None) -> DSConnection: |
409 | 465 | raise NotImplementedError("DSConnection function not available") |
410 | 466 |
|
411 | 467 | @classmethod |
412 | | - def load_class(cls, name: str) -> any: |
| 468 | + def load_class(cls, name: str) -> type["DSDriver"]: |
413 | 469 | """ |
414 | 470 | Load a class from its fully qualified name (dot separated). |
415 | 471 |
|
|
0 commit comments