zpp_config 1.2.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- zpp_config-1.2.1/LICENSE +21 -0
- zpp_config-1.2.1/PKG-INFO +164 -0
- zpp_config-1.2.1/README.md +147 -0
- zpp_config-1.2.1/setup.cfg +4 -0
- zpp_config-1.2.1/zpp_config/__init__.py +5 -0
- zpp_config-1.2.1/zpp_config/config.py +486 -0
- zpp_config-1.2.1/zpp_config.egg-info/PKG-INFO +164 -0
- zpp_config-1.2.1/zpp_config.egg-info/SOURCES.txt +8 -0
- zpp_config-1.2.1/zpp_config.egg-info/dependency_links.txt +1 -0
- zpp_config-1.2.1/zpp_config.egg-info/top_level.txt +1 -0
zpp_config-1.2.1/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 ZephyrOff (Alexandre Pajak)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: zpp_config
|
|
3
|
+
Version: 1.2.1
|
|
4
|
+
Summary: Module pour le chargement et la modification de fichier de configuration
|
|
5
|
+
Home-page: https://github.com/ZephyrOff/py-zpp_config
|
|
6
|
+
Author:
|
|
7
|
+
License: MIT License
|
|
8
|
+
Project-URL: Documentation, https://github.com/ZephyrOff/py-zpp_config
|
|
9
|
+
Keywords: config file terminal zephyroff
|
|
10
|
+
Platform: ALL
|
|
11
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
12
|
+
Classifier: Environment :: Console
|
|
13
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Description-Content-Type: text/markdown
|
|
16
|
+
License-File: LICENSE
|
|
17
|
+
|
|
18
|
+
# zpp-config
|
|
19
|
+
## Informations
|
|
20
|
+
Librairie pour l'utilisation et la modification de fichier de configuration:<br>
|
|
21
|
+
- Charger un ou plusieurs paramètres
|
|
22
|
+
- Modifier un paramètre existant
|
|
23
|
+
- Ajout un paramètre ou une section
|
|
24
|
+
- Supprimer un paramètre ou une section
|
|
25
|
+
- Lister les sections disponibles
|
|
26
|
+
- Lister les paramètres et/ou sections désactivés
|
|
27
|
+
|
|
28
|
+
Prends en compte les paramètres commentés.<br>
|
|
29
|
+
Compatible avec les fichiers de configuration indentés.<br><br>
|
|
30
|
+
Traduit les paramètres pour les types str, int, float, bool, list, dict
|
|
31
|
+
|
|
32
|
+
### Prérequis
|
|
33
|
+
- Python 3
|
|
34
|
+
<br>
|
|
35
|
+
|
|
36
|
+
# Installation
|
|
37
|
+
```console
|
|
38
|
+
pip install zpp_config
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
# Utilisation
|
|
42
|
+
### Conseil d'importation du module
|
|
43
|
+
```python
|
|
44
|
+
from zpp_config import Config
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
<br>
|
|
48
|
+
|
|
49
|
+
### Exemple de fichier de config
|
|
50
|
+
```xml
|
|
51
|
+
[section]
|
|
52
|
+
value1 = key1
|
|
53
|
+
value2 = key2
|
|
54
|
+
value3 = key3
|
|
55
|
+
|
|
56
|
+
[section2]
|
|
57
|
+
value1 = key1
|
|
58
|
+
value2 = key2
|
|
59
|
+
value3 = key3
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
<br>
|
|
63
|
+
|
|
64
|
+
### Initialisaton d'un fichier de configuration
|
|
65
|
+
```python
|
|
66
|
+
c = Config("conf.ini")
|
|
67
|
+
```
|
|
68
|
+
>En paramètre supplémentaire, nous pouvons mettre:<br/>
|
|
69
|
+
>- separator: Définir le séparateur entre la clé et la valeur dans le fichier. (Par défaut: " = ")
|
|
70
|
+
>- escape_line: Définir le caractère utilisé pour commenter une valeur ou une section. (Par défaut: "#")
|
|
71
|
+
>- auto_create: Créer le fichier de configuration s'il n'existe pas. (Par défaut: "False")
|
|
72
|
+
>- read_only: Ouvrir le fichier de configuration en lecture seule. (Par défaut: "False")
|
|
73
|
+
|
|
74
|
+
<br>
|
|
75
|
+
|
|
76
|
+
### Chargement de paramètre
|
|
77
|
+
|
|
78
|
+
La fonction renvoie la valeur si un unique paramètre a été trouvé, sinon renvoie un dictionnaire avec les différentes valeurs trouvées (classé par section)
|
|
79
|
+
Renvoie un tableau vide si aucun paramètre n'a été trouvé
|
|
80
|
+
|
|
81
|
+
#### Chargement de tous les paramètres
|
|
82
|
+
```python
|
|
83
|
+
data = c.load()
|
|
84
|
+
```
|
|
85
|
+
#### Chargement d'une section du fichier
|
|
86
|
+
```python
|
|
87
|
+
data = c.load(section='section_name')
|
|
88
|
+
```
|
|
89
|
+
#### Chargement d'une valeur dans tout le fichier
|
|
90
|
+
```python
|
|
91
|
+
data = c.load(val='value_name')
|
|
92
|
+
```
|
|
93
|
+
#### Chargement d'une valeur dans une section spécifique
|
|
94
|
+
```python
|
|
95
|
+
data = c.load(val='value_name', section='section_name')
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
>En paramètre supplémentaire, nous pouvons mettre:<br/>
|
|
99
|
+
>- default: Pour initialiser une valeur par défaut si aucun résultat est trouvé
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
<br>
|
|
103
|
+
|
|
104
|
+
### Changement de paramètre
|
|
105
|
+
#### Changement d'une valeur dans tout le fichier
|
|
106
|
+
```python
|
|
107
|
+
c.change(val='value_name', key='key_value')
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
#### Changement d'une valeur dans une section spécifique
|
|
111
|
+
```python
|
|
112
|
+
c.change(val='value_name', key='key_value', section='section_name')
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
<br>
|
|
116
|
+
|
|
117
|
+
### Ajout de paramètre ou de section
|
|
118
|
+
|
|
119
|
+
Ajoute une section ou un paramètre dans le fichier de configuration.
|
|
120
|
+
Dans le cas de l'ajout d'un paramètre, rajoute la section si elle n'existe pas.
|
|
121
|
+
|
|
122
|
+
#### Ajout d'une section
|
|
123
|
+
```python
|
|
124
|
+
c.add(section='section_name')
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
#### Ajout d'un paramètre dans une section
|
|
128
|
+
```python
|
|
129
|
+
c.add(val='value_name', key='key_value', section='section_name')
|
|
130
|
+
```
|
|
131
|
+
> Si aucune section est défini, rajoute le paramètre en dehors des sections.
|
|
132
|
+
|
|
133
|
+
<br>
|
|
134
|
+
|
|
135
|
+
### Suppression de paramètre ou de section
|
|
136
|
+
|
|
137
|
+
#### Suppression d'une section
|
|
138
|
+
```python
|
|
139
|
+
c.delete(section='section_name')
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
#### Suppression d'un paramètre dans une section
|
|
143
|
+
```python
|
|
144
|
+
c.delete(val='value_name', section='section_name')
|
|
145
|
+
```
|
|
146
|
+
> Si aucune section est défini, recherche le paramètre en dehors des sections.
|
|
147
|
+
|
|
148
|
+
<br>
|
|
149
|
+
|
|
150
|
+
### Liste des paramètres non pris en compte
|
|
151
|
+
|
|
152
|
+
Retourne la liste des paramètres qui sont non pris en compte dans le fichier de configuration.
|
|
153
|
+
|
|
154
|
+
```python
|
|
155
|
+
data = c.disabled_line()
|
|
156
|
+
```
|
|
157
|
+
> Possibilité de préciser la section en utilisant le paramètre section
|
|
158
|
+
|
|
159
|
+
<br>
|
|
160
|
+
|
|
161
|
+
### Liste les sections disponibles
|
|
162
|
+
```python
|
|
163
|
+
data = c.list_section()
|
|
164
|
+
```
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
# zpp-config
|
|
2
|
+
## Informations
|
|
3
|
+
Librairie pour l'utilisation et la modification de fichier de configuration:<br>
|
|
4
|
+
- Charger un ou plusieurs paramètres
|
|
5
|
+
- Modifier un paramètre existant
|
|
6
|
+
- Ajout un paramètre ou une section
|
|
7
|
+
- Supprimer un paramètre ou une section
|
|
8
|
+
- Lister les sections disponibles
|
|
9
|
+
- Lister les paramètres et/ou sections désactivés
|
|
10
|
+
|
|
11
|
+
Prends en compte les paramètres commentés.<br>
|
|
12
|
+
Compatible avec les fichiers de configuration indentés.<br><br>
|
|
13
|
+
Traduit les paramètres pour les types str, int, float, bool, list, dict
|
|
14
|
+
|
|
15
|
+
### Prérequis
|
|
16
|
+
- Python 3
|
|
17
|
+
<br>
|
|
18
|
+
|
|
19
|
+
# Installation
|
|
20
|
+
```console
|
|
21
|
+
pip install zpp_config
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
# Utilisation
|
|
25
|
+
### Conseil d'importation du module
|
|
26
|
+
```python
|
|
27
|
+
from zpp_config import Config
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
<br>
|
|
31
|
+
|
|
32
|
+
### Exemple de fichier de config
|
|
33
|
+
```xml
|
|
34
|
+
[section]
|
|
35
|
+
value1 = key1
|
|
36
|
+
value2 = key2
|
|
37
|
+
value3 = key3
|
|
38
|
+
|
|
39
|
+
[section2]
|
|
40
|
+
value1 = key1
|
|
41
|
+
value2 = key2
|
|
42
|
+
value3 = key3
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
<br>
|
|
46
|
+
|
|
47
|
+
### Initialisaton d'un fichier de configuration
|
|
48
|
+
```python
|
|
49
|
+
c = Config("conf.ini")
|
|
50
|
+
```
|
|
51
|
+
>En paramètre supplémentaire, nous pouvons mettre:<br/>
|
|
52
|
+
>- separator: Définir le séparateur entre la clé et la valeur dans le fichier. (Par défaut: " = ")
|
|
53
|
+
>- escape_line: Définir le caractère utilisé pour commenter une valeur ou une section. (Par défaut: "#")
|
|
54
|
+
>- auto_create: Créer le fichier de configuration s'il n'existe pas. (Par défaut: "False")
|
|
55
|
+
>- read_only: Ouvrir le fichier de configuration en lecture seule. (Par défaut: "False")
|
|
56
|
+
|
|
57
|
+
<br>
|
|
58
|
+
|
|
59
|
+
### Chargement de paramètre
|
|
60
|
+
|
|
61
|
+
La fonction renvoie la valeur si un unique paramètre a été trouvé, sinon renvoie un dictionnaire avec les différentes valeurs trouvées (classé par section)
|
|
62
|
+
Renvoie un tableau vide si aucun paramètre n'a été trouvé
|
|
63
|
+
|
|
64
|
+
#### Chargement de tous les paramètres
|
|
65
|
+
```python
|
|
66
|
+
data = c.load()
|
|
67
|
+
```
|
|
68
|
+
#### Chargement d'une section du fichier
|
|
69
|
+
```python
|
|
70
|
+
data = c.load(section='section_name')
|
|
71
|
+
```
|
|
72
|
+
#### Chargement d'une valeur dans tout le fichier
|
|
73
|
+
```python
|
|
74
|
+
data = c.load(val='value_name')
|
|
75
|
+
```
|
|
76
|
+
#### Chargement d'une valeur dans une section spécifique
|
|
77
|
+
```python
|
|
78
|
+
data = c.load(val='value_name', section='section_name')
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
>En paramètre supplémentaire, nous pouvons mettre:<br/>
|
|
82
|
+
>- default: Pour initialiser une valeur par défaut si aucun résultat est trouvé
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
<br>
|
|
86
|
+
|
|
87
|
+
### Changement de paramètre
|
|
88
|
+
#### Changement d'une valeur dans tout le fichier
|
|
89
|
+
```python
|
|
90
|
+
c.change(val='value_name', key='key_value')
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
#### Changement d'une valeur dans une section spécifique
|
|
94
|
+
```python
|
|
95
|
+
c.change(val='value_name', key='key_value', section='section_name')
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
<br>
|
|
99
|
+
|
|
100
|
+
### Ajout de paramètre ou de section
|
|
101
|
+
|
|
102
|
+
Ajoute une section ou un paramètre dans le fichier de configuration.
|
|
103
|
+
Dans le cas de l'ajout d'un paramètre, rajoute la section si elle n'existe pas.
|
|
104
|
+
|
|
105
|
+
#### Ajout d'une section
|
|
106
|
+
```python
|
|
107
|
+
c.add(section='section_name')
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
#### Ajout d'un paramètre dans une section
|
|
111
|
+
```python
|
|
112
|
+
c.add(val='value_name', key='key_value', section='section_name')
|
|
113
|
+
```
|
|
114
|
+
> Si aucune section est défini, rajoute le paramètre en dehors des sections.
|
|
115
|
+
|
|
116
|
+
<br>
|
|
117
|
+
|
|
118
|
+
### Suppression de paramètre ou de section
|
|
119
|
+
|
|
120
|
+
#### Suppression d'une section
|
|
121
|
+
```python
|
|
122
|
+
c.delete(section='section_name')
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
#### Suppression d'un paramètre dans une section
|
|
126
|
+
```python
|
|
127
|
+
c.delete(val='value_name', section='section_name')
|
|
128
|
+
```
|
|
129
|
+
> Si aucune section est défini, recherche le paramètre en dehors des sections.
|
|
130
|
+
|
|
131
|
+
<br>
|
|
132
|
+
|
|
133
|
+
### Liste des paramètres non pris en compte
|
|
134
|
+
|
|
135
|
+
Retourne la liste des paramètres qui sont non pris en compte dans le fichier de configuration.
|
|
136
|
+
|
|
137
|
+
```python
|
|
138
|
+
data = c.disabled_line()
|
|
139
|
+
```
|
|
140
|
+
> Possibilité de préciser la section en utilisant le paramètre section
|
|
141
|
+
|
|
142
|
+
<br>
|
|
143
|
+
|
|
144
|
+
### Liste les sections disponibles
|
|
145
|
+
```python
|
|
146
|
+
data = c.list_section()
|
|
147
|
+
```
|
|
@@ -0,0 +1,486 @@
|
|
|
1
|
+
import re
|
|
2
|
+
import ast
|
|
3
|
+
from os.path import isfile, exists
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class Config():
|
|
7
|
+
def __init__(self,file,separator=" = ", escape_line="#", auto_create=False, read_only=False):
|
|
8
|
+
self.reg = r'\[[0-9A-Za-z-_ ]*\]'
|
|
9
|
+
|
|
10
|
+
self.separator=separator
|
|
11
|
+
self.escape_line = escape_line
|
|
12
|
+
|
|
13
|
+
self.read_only = read_only
|
|
14
|
+
self.created = False
|
|
15
|
+
|
|
16
|
+
if isfile(file) and exists(file):
|
|
17
|
+
self.file = file
|
|
18
|
+
else:
|
|
19
|
+
if auto_create==False:
|
|
20
|
+
print("Error: File not exists")
|
|
21
|
+
else:
|
|
22
|
+
self.file = file
|
|
23
|
+
with open(self.file, 'a') as f:
|
|
24
|
+
pass
|
|
25
|
+
self.created = True
|
|
26
|
+
|
|
27
|
+
#Str to real type
|
|
28
|
+
def return_data(self, data):
|
|
29
|
+
if data=="true" or data=="True":
|
|
30
|
+
return True
|
|
31
|
+
elif data=="false" or data=="False":
|
|
32
|
+
return False
|
|
33
|
+
else:
|
|
34
|
+
try:
|
|
35
|
+
return ast.literal_eval(data)
|
|
36
|
+
except:
|
|
37
|
+
return data
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def indent_line(self,line):
|
|
41
|
+
lineC = line.replace("\t","")
|
|
42
|
+
|
|
43
|
+
return lineC
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def list_section(self):
|
|
47
|
+
if hasattr(self,'file'):
|
|
48
|
+
with open(self.file) as f:
|
|
49
|
+
sec_array = []
|
|
50
|
+
for line in f.readlines():
|
|
51
|
+
line = self.indent_line(line.strip())
|
|
52
|
+
if re.match(self.reg,line):
|
|
53
|
+
sec_array.append(line.replace("[","").replace("]",""))
|
|
54
|
+
return sec_array
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def load(self,val=None,section=None,default=None):
|
|
58
|
+
lock=False
|
|
59
|
+
ban = False
|
|
60
|
+
data = []
|
|
61
|
+
sec = ''
|
|
62
|
+
dict_data = {}
|
|
63
|
+
|
|
64
|
+
if hasattr(self,'file'):
|
|
65
|
+
if section!=None and section!="" and section not in self.list_section():
|
|
66
|
+
return data
|
|
67
|
+
|
|
68
|
+
with open(self.file) as f:
|
|
69
|
+
for line in f.readlines():
|
|
70
|
+
line = self.indent_line(line.strip())
|
|
71
|
+
if ban==True and line.startswith("[") and line.endswith("]"):
|
|
72
|
+
ban=False
|
|
73
|
+
|
|
74
|
+
if line.startswith(self.escape_line+"[") and line.endswith("]"):
|
|
75
|
+
ban=True
|
|
76
|
+
elif line.startswith(self.escape_line):
|
|
77
|
+
pass
|
|
78
|
+
else:
|
|
79
|
+
if ban==False:
|
|
80
|
+
if section!=None:
|
|
81
|
+
if section!="" and re.match(self.reg,line):
|
|
82
|
+
if lock==False:
|
|
83
|
+
if line=="["+section+"]":
|
|
84
|
+
lock=True
|
|
85
|
+
data = []
|
|
86
|
+
else:
|
|
87
|
+
lock=False
|
|
88
|
+
break
|
|
89
|
+
|
|
90
|
+
else:
|
|
91
|
+
if lock==True:
|
|
92
|
+
if len(line)!=0:
|
|
93
|
+
if val==None:
|
|
94
|
+
data.append(line)
|
|
95
|
+
else:
|
|
96
|
+
line = line.split(self.separator)
|
|
97
|
+
if line[0]==val:
|
|
98
|
+
return self.return_data(line[1])
|
|
99
|
+
elif section=="":
|
|
100
|
+
if len(line)!=0:
|
|
101
|
+
if not line.startswith("[") and not line.endswith("]"):
|
|
102
|
+
if val==None:
|
|
103
|
+
data.append(line)
|
|
104
|
+
else:
|
|
105
|
+
line = line.split(self.separator)
|
|
106
|
+
if line[0]==val:
|
|
107
|
+
return self.return_data(line[1])
|
|
108
|
+
else:
|
|
109
|
+
break
|
|
110
|
+
|
|
111
|
+
elif val!=None:
|
|
112
|
+
if re.match(self.reg,line):
|
|
113
|
+
sec = line.replace("[","").replace("]","")
|
|
114
|
+
|
|
115
|
+
if len(line)!=0:
|
|
116
|
+
line = line.split(self.separator)
|
|
117
|
+
if line[0]==val:
|
|
118
|
+
data.append(sec + self.separator + line[1])
|
|
119
|
+
|
|
120
|
+
else:
|
|
121
|
+
if len(line)!=0:
|
|
122
|
+
if not re.match(self.reg,line):
|
|
123
|
+
line = line.split(self.separator)
|
|
124
|
+
if sec=='' and sec not in dict_data:
|
|
125
|
+
dict_data[sec] = {}
|
|
126
|
+
dict_data[sec][line[0]] = self.return_data(line[1])
|
|
127
|
+
else:
|
|
128
|
+
sec = line.replace("[","").replace("]","")
|
|
129
|
+
if sec not in dict_data.keys():
|
|
130
|
+
dict_data[sec] = {}
|
|
131
|
+
|
|
132
|
+
if val==None and section==None:
|
|
133
|
+
return dict_data
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
if len(data)!=0:
|
|
137
|
+
for element in data:
|
|
138
|
+
element=element.split(self.separator)
|
|
139
|
+
if len(element)>1:
|
|
140
|
+
dict_data[element[0]] = self.return_data(element[1])
|
|
141
|
+
|
|
142
|
+
if dict_data=={} and default!=None:
|
|
143
|
+
return default
|
|
144
|
+
else:
|
|
145
|
+
return dict_data
|
|
146
|
+
|
|
147
|
+
if data==[] and default!=None:
|
|
148
|
+
return default
|
|
149
|
+
else:
|
|
150
|
+
return data
|
|
151
|
+
|
|
152
|
+
if default!=None:
|
|
153
|
+
return default
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
def edit(self,line,val,key,temp):
|
|
157
|
+
if len(line)!=0:
|
|
158
|
+
if val!=None:
|
|
159
|
+
lineT = line.split(self.separator)
|
|
160
|
+
if lineT[0]==val:
|
|
161
|
+
self.new_content.append(temp.replace(self.separator+lineT[1], self.separator+str(key)))
|
|
162
|
+
return True
|
|
163
|
+
else:
|
|
164
|
+
self.new_content.append(temp)
|
|
165
|
+
return True
|
|
166
|
+
return False
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
def change(self,val=None,key=None,section=None):
|
|
170
|
+
if self.read_only==False:
|
|
171
|
+
if val!=None and key!=None:
|
|
172
|
+
if isinstance(key,str) or isinstance(key,list) or isinstance(key,int) or isinstance(key,float) or isinstance(key,bool) or isinstance(key,dict):
|
|
173
|
+
if hasattr(self,'file'):
|
|
174
|
+
with open(self.file) as f:
|
|
175
|
+
self.new_content = []
|
|
176
|
+
lock=False
|
|
177
|
+
|
|
178
|
+
for line in f.readlines():
|
|
179
|
+
temp = line.rstrip()
|
|
180
|
+
line = self.indent_line(line.strip())
|
|
181
|
+
if len(line)!=0:
|
|
182
|
+
if section!=None:
|
|
183
|
+
if section!="" and re.match(self.reg,line):
|
|
184
|
+
if lock==False:
|
|
185
|
+
if line=="["+section+"]":
|
|
186
|
+
lock=True
|
|
187
|
+
else:
|
|
188
|
+
lock=False
|
|
189
|
+
|
|
190
|
+
else:
|
|
191
|
+
if lock==True:
|
|
192
|
+
if self.edit(line,val,key,temp):
|
|
193
|
+
continue
|
|
194
|
+
else:
|
|
195
|
+
if self.edit(line,val,key,temp):
|
|
196
|
+
continue
|
|
197
|
+
|
|
198
|
+
self.new_content.append(temp)
|
|
199
|
+
else:
|
|
200
|
+
self.new_content.append(temp)
|
|
201
|
+
|
|
202
|
+
with open(self.file,"w") as f:
|
|
203
|
+
f.write("\n".join(self.new_content))
|
|
204
|
+
else:
|
|
205
|
+
print("Read-only mode")
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
def add(self,val=None,key=None,section=None):
|
|
209
|
+
if self.read_only==False:
|
|
210
|
+
exist = True
|
|
211
|
+
new_content=None
|
|
212
|
+
lock=False
|
|
213
|
+
|
|
214
|
+
if key=="":
|
|
215
|
+
key = '""'
|
|
216
|
+
|
|
217
|
+
if hasattr(self,'file'):
|
|
218
|
+
if section!=None and section!="" and section not in self.list_section():
|
|
219
|
+
exist = False
|
|
220
|
+
|
|
221
|
+
if val!=None and val!="" and key!=None and not (isinstance(key,str) or isinstance(key,list) or isinstance(key,int) or isinstance(key,float) or isinstance(key,bool) or isinstance(key,dict)):
|
|
222
|
+
print("Key not supported")
|
|
223
|
+
return
|
|
224
|
+
|
|
225
|
+
with open(self.file) as f:
|
|
226
|
+
if exist==False:
|
|
227
|
+
new_content=f.read()
|
|
228
|
+
if new_content[len(new_content)-1]=="\n":
|
|
229
|
+
new_content+="\n["+section+"]"
|
|
230
|
+
else:
|
|
231
|
+
new_content+="\n\n["+section+"]"
|
|
232
|
+
if val!=None and val!="" and key!=None:
|
|
233
|
+
new_content+="\n"+val+self.separator+str(key)
|
|
234
|
+
|
|
235
|
+
else:
|
|
236
|
+
data = self.load()
|
|
237
|
+
if not len(data):
|
|
238
|
+
data = {"":{}}
|
|
239
|
+
if section!=None:
|
|
240
|
+
se_test = section
|
|
241
|
+
else:
|
|
242
|
+
se_test = ""
|
|
243
|
+
|
|
244
|
+
if val!=None:
|
|
245
|
+
se = data[se_test]
|
|
246
|
+
if val in se.keys():
|
|
247
|
+
print("Key already exist")
|
|
248
|
+
return
|
|
249
|
+
|
|
250
|
+
if val!=None and val!="" and key!=None:
|
|
251
|
+
if section=="" or section==None:
|
|
252
|
+
new_content=val+self.separator+str(key)+"\n"
|
|
253
|
+
new_content+=f.read()
|
|
254
|
+
else:
|
|
255
|
+
new_content = []
|
|
256
|
+
for line in f.readlines():
|
|
257
|
+
line_temp = line.rstrip()
|
|
258
|
+
line = self.indent_line(line.strip())
|
|
259
|
+
if line_temp!="":
|
|
260
|
+
if section!=None:
|
|
261
|
+
if section!="" and re.match(self.reg,line):
|
|
262
|
+
if lock==False:
|
|
263
|
+
if line=="["+section+"]":
|
|
264
|
+
if len(se.keys())==0:
|
|
265
|
+
line_temp+="\n"+val+self.separator+str(key)
|
|
266
|
+
lock=True
|
|
267
|
+
else:
|
|
268
|
+
lock=False
|
|
269
|
+
|
|
270
|
+
else:
|
|
271
|
+
if lock==True:
|
|
272
|
+
if "\t" in line_temp:
|
|
273
|
+
line_temp+="\n"+("\t"*line_temp.count('\t'))+val+self.separator+str(key)
|
|
274
|
+
lock=False
|
|
275
|
+
|
|
276
|
+
new_content.append(line_temp)
|
|
277
|
+
|
|
278
|
+
new_content = "\n".join(new_content)
|
|
279
|
+
|
|
280
|
+
if new_content!=None:
|
|
281
|
+
with open(self.file,'w') as f:
|
|
282
|
+
f.write(new_content)
|
|
283
|
+
else:
|
|
284
|
+
print("Read-only mode")
|
|
285
|
+
|
|
286
|
+
|
|
287
|
+
def delete(self,val=None,section=None):
|
|
288
|
+
if self.read_only==False:
|
|
289
|
+
if hasattr(self,'file'):
|
|
290
|
+
with open(self.file) as f:
|
|
291
|
+
lock=False
|
|
292
|
+
new_content=""
|
|
293
|
+
sec = ""
|
|
294
|
+
ban=False
|
|
295
|
+
|
|
296
|
+
for line in f.readlines():
|
|
297
|
+
lineT = line
|
|
298
|
+
line = self.indent_line(line.strip())
|
|
299
|
+
|
|
300
|
+
if line.startswith("[") and line.endswith("]"):
|
|
301
|
+
ban=False
|
|
302
|
+
sec = line.replace("[","").replace("]","")
|
|
303
|
+
|
|
304
|
+
elif line.startswith(self.escape_line+"[") and line.endswith("]"):
|
|
305
|
+
ban=True
|
|
306
|
+
sec = line.replace(self.escape_line+"[","").replace("]","")
|
|
307
|
+
|
|
308
|
+
if ban!=True:
|
|
309
|
+
if section!=None and section!="":
|
|
310
|
+
if section!="" and re.match(self.reg,line):
|
|
311
|
+
if lock==False:
|
|
312
|
+
if line=="["+section+"]":
|
|
313
|
+
lock=True
|
|
314
|
+
if val!=None:
|
|
315
|
+
new_content+=lineT
|
|
316
|
+
else:
|
|
317
|
+
new_content+=lineT
|
|
318
|
+
else:
|
|
319
|
+
new_content+=lineT
|
|
320
|
+
lock=False
|
|
321
|
+
else:
|
|
322
|
+
if lock==True:
|
|
323
|
+
if val==None:
|
|
324
|
+
pass
|
|
325
|
+
else:
|
|
326
|
+
line = line.split(self.separator)
|
|
327
|
+
if line[0]==val:
|
|
328
|
+
pass
|
|
329
|
+
else:
|
|
330
|
+
new_content+=lineT
|
|
331
|
+
else:
|
|
332
|
+
new_content+=lineT
|
|
333
|
+
else:
|
|
334
|
+
if val!=None and sec=="":
|
|
335
|
+
line = line.split(self.separator)
|
|
336
|
+
if line[0]==val:
|
|
337
|
+
pass
|
|
338
|
+
else:
|
|
339
|
+
new_content+=lineT
|
|
340
|
+
else:
|
|
341
|
+
new_content+=lineT
|
|
342
|
+
else:
|
|
343
|
+
new_content+=lineT
|
|
344
|
+
|
|
345
|
+
if new_content!="":
|
|
346
|
+
with open(self.file,'w') as f:
|
|
347
|
+
f.write(new_content)
|
|
348
|
+
else:
|
|
349
|
+
print("Read-only mode")
|
|
350
|
+
|
|
351
|
+
|
|
352
|
+
def disabled_line(self, section=None):
|
|
353
|
+
dict_data = {}
|
|
354
|
+
|
|
355
|
+
if hasattr(self,'file'):
|
|
356
|
+
with open(self.file) as f:
|
|
357
|
+
ban=False
|
|
358
|
+
sec = ""
|
|
359
|
+
|
|
360
|
+
for line in f.readlines():
|
|
361
|
+
line = self.indent_line(line.strip())
|
|
362
|
+
if len(line)!=0:
|
|
363
|
+
if line.startswith("[") and line.endswith("]"):
|
|
364
|
+
sec = line.replace("[","").replace("]","")
|
|
365
|
+
if ban==True:
|
|
366
|
+
ban=False
|
|
367
|
+
|
|
368
|
+
if line.startswith(self.escape_line+"[") and line.endswith("]"):
|
|
369
|
+
ban=True
|
|
370
|
+
sec = line.replace(self.escape_line+"[","").replace("]","")
|
|
371
|
+
|
|
372
|
+
elif line.startswith(self.escape_line):
|
|
373
|
+
line = line[1:len(line)].split(self.separator)
|
|
374
|
+
if sec not in dict_data.keys():
|
|
375
|
+
dict_data[sec] = {}
|
|
376
|
+
dict_data[sec][line[0]] = self.return_data(line[1])
|
|
377
|
+
|
|
378
|
+
else:
|
|
379
|
+
if ban==True:
|
|
380
|
+
line = line.split(self.separator)
|
|
381
|
+
if sec not in dict_data.keys():
|
|
382
|
+
dict_data[sec] = {}
|
|
383
|
+
dict_data[sec][line[0]] = self.return_data(line[1])
|
|
384
|
+
return dict_data
|
|
385
|
+
|
|
386
|
+
def disable(self,val=None,section=None):
|
|
387
|
+
if self.read_only==False:
|
|
388
|
+
if val!=None:
|
|
389
|
+
if hasattr(self,'file'):
|
|
390
|
+
modified = False
|
|
391
|
+
with open(self.file) as f:
|
|
392
|
+
self.new_content = []
|
|
393
|
+
lock=False
|
|
394
|
+
|
|
395
|
+
for line in f.readlines():
|
|
396
|
+
temp = line.rstrip()
|
|
397
|
+
line = self.indent_line(line.strip())
|
|
398
|
+
if len(line)!=0:
|
|
399
|
+
if section!=None:
|
|
400
|
+
if section!="" and re.match(self.reg,line):
|
|
401
|
+
if lock==False:
|
|
402
|
+
if line=="["+section+"]":
|
|
403
|
+
lock=True
|
|
404
|
+
else:
|
|
405
|
+
lock=False
|
|
406
|
+
else:
|
|
407
|
+
if lock==True:
|
|
408
|
+
lineT = line.split(self.separator)
|
|
409
|
+
if lineT[0]==val:
|
|
410
|
+
modified = True
|
|
411
|
+
self.new_content.append("#"+line)
|
|
412
|
+
else:
|
|
413
|
+
self.new_content.append(line)
|
|
414
|
+
continue
|
|
415
|
+
else:
|
|
416
|
+
lineT = line.split(self.separator)
|
|
417
|
+
if lineT[0]==val:
|
|
418
|
+
modified = True
|
|
419
|
+
self.new_content.append("#"+line)
|
|
420
|
+
else:
|
|
421
|
+
self.new_content.append(line)
|
|
422
|
+
continue
|
|
423
|
+
self.new_content.append(temp)
|
|
424
|
+
else:
|
|
425
|
+
self.new_content.append(temp)
|
|
426
|
+
|
|
427
|
+
if modified:
|
|
428
|
+
with open(self.file,"w") as f:
|
|
429
|
+
f.write("\n".join(self.new_content))
|
|
430
|
+
return True
|
|
431
|
+
return False
|
|
432
|
+
else:
|
|
433
|
+
print("Read-only mode")
|
|
434
|
+
return False
|
|
435
|
+
|
|
436
|
+
|
|
437
|
+
def enable(self,val=None,section=None):
|
|
438
|
+
if self.read_only==False:
|
|
439
|
+
if val!=None:
|
|
440
|
+
val = "#"+val
|
|
441
|
+
if hasattr(self,'file'):
|
|
442
|
+
modified = False
|
|
443
|
+
with open(self.file) as f:
|
|
444
|
+
self.new_content = []
|
|
445
|
+
lock=False
|
|
446
|
+
|
|
447
|
+
for line in f.readlines():
|
|
448
|
+
temp = line.rstrip()
|
|
449
|
+
line = self.indent_line(line.strip())
|
|
450
|
+
if len(line)!=0:
|
|
451
|
+
if section!=None:
|
|
452
|
+
if section!="" and re.match(self.reg,line):
|
|
453
|
+
if lock==False:
|
|
454
|
+
if line=="["+section+"]":
|
|
455
|
+
lock=True
|
|
456
|
+
else:
|
|
457
|
+
lock=False
|
|
458
|
+
else:
|
|
459
|
+
if lock==True:
|
|
460
|
+
lineT = line.split(self.separator)
|
|
461
|
+
if lineT[0]==val:
|
|
462
|
+
modified = True
|
|
463
|
+
self.new_content.append(line[1:])
|
|
464
|
+
else:
|
|
465
|
+
self.new_content.append(line)
|
|
466
|
+
continue
|
|
467
|
+
else:
|
|
468
|
+
lineT = line.split(self.separator)
|
|
469
|
+
if lineT[0]==val:
|
|
470
|
+
modified = True
|
|
471
|
+
self.new_content.append(line[1:])
|
|
472
|
+
else:
|
|
473
|
+
self.new_content.append(line)
|
|
474
|
+
continue
|
|
475
|
+
self.new_content.append(temp)
|
|
476
|
+
else:
|
|
477
|
+
self.new_content.append(temp)
|
|
478
|
+
|
|
479
|
+
if modified:
|
|
480
|
+
with open(self.file,"w") as f:
|
|
481
|
+
f.write("\n".join(self.new_content))
|
|
482
|
+
return True
|
|
483
|
+
return False
|
|
484
|
+
else:
|
|
485
|
+
print("Read-only mode")
|
|
486
|
+
return False
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: zpp-config
|
|
3
|
+
Version: 1.2.1
|
|
4
|
+
Summary: Module pour le chargement et la modification de fichier de configuration
|
|
5
|
+
Home-page: https://github.com/ZephyrOff/py-zpp_config
|
|
6
|
+
Author:
|
|
7
|
+
License: MIT License
|
|
8
|
+
Project-URL: Documentation, https://github.com/ZephyrOff/py-zpp_config
|
|
9
|
+
Keywords: config file terminal zephyroff
|
|
10
|
+
Platform: ALL
|
|
11
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
12
|
+
Classifier: Environment :: Console
|
|
13
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Description-Content-Type: text/markdown
|
|
16
|
+
License-File: LICENSE
|
|
17
|
+
|
|
18
|
+
# zpp-config
|
|
19
|
+
## Informations
|
|
20
|
+
Librairie pour l'utilisation et la modification de fichier de configuration:<br>
|
|
21
|
+
- Charger un ou plusieurs paramètres
|
|
22
|
+
- Modifier un paramètre existant
|
|
23
|
+
- Ajout un paramètre ou une section
|
|
24
|
+
- Supprimer un paramètre ou une section
|
|
25
|
+
- Lister les sections disponibles
|
|
26
|
+
- Lister les paramètres et/ou sections désactivés
|
|
27
|
+
|
|
28
|
+
Prends en compte les paramètres commentés.<br>
|
|
29
|
+
Compatible avec les fichiers de configuration indentés.<br><br>
|
|
30
|
+
Traduit les paramètres pour les types str, int, float, bool, list, dict
|
|
31
|
+
|
|
32
|
+
### Prérequis
|
|
33
|
+
- Python 3
|
|
34
|
+
<br>
|
|
35
|
+
|
|
36
|
+
# Installation
|
|
37
|
+
```console
|
|
38
|
+
pip install zpp_config
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
# Utilisation
|
|
42
|
+
### Conseil d'importation du module
|
|
43
|
+
```python
|
|
44
|
+
from zpp_config import Config
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
<br>
|
|
48
|
+
|
|
49
|
+
### Exemple de fichier de config
|
|
50
|
+
```xml
|
|
51
|
+
[section]
|
|
52
|
+
value1 = key1
|
|
53
|
+
value2 = key2
|
|
54
|
+
value3 = key3
|
|
55
|
+
|
|
56
|
+
[section2]
|
|
57
|
+
value1 = key1
|
|
58
|
+
value2 = key2
|
|
59
|
+
value3 = key3
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
<br>
|
|
63
|
+
|
|
64
|
+
### Initialisaton d'un fichier de configuration
|
|
65
|
+
```python
|
|
66
|
+
c = Config("conf.ini")
|
|
67
|
+
```
|
|
68
|
+
>En paramètre supplémentaire, nous pouvons mettre:<br/>
|
|
69
|
+
>- separator: Définir le séparateur entre la clé et la valeur dans le fichier. (Par défaut: " = ")
|
|
70
|
+
>- escape_line: Définir le caractère utilisé pour commenter une valeur ou une section. (Par défaut: "#")
|
|
71
|
+
>- auto_create: Créer le fichier de configuration s'il n'existe pas. (Par défaut: "False")
|
|
72
|
+
>- read_only: Ouvrir le fichier de configuration en lecture seule. (Par défaut: "False")
|
|
73
|
+
|
|
74
|
+
<br>
|
|
75
|
+
|
|
76
|
+
### Chargement de paramètre
|
|
77
|
+
|
|
78
|
+
La fonction renvoie la valeur si un unique paramètre a été trouvé, sinon renvoie un dictionnaire avec les différentes valeurs trouvées (classé par section)
|
|
79
|
+
Renvoie un tableau vide si aucun paramètre n'a été trouvé
|
|
80
|
+
|
|
81
|
+
#### Chargement de tous les paramètres
|
|
82
|
+
```python
|
|
83
|
+
data = c.load()
|
|
84
|
+
```
|
|
85
|
+
#### Chargement d'une section du fichier
|
|
86
|
+
```python
|
|
87
|
+
data = c.load(section='section_name')
|
|
88
|
+
```
|
|
89
|
+
#### Chargement d'une valeur dans tout le fichier
|
|
90
|
+
```python
|
|
91
|
+
data = c.load(val='value_name')
|
|
92
|
+
```
|
|
93
|
+
#### Chargement d'une valeur dans une section spécifique
|
|
94
|
+
```python
|
|
95
|
+
data = c.load(val='value_name', section='section_name')
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
>En paramètre supplémentaire, nous pouvons mettre:<br/>
|
|
99
|
+
>- default: Pour initialiser une valeur par défaut si aucun résultat est trouvé
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
<br>
|
|
103
|
+
|
|
104
|
+
### Changement de paramètre
|
|
105
|
+
#### Changement d'une valeur dans tout le fichier
|
|
106
|
+
```python
|
|
107
|
+
c.change(val='value_name', key='key_value')
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
#### Changement d'une valeur dans une section spécifique
|
|
111
|
+
```python
|
|
112
|
+
c.change(val='value_name', key='key_value', section='section_name')
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
<br>
|
|
116
|
+
|
|
117
|
+
### Ajout de paramètre ou de section
|
|
118
|
+
|
|
119
|
+
Ajoute une section ou un paramètre dans le fichier de configuration.
|
|
120
|
+
Dans le cas de l'ajout d'un paramètre, rajoute la section si elle n'existe pas.
|
|
121
|
+
|
|
122
|
+
#### Ajout d'une section
|
|
123
|
+
```python
|
|
124
|
+
c.add(section='section_name')
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
#### Ajout d'un paramètre dans une section
|
|
128
|
+
```python
|
|
129
|
+
c.add(val='value_name', key='key_value', section='section_name')
|
|
130
|
+
```
|
|
131
|
+
> Si aucune section est défini, rajoute le paramètre en dehors des sections.
|
|
132
|
+
|
|
133
|
+
<br>
|
|
134
|
+
|
|
135
|
+
### Suppression de paramètre ou de section
|
|
136
|
+
|
|
137
|
+
#### Suppression d'une section
|
|
138
|
+
```python
|
|
139
|
+
c.delete(section='section_name')
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
#### Suppression d'un paramètre dans une section
|
|
143
|
+
```python
|
|
144
|
+
c.delete(val='value_name', section='section_name')
|
|
145
|
+
```
|
|
146
|
+
> Si aucune section est défini, recherche le paramètre en dehors des sections.
|
|
147
|
+
|
|
148
|
+
<br>
|
|
149
|
+
|
|
150
|
+
### Liste des paramètres non pris en compte
|
|
151
|
+
|
|
152
|
+
Retourne la liste des paramètres qui sont non pris en compte dans le fichier de configuration.
|
|
153
|
+
|
|
154
|
+
```python
|
|
155
|
+
data = c.disabled_line()
|
|
156
|
+
```
|
|
157
|
+
> Possibilité de préciser la section en utilisant le paramètre section
|
|
158
|
+
|
|
159
|
+
<br>
|
|
160
|
+
|
|
161
|
+
### Liste les sections disponibles
|
|
162
|
+
```python
|
|
163
|
+
data = c.list_section()
|
|
164
|
+
```
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
zpp_config
|