1010from git .utils import LazyMixin , Iterable
1111from refs import RemoteRef
1212
13- class _SectionConstrain (object ):
13+ class _SectionConstraint (object ):
1414 """
1515 Constrains a ConfigParser to only option commands which are constrained to
16- always use the section we have been initialized with
16+ always use the section we have been initialized with.
17+
18+ It supports all ConfigParser methods that operate on an option
1719 """
18- __slots__ = ( "_config" , "_section_name"
19- _valid_attrs_ = ("get" , "set" , "getint" , "getfloat" , "getboolean" )
20+ __slots__ = ("_config" , "_section_name" )
21+ _valid_attrs_ = ("get" , "set" , "getint" , "getfloat" , "getboolean" , "has_option" )
2022
2123 def __init__ (self , config , section ):
2224 self ._config = config
2325 self ._section_name = section
2426
2527 def __getattr__ (self , attr ):
28+ if attr in self ._valid_attrs_ :
29+ return lambda * args : self ._call_config (attr , * args )
30+ return super (_SectionConstraint ,self ).__getattribute__ (attr )
2631
32+ def _call_config (self , method , * args ):
33+ """Call the configuration at the given method which must take a section name
34+ as first argument"""
35+ return getattr (self ._config , method )(self ._section_name , * args )
2736
28- def get (option ):
29- return self ._config .get (self ._section_name , option )
30-
31- def set (option , value ):
32- return self ._config .set (self ._section_name , option , value )
33-
34-
3537
3638class Remote (LazyMixin , Iterable ):
3739 """
@@ -44,7 +46,7 @@ class Remote(LazyMixin, Iterable):
4446 to speed up subsequent accesses.
4547 """
4648
47- __slots__ = ( "repo" , "name" , "_config " )
49+ __slots__ = ( "repo" , "name" , "_config_reader " )
4850
4951 def __init__ (self , repo , name ):
5052 """
@@ -64,11 +66,17 @@ def __getattr__(self, attr):
6466 Allows to call this instance like
6567 remote.special( *args, **kwargs) to call git-remote special self.name
6668 """
67- return self ._call_cmd (attr )
69+ if attr == "_config_reader" :
70+ return super (Remote , self ).__getattr__ (attr )
71+
72+ return self ._config_reader .get (attr )
73+
74+ def _config_section_name (self ):
75+ return 'remote "%s"' % self .name
6876
6977 def _set_cache_ (self , attr ):
70- if attr == "_config " :
71- self ._config = self .repo .config_reader
78+ if attr == "_config_reader " :
79+ self ._config_reader = _SectionConstraint ( self .repo .config_reader , self . _config_section_name ())
7280 else :
7381 super (Remote , self )._set_cache_ (attr )
7482
@@ -168,7 +176,7 @@ def rename(self, new_name):
168176
169177 self .repo .git .remote ("rename" , self .name , new_name )
170178 self .name = new_name
171- del (self ._config ) # it contains cached values, section names are different now
179+ del (self ._config_reader ) # it contains cached values, section names are different now
172180 return self
173181
174182 def update (self , ** kwargs ):
@@ -184,3 +192,31 @@ def update(self, **kwargs):
184192 self .repo .git .remote ("update" , self .name )
185193 return self
186194
195+ @property
196+ def config_reader (self ):
197+ """
198+ Returns
199+ GitConfigParser compatible object able to read options for only our remote.
200+ Hence you may simple type config.get("pushurl") to obtain the information
201+ """
202+ return self ._config_reader
203+
204+ @property
205+ def config_writer (self ):
206+ """
207+ Return
208+ GitConfigParser compatible object able to write options for this remote.
209+
210+ Note
211+ You can only own one writer at a time - delete it to release the
212+ configuration file and make it useable by others.
213+
214+ To assure consistent results, you should only query options through the
215+ writer. Once you are done writing, you are free to use the config reader
216+ once again.
217+ """
218+ writer = self .repo .config_writer ()
219+
220+ # clear our cache to assure we re-read the possibly changed configuration
221+ del (self ._config_reader )
222+ return _SectionConstraint (writer , self ._config_section_name ())
0 commit comments