API Reference
Source code in config_file/config_file.py
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 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 | |
__init__(file_path)
Stores the config file path and reads the file contents.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
Union[str, Path]
|
Path to the configuration file. Supports tilde (~) expansion for home directory. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the specified file path does not have an extension that is supported or it is a directory. |
FileNotFoundError
|
If the specified file path does not exist. |
ParsingError
|
If the file_path could not be parsed. |
Source code in config_file/config_file.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | |
delete(key)
Deletes a section or key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The section, sub-section, or key to delete. |
required |
Raises:
| Type | Description |
|---|---|
KeyError
|
If a key is attempted to be deleted that |
Source code in config_file/config_file.py
113 114 115 116 117 118 119 120 121 122 123 | |
get(key, parse_types=False, return_type=None, default=Default(None))
Retrieve the value of a key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The key to retrieve. |
required |
parse_types
|
bool
|
Automatically parse ints, floats, booleans, dicts, and lists. This recursively parses all types in whatever you're retrieving, not just a single type. For example, if you are retrieving a section, all values in that section will be parsed. |
False
|
return_type
|
Union[Callable[[Any], Any], None]
|
The type to coerce the return value to. |
None
|
default
|
Any
|
The default value to return if the value of the key is empty. If not specified, a KeyError will be raised if the key does not exist. |
Default(None)
|
Returns:
| Type | Description |
|---|---|
Any
|
The value of the key. |
Raises:
| Type | Description |
|---|---|
KeyError
|
If a key is attempted to be retrieved that does not exist. |
ValueError
|
If the value is not able to be coerced into return_type. |
Source code in config_file/config_file.py
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 | |
has(key, wild=False)
Check if a section, sub-section, or key exists.
Some formats, like JSON, do not have sections and therefore, it would only be checking if a particular key exists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The section, sub-section, or key to find. |
required |
wild
|
bool
|
Whether or not to search everywhere. |
False
|
Returns:
| Type | Description |
|---|---|
bool
|
True if the key exists. False otherwise. |
Source code in config_file/config_file.py
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | |
restore_original(original_path=None)
Restores the original the configuration file.
The current one is deleted and the original is copied back in its place. The internal contents are then reset to the new file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
original_path
|
Union[str, Path, None]
|
The original file to reset to. |
None
|
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the original configuration file (whether |
OSError
|
If the current configuration path is not writable. |
Source code in config_file/config_file.py
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 | |
save()
Save your configuration changes.
This writes the file back out, including any changes you've made, to the specified path given from this object's constructor.
Source code in config_file/config_file.py
177 178 179 180 181 182 183 184 185 186 | |
set(key, value)
Sets the value of a key.
If the given key does not exist, it will be automatically created for you. That includes if there are multiple keys in a row that do not exist. e.g. set('exists.does_not.does_not.also_does_not', 5)
The behavior of how this is done, however, depends on the
file used. For example, with INI, subsections are not supported.
So it would create a key in the section exists called does_not
and set it to the value {'does_not': {'also_does_not': 5}}.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The section, sub-section, or key to delete. |
required |
value
|
Any
|
The value to set the key to. |
required |
Source code in config_file/config_file.py
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | |