API Reference
This module contains all YOCO functions to load and save configurations.
YOCO is based on Python dictionaries and YAML files to provide a simple, yet powerful way of configuring Python projects. YOCO supports specifying parameters through the command line, YAML-files or directly from a Python dictionary.
load_config(config_dict, current_dict=None, parent=None, search_paths=None)
Load a config dictionary.
If a key is already in current_dict, config_dict will overwrite it.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config_dict |
dict
|
Configuration dictionary to be parsed. |
required |
current_dict |
Optional[dict]
|
Current configuration dictionary to be updated, will not be changed. |
None
|
parent |
Optional[str]
|
Path of parent config. Used to resolve relative paths. |
None
|
search_paths |
Optional[List[str]]
|
Search paths used to resolve config files. See resolve_path for more information. |
None
|
Returns:
Type | Description |
---|---|
dict
|
Loaded / updated configuration dictionary. |
Source code in yoco.py
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 |
|
load_config_from_args(parser, args=None, search_paths=None)
Parse arguments and load configs into a config dictionary.
Strings following -- will be used as key. Dots in that string are used to access nested dictionaries. YAML will be used for type conversion of the value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
parser |
ArgumentParser
|
Parser used to parse known and unknown arguments. This function will handle both the known args that have been added before and it tries to parse all other args and integrate them into the config. |
required |
args |
Optional[list]
|
List of arguments to parse. If None, sys.argv[1:] is used. |
None
|
search_paths |
Optional[List[str]]
|
Search paths used to resolve config files. See resolve_path for more information. |
None
|
Returns:
Type | Description |
---|---|
dict
|
Loaded configuration dictionary. |
Source code in yoco.py
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 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 |
|
load_config_from_file(path, current_dict=None, parent=None, search_paths=None)
Load configuration from a file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
str
|
Path of YAML file to load. |
required |
current_dict |
Optional[dict]
|
Current configuration dictionary. Will not be modified. If None, an empty dictionary will be created. |
None
|
parent |
Optional[str]
|
Parent directory. If not None, path will be assumed to be relative to parent. |
None
|
search_paths |
Optional[List[str]]
|
Search paths used to resolve config files. See resolve_path for more information. |
None
|
Returns:
Type | Description |
---|---|
dict
|
Updated configuration dictionary. |
Source code in yoco.py
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 |
|
resolve_path(path, parent=None, search_paths=None)
Resolves a path to a full absolute path based on parent and search_paths.
This function considers paths of 5 different cases:
/...
Absolute path, nothing todo.~/...
Home dir, expand user../...
Relative to parent (current directory if parent is None).../...
Relative to parent (current directory if parent is None)....
Relative to search paths. If search_paths is None: relative to parent and current working directory is assumed, in that order (i.e.,search_paths=[".", ""]
).
Relative search paths such as "."
or "./"
will be relative to parent (or current
directory if parent is None). The empty search path ""
refers to the current
directory and is not automatically included.
I.e., if parent is None "."
and ""
refer to the same path. While if parent is
not None, "."
means relative to the parent and ""
means relative to current
directory.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
str
|
The path to resolve. |
required |
parent |
Optional[str]
|
Parent folder. Will be used for explicit relative paths (./ or ../). |
None
|
search_paths |
Optional[List[str]]
|
List of search paths to prepend relative paths which are not explicitly relative to current directory. If None, no search paths are assumed. |
None
|
Returns:
Type | Description |
---|---|
str
|
The normalized resolved path. Original path, if path could not be resolved. |
Source code in yoco.py
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 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
|
save_config_to_file(path, config_dict)
Save config dictionary as a yaml file.
Source code in yoco.py
256 257 258 259 |
|