-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfig.py
More file actions
73 lines (56 loc) · 2.53 KB
/
config.py
File metadata and controls
73 lines (56 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# -*- coding: utf-8 -*-
import os
from pathlib import Path
from typing import TYPE_CHECKING
from indico.errors import IndicoInvalidConfigSetting
if TYPE_CHECKING: # pragma: no cover
from typing import Any, Optional, Tuple, Union
from indico.typing import AnyDict
class IndicoConfig:
"""
Configuration for the IndicoClient.
Args:
host= (str, optional): Indico Platform hostname (eg mycluster.indico.io )
api_token_path= (str, optional): Path to the Indico API token file indico_api_token.txt. Defaults to user's home directory. Ignored if api_token is provided.
api_token= (str, optional): The actual text of the API Token. Takes precedence over api_token_path
verify_ssl= (bool, optional): Whether to verify the host's SSL certificate. Default=True
requests_params= (dict, optional): Dictionary of httpx Client parameters to set
enable_http2= (bool, optional): Enable HTTP/2 for async client. Default=False (HTTP/1.1).
Returns:
IndicoConfig object
Raises:
RuntimeError: If api_token_path does not exist.
"""
def __init__(self, **kwargs: "Any"):
self.host: str = os.getenv("INDICO_HOST", "")
self.protocol: str = os.getenv("INDICO_PROTOCOL", "https")
self.serializer: str = os.getenv("INDICO_SERIALIZER", "msgpack")
self.api_token_path: "Union[str, Path]" = os.getenv(
"INDICO_API_TOKEN_PATH", Path.home()
)
self.api_token: "Optional[str]" = os.getenv("INDICO_API_TOKEN")
self.verify_ssl: bool = True
self.requests_params: "Optional[AnyDict]" = None
self._disable_cookie_domain: bool = False
self.enable_http2: bool = False
for key, value in kwargs.items():
if hasattr(self, key):
setattr(self, key, value)
else:
raise IndicoInvalidConfigSetting(key)
if not self.api_token:
self.api_token_path, self.api_token = self._resolve_api_token()
def _resolve_api_token(self) -> "Tuple[Path, str]":
path = self.api_token_path
if not isinstance(path, Path):
path = Path(path)
if not path.exists():
path = Path.home()
if not path.is_file():
path = path / "indico_api_token.txt"
if not path.exists():
raise RuntimeError(
"Expected indico_api_token.txt in home directory, "
"or provided as indicoio.config.api_token_path"
)
return path, path.read_text().strip()