17
17
18
18
__all__ = ('GitConfigParser' , 'SectionConstraint' )
19
19
20
+
20
21
class MetaParserBuilder (type ):
22
+
21
23
"""Utlity class wrapping base-class methods into decorators that assure read-only properties"""
22
24
def __new__ (metacls , name , bases , clsdict ):
23
25
"""
@@ -45,20 +47,22 @@ def __new__(metacls, name, bases, clsdict):
45
47
return new_type
46
48
47
49
48
-
49
50
def needs_values (func ):
50
51
"""Returns method assuring we read values (on demand) before we try to access them"""
52
+
51
53
def assure_data_present (self , * args , ** kwargs ):
52
54
self .read ()
53
55
return func (self , * args , ** kwargs )
54
56
# END wrapper method
55
57
assure_data_present .__name__ = func .__name__
56
58
return assure_data_present
57
59
60
+
58
61
def set_dirty_and_flush_changes (non_const_func ):
59
62
"""Return method that checks whether given non constant function may be called.
60
63
If so, the instance will be set dirty.
61
64
Additionally, we flush the changes right to disk"""
65
+
62
66
def flush_changes (self , * args , ** kwargs ):
63
67
rval = non_const_func (self , * args , ** kwargs )
64
68
self .write ()
@@ -69,6 +73,7 @@ def flush_changes(self, *args, **kwargs):
69
73
70
74
71
75
class SectionConstraint (object ):
76
+
72
77
"""Constrains a ConfigParser to only option commands which are constrained to
73
78
always use the section we have been initialized with.
74
79
@@ -98,6 +103,7 @@ def config(self):
98
103
99
104
100
105
class GitConfigParser (cp .RawConfigParser , object ):
106
+
101
107
"""Implements specifics required to read git style configuration files.
102
108
103
109
This variation behaves much like the git.config command such that the configuration
@@ -114,7 +120,6 @@ class GitConfigParser(cp.RawConfigParser, object):
114
120
must match perfectly."""
115
121
__metaclass__ = MetaParserBuilder
116
122
117
-
118
123
#{ Configuration
119
124
# The lock type determines the type of lock to use in new configuration readers.
120
125
# They must be compatible to the LockFile interface.
@@ -172,7 +177,6 @@ def __init__(self, file_or_files, read_only=True):
172
177
self ._lock ._obtain_lock ()
173
178
# END read-only check
174
179
175
-
176
180
def __del__ (self ):
177
181
"""Write pending changes if required and release locks"""
178
182
# checking for the lock here makes sure we do not raise during write()
@@ -261,7 +265,6 @@ def _read(self, fp, fpname):
261
265
if e :
262
266
raise e
263
267
264
-
265
268
def read (self ):
266
269
"""Reads the data stored in the files we have been initialized with. It will
267
270
ignore files that cannot be read, possibly leaving an empty configuration
@@ -311,7 +314,6 @@ def write_section(name, section_dict):
311
314
write_section (cp .DEFAULTSECT , self ._defaults )
312
315
map (lambda t : write_section (t [0 ],t [1 ]), self ._sections .items ())
313
316
314
-
315
317
@needs_values
316
318
def write (self ):
317
319
"""Write changes to our file, if there are changes at all
0 commit comments