Configuration¶
Stored API keys. See API keys.
provesid.config
¶
Configuration management for PROVESID API keys and settings. Provides persistent storage for API keys and user preferences.
The keys live in config.json under $XDG_CONFIG_HOME/provesid (by
default ~/.config/provesid), or %APPDATA%\PROVESID on Windows, as
plain text. Only CAS Common Chemistry needs one today:
CASCommonChem reads it when no key or key
file is passed, and before the CCC_API_KEY and CAS_API_KEY
environment variables --- so a stored key wins over the environment.
Examples:
>>> from provesid.config import get_config_manager
>>> get_config_manager().config_file.name
'config.json'
Classes¶
ConfigManager
¶
Manages persistent configuration for PROVESID.
Every read goes to the file, so a key set in another process is seen at
once. Use get_config_manager for
the shared instance.
Examples:
>>> manager = ConfigManager()
>>> manager.set_api_key("example", "not-a-real-key")
>>> manager.get_api_key("example")
'not-a-real-key'
>>> manager.remove_api_key("example")
True
Source code in src/provesid/config.py
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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 | |
Methods:¶
__init__()
¶
Initialize configuration manager with default paths.
Creates the configuration directory if it is missing; a failure to create it is logged, not raised.
Examples:
>>> ConfigManager().config_dir.name in ("provesid", "PROVESID")
True
Source code in src/provesid/config.py
44 45 46 47 48 49 50 51 52 53 54 55 56 57 | |
load_config()
¶
Load configuration from file.
Returns:
| Type | Description |
|---|---|
dict
|
The file's contents; empty when there is no file or it cannot be read (the latter logged). |
Examples:
>>> isinstance(ConfigManager().load_config(), dict)
True
Source code in src/provesid/config.py
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | |
save_config(config)
¶
Save configuration to file, replacing what is there.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
Dict[str, Any]
|
The whole configuration. Keys not in it are lost; to change one entry, load, modify and save. |
required |
Note
A failure to write is logged at ERROR, not raised.
Examples:
>>> manager = ConfigManager()
>>> config = manager.load_config()
>>> manager.save_config(config)
Source code in src/provesid/config.py
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | |
get_api_key(service)
¶
Get API key for a specific service.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
service
|
str
|
Service name, e.g. |
required |
Returns:
| Type | Description |
|---|---|
Optional[str]
|
The stored key, or None when there is none. |
Examples:
>>> ConfigManager().get_api_key("no-such-service") is None
True
Source code in src/provesid/config.py
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | |
set_api_key(service, api_key)
¶
Set API key for a specific service, replacing any stored one.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
service
|
str
|
Service name, e.g. |
required |
api_key
|
str
|
The key; surrounding whitespace is stripped. |
required |
Examples:
>>> manager = ConfigManager()
>>> manager.set_api_key("example", " not-a-real-key ")
>>> manager.get_api_key("example")
'not-a-real-key'
>>> manager.remove_api_key("example")
True
Source code in src/provesid/config.py
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 | |
remove_api_key(service)
¶
Remove API key for a specific service.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
service
|
str
|
Service name. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if a key was removed, False if none was stored. |
Examples:
>>> ConfigManager().remove_api_key("no-such-service")
False
Source code in src/provesid/config.py
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 | |
list_configured_services()
¶
List all services with configured API keys.
Returns:
| Type | Description |
|---|---|
list
|
Service names, in the order they were first stored. |
Examples:
>>> manager = ConfigManager()
>>> manager.set_api_key("example", "not-a-real-key")
>>> "example" in manager.list_configured_services()
True
>>> manager.remove_api_key("example")
True
Source code in src/provesid/config.py
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 | |
get_config_info()
¶
Get information about the configuration.
Returns:
| Type | Description |
|---|---|
dict
|
|
Examples:
>>> sorted(ConfigManager().get_config_info())
['config_directory', 'config_exists', 'config_file', 'configured_services']
Source code in src/provesid/config.py
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 | |
Functions:¶
get_config_manager()
¶
Get the global configuration manager instance.
Built on first call; the same object every time after.
Returns:
| Type | Description |
|---|---|
ConfigManager
|
The shared instance. |
Examples:
>>> get_config_manager() is get_config_manager()
True
Source code in src/provesid/config.py
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 | |
set_cas_api_key(api_key)
¶
Set CAS Common Chemistry API key for persistent storage
CASCommonChem uses the stored
key from then on. The file's path is logged at INFO.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
api_key
|
str
|
Your CAS API key |
required |
Returns:
| Type | Description |
|---|---|
Path
|
The config file the key was written to. |
Note
This replaces any key already stored, without asking.
Examples:
>>> from provesid.config import set_cas_api_key
>>> set_cas_api_key("your-cas-api-key-here")
PosixPath('/home/me/.config/provesid/config.json')
Source code in src/provesid/config.py
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 | |
get_cas_api_key()
¶
Get the stored CAS API key.
Only the config file is read; the CAS_API_KEY environment variable,
which CASCommonChem consults after this
file, is not.
Returns:
| Type | Description |
|---|---|
Optional[str]
|
The key, or None when none is stored. |
Examples:
>>> key = get_cas_api_key()
>>> key is None or isinstance(key, str)
True
Source code in src/provesid/config.py
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 | |
remove_cas_api_key()
¶
Remove the stored CAS API key.
Returns:
| Type | Description |
|---|---|
bool
|
True if a key was stored and is now gone, False if none was stored. |
Examples:
>>> remove_cas_api_key()
True
Source code in src/provesid/config.py
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 | |
show_config()
¶
The configuration's location and which services have keys.
The same as get_config_manager().get_config_info(). The keys
themselves are not included.
Returns:
| Type | Description |
|---|---|
dict
|
|
Examples:
>>> show_config()
{'config_directory': '/home/me/.config/provesid',
'config_file': '/home/me/.config/provesid/config.json',
'config_exists': True,
'configured_services': ['cas']}
Source code in src/provesid/config.py
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 | |