Python - Clase para editar un archivo de configuración

 
Vista:
sin imagen de perfil
Val: 54
Ha disminuido su posición en 2 puestos en Python (en relación al último mes)
Gráfica de Python

Clase para editar un archivo de configuración

Publicado por CARLOS (19 intervenciones) el 30/07/2019 13:45:58
Hola foreros:

En mi proyecto, utilizo un archivo de configuración (file.conf) con el siguiente formato:
1
2
3
4
5
[DEVICE]
ip_address = 10.8.20.1
mode = rfid
privacity = 0
dev_type = 20

Intento utilizar la clase siguiente en un ecript para poder modificar los parámetros pero no consigo hacerlo.

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
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
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
254
255
256
257
258
259
260
261
262
263
264
265
#!/usr/bin/env python
# -*- coding: utf-8 -*-
 
"""
Class for read and modify files with key value content.

Last versión:
http://lwp-l.com/s5436

Samples:

    Read key for file with separation is "="
        sample fileConfig content:
            # comment
            key1=value1
        code:
            from keyValueFile import KeyValueFile
            kv=KeyValueFile("fileConfig", "=")
            kv.getValue("key1") # return value1
 
    Read key commented for file with separation is "=" and comment line started with "#"
        sample fileConfig content:
            # comment
            key1=value1
            #key2=value2
        code:
            from keyValueFile import KeyValueFile
            kv=KeyValueFile("fileConfig", "=")
            kv.getValue("key2", True) # return value2
 
    Read key commented for file with separation is "=" and comment line started with ";"
        sample fileConfig content:
            ; comment
            key1=value1
            ;key2=value2
        code:
            from keyValueFile import KeyValueFile
            kv=KeyValueFile("fileConfig", "=", ";")
            kv.getValue("key2", True) # return value2
 
    Read key for file with separation is one space " "
        sample fileConfig content:
            # comment
            key2 value2
        code:
            from keyValueFile import KeyValueFile
            kv=KeyValueFile("fileConfig", " ")
            kv.getValue("key2") # return value2
 
    Update value for file with separation is "="
        sample fileConfig content:
            # comment
            key1=value1
        code:
            from keyValueFile import KeyValueFile
            kv=KeyValueFile("fileConfig", "=")
            kv.setValue("key1", "new value")
            kv.save()
 
    Get commented value for a file with separation is "=" and comment line started with ";"
        sample fileConfig content:
            ; comment
            key1=value1
            ;key2=value2
        code:
            from keyValueFile import KeyValueFile
            kv=KeyValueFile("fileConfig", "=", ";")
            kv.getValue("key2") # return None
            kv.getValue("key2", True) # return value2
 
    Set commented for a key value pairs
        sample fileConfig content:
            ; comment
            key1=value1
            key2=value2
        code:
            from keyValueFile import KeyValueFile
            kv=KeyValueFile("fileConfig", "=", ";")
            kv.setValue("key2", kv.getValue("key2"), True)
            kv.save()
 
    Remove commented for a key value pairs
        sample fileConfig content:
            ; comment
            key1=value1
            ;key2=value2
        code:
            from keyValueFile import KeyValueFile
            kv=KeyValueFile("fileConfig", "=", ";")
            kv.setValue("key2", kv.getValue("key2", True))
            kv.save()
 
    Remove key value pairs from file
        sample fileConfig content:
            # comment
            key1=value1
            key2=value2
        code:
            from keyValueFile import KeyValueFile
            kv=KeyValueFile("fileConfig")
            kv.removeKey("key2") # remove the line key2=value2
            kv.save()
 
"""

import os
from tempfile import mkstemp
from shutil import move

__ver__="0.1"

class KeyValueFile():
    file=""
    separationKeyValue=""
    commentCharacterStart=""
    keyValues={}
    keyRemove=[]
    
    def __init__(self, file="", separationKeyValue="=", commentCharacterStart="#"):
        """
        Parameters:
            file string - file to read and save
            separation string - separation between key and value
        """
        self.setFile(file)
        self.separationKeyValue=separationKeyValue
        self.commentCharacterStart=commentCharacterStart
 
    def setFile(self, file, separationKeyValue="="):
        """
        Function that receive a filename to update.
        
        Parameters
            string file
            string separationKeyValue (default =)
        Return
            boolean
        """
        if os.path.exists(file) and separationKeyValue:
            self.file=file
            self.separationKeyValue=separationKeyValue
            return True
        return False
 
    def setValue(self, key, value="", comment=False):
        """
        Function to save key value in a var for update or insert when save data.

        Parameters:
            string key
            string value (optional)
            boolean comment - define if the value has to be commented
        Return:
            boolean
        """
        if not key:
            return False
 
        if key in self.keyRemove:
            self.keyRemove.remove(key)
 
        self.keyValues[key.strip()]=[value.strip(), comment]
        return True
 
    def removeKey(self, key):
        """
        Function to save key value for remove this line with key when save data.

        Parameters:
            string key
        """
        if not key:
            return False
 
        if key in self.keyValues:
            del self.keyValues[key]
 
        self.keyRemove.append(key)
        return True
 
    def getValue(self, key, commented=False):
        """
        Function that find a key into file and return a value if finded key.

        Parameters:
            key string 
            commented boolean - define if search key in commented lines
        Return:
            string - value for a key or None
        """
        with open(self.file, "r") as f:
            for line in f:
                kv=line.split(self.separationKeyValue, 1)
 
                # the line doesn't have a separation key value
                if len(kv)==1:
                    continue
 
                k,v=kv
                k=k.strip()
                v=v.strip()
 
                k=k[1:].strip() if commented and k[0]==self.commentCharacterStart else k
 
                if k==key:
                    return v
        return None
 
    def save(self):
        """
        Function to save the content of vars in a file.

        Return:
            int - number of lines + added or updated - removed
        """
        count=0
        if os.path.exists(self.file) and os.access(self.file, os.W_OK) and self.separationKeyValue:
            fh, abs_path = mkstemp()
            with os.fdopen(fh, 'w') as new_file:
                with open(self.file, "r") as old_file:
                    for line in old_file:
                        kv=line.split(self.separationKeyValue, 1)
 
                        if len(kv)==1:
                            # the line doesn't have a separation key value
                            new_file.write(line)
                            continue
 
                        k,v=kv
                        k=k.strip()
                        v=v.strip()
 
                        k=k[1:] if k[0]==self.commentCharacterStart else k
 
                        if k and k in self.keyRemove:
                            # this key if market as remove
                            count-=1
                            self.keyRemove.remove(k)
                            continue
 
                        if k and k in self.keyValues:
                            # this key will update from file
                            count+=1
                            if self.keyValues[k][1]==True:
                                # write as commented
                                new_file.write(self.commentCharacterStart+k+self.separationKeyValue+self.keyValues[k][0]+"\n")
                            else:
                                new_file.write(k+self.separationKeyValue+self.keyValues[k][0]+"\n")
                            del self.keyValues[k]
                        else:
                            new_file.write(line)
 
                    # Add elements not updated
                    for k,v in self.keyValues.items():
                        count+=1
                        new_file.write(k+self.separationKeyValue+v[0]+"\n")
 
                    self.keyValues={}
                    self.keyRemove=[]
 
            # Remove original file
            os.remove(self.file)
            # Move new file
            move(abs_path, self.file)
        return count

Me podeis echar una mano...?
Gracia de antemano.
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de Xavi
Val: 666
Bronce
Ha disminuido 1 puesto en Python (en relación al último mes)
Gráfica de Python

Clase para editar un archivo de configuración

Publicado por Xavi (47 intervenciones) el 31/07/2019 08:51:41
Hola Carlos, por ejemplo para cambiar la IP seria algo así (suponiendo que la clase se encuentre en la misma ruta y el archivo se llame KeyValueFile.py):

1
2
3
4
5
from KeyValueFile import KeyValueFile
 
kv=KeyValueFile("file.conf", " = ")
kv.setValue("ip_address", "192.168.1.1")
kv.save()

Cualquier duda...
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
1
Comentar
sin imagen de perfil
Val: 54
Ha disminuido su posición en 2 puestos en Python (en relación al último mes)
Gráfica de Python

Clase para editar un archivo de configuración

Publicado por CARLOS (19 intervenciones) el 31/07/2019 09:26:01
Genial Xavi. Funciona perfecto...
Muchas gracias
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar