@@ -23,8 +23,6 @@ def __new__(metacls, name, bases, clsdict):
2323 Equip all base-class methods with a _needs_values decorator, and all non-const methods
2424 with a _set_dirty_and_flush_changes decorator in addition to that.
2525 """
26- new_type = super (_MetaParserBuilder , metacls ).__new__ (metacls , name , bases , clsdict )
27-
2826 mutating_methods = clsdict ['_mutating_methods_' ]
2927 for base in bases :
3028 methods = ( t for t in inspect .getmembers (base , inspect .ismethod ) if not t [0 ].startswith ("_" ) )
@@ -35,9 +33,11 @@ def __new__(metacls, name, bases, clsdict):
3533 if name in mutating_methods :
3634 method_with_values = _set_dirty_and_flush_changes (method_with_values )
3735 # END mutating methods handling
36+
3837 clsdict [name ] = method_with_values
3938 # END for each base
4039
40+ new_type = super (_MetaParserBuilder , metacls ).__new__ (metacls , name , bases , clsdict )
4141 return new_type
4242
4343
@@ -98,7 +98,7 @@ class GitConfigParser(cp.RawConfigParser, object):
9898 )
9999
100100 # list of RawConfigParser methods able to change the instance
101- _mutating_methods_ = ("remove_section" , "remove_option" , "set" )
101+ _mutating_methods_ = ("add_section" , " remove_section" , "remove_option" , "set" )
102102
103103 def __init__ (self , file_or_files , read_only = True ):
104104 """
@@ -141,8 +141,10 @@ def __del__(self):
141141 """
142142 Write pending changes if required and release locks
143143 """
144- if self .read_only :
145- return
144+ # checking for the lock here makes sure we do not raise during write()
145+ # in case an invalid parser was created who could not get a lock
146+ if self .read_only or not self ._has_lock ():
147+ return
146148
147149 try :
148150 try :
@@ -242,7 +244,7 @@ def read(self):
242244 close_fp = False
243245 # assume a path if it is not a file-object
244246 if not hasattr (file_object , "seek" ):
245- fp = open (file_object , "w" )
247+ fp = open (file_object )
246248 close_fp = True
247249 # END fp handling
248250
@@ -271,6 +273,7 @@ def write_section(name, section_dict):
271273 map (lambda t : write_section (t [0 ],t [1 ]), self ._sections .items ())
272274
273275
276+ @_needs_values
274277 def write (self ):
275278 """
276279 Write changes to our file, if there are changes at all
@@ -307,6 +310,15 @@ def _assure_writable(self, method_name):
307310 if self .read_only :
308311 raise IOError ("Cannot execute non-constant method %s.%s" % (self , method_name ))
309312
313+ @_needs_values
314+ @_set_dirty_and_flush_changes
315+ def add_section (self , section ):
316+ """
317+ Assures added options will stay in order
318+ """
319+ super (GitConfigParser , self ).add_section (section )
320+ self ._sections [section ] = OrderedDict ()
321+
310322 @property
311323 def read_only (self ):
312324 """
0 commit comments