blob: de1fa9924b6a38d5c28e6b20590041e14c7ef37f (
plain) (
blame)
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
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
|
# Config File
> Manage and manipulate your configuration files
[](https://www.python.org/downloads/release/python-380/)
[](https://pypi.org/project/config-file/)
[](https://pypi.org/project/black/)
[](https://travis-ci.com/eugenetriguba/config_file)
[](https://codecov.io/gh/eugenetriguba/config_file)
> This python package is currently a work in progress and is in a pre-alpha phase. The API is liable to break until v1.
Config File allows you to use the same simple API for manipulating INI, JSON,
YAML, and TOML configuration files. For the time being, it only supports INI.
## Installation
```bash
$ pip install config_file
```
## Example
### Sample Configuration File
`config.ini`
```ini
[calendar]
today = monday
start_week_on_sunday = false
today_index = 0
quarter_hours_passed = 0.25
```
### Create a ConfigFile
```python
from config_file import ConfigFile
config = ConfigFile("~/.config/test/config.ini")
```
### Output your config file as a string
```python
config.stringify()
>>> '[calendar]\ntoday = monday\nstart_week_on_sunday = false\ntoday_index = 0\nquarter_hours_passed = 0.25\n\n'
```
### Retrieve values
A section.key format is used for retrieving and setting values.
```python
config.get("calendar.today")
>>> 'monday'
# Values from the config file are automatically parsed
config.get("calendar.start_week_on_sunday")
>>> False
# Unless you don't want them to be parsed
config.get("calendar.today_index", parse_type=False)
>>> '0'
```
### Set values
```python
config.set("calendar.today_index", 20)
>>> True
config.stringify()
>>> '[calendar]\ntoday = monday\nstart_week_on_sunday = false\ntoday_index = 20\nquarter_hours_passed = 0.25\n\n'
# If you specify a section that isn't in your config file, the section and the key are added for you.
config.set("week.tuesday_index", 2)
>>> True
config.stringify()
>>> '[calendar]\ntoday = monday\nstart_week_on_sunday = false\ntoday_index = 20\nquarter_hours_passed = 0.25\n\n[week]\ntuesday_index = 2\n\n'
```
### Delete sections or key/value pairs.
```python
config.delete('week')
>>> True
config.stringify()
>>> '[calendar]\ntoday = monday\nstart_week_on_sunday = false\ntoday_index = 20\nquarter_hours_passed = 0.25\n\n'
config.delete('calendar.today')
>>> True
config.stringify()
>>> '[calendar]\nstart_week_on_sunday = false\ntoday_index = 20\nquarter_hours_passed = 0.25\n\n'
```
### Check whether you have a particular section or key
```python
config.has('calendar')
>>> True
config.has('week')
>>> False
config.has('calendar.start_week_on_sunday')
>>> True
```
### Save when you're done
The contents are only written back out when you call `save()`.
```python
config.save()
>>> True
```
### Reset the file back to its original
The current configuration file would be deleted and replaced by a copy of the original.
By default, since our passed in config file was at path `~/.config/test/config.ini`, `reset()`
will look for `~/.config/test/config.original.ini`.
```python
config.reset()
>>> True
# But you can also specify the original config file explicitly.
config.reset(original_file_path="~/some_other_directory/this_is_actually_the_original.ini")
>>> True
```
|