diff options
author | Raphael Boidol <raphael.boidol@tngtech.com> | 2016-02-21 15:40:44 +0100 |
---|---|---|
committer | Raphael Boidol <raphael.boidol@tngtech.com> | 2016-03-06 16:47:44 +0100 |
commit | d97a8bb75098ad643d1a8853fe1b59cbb8e2338c (patch) | |
tree | 12efb271cfdc3864c178d584ee1a2998d2902207 /git/config.py | |
parent | 0f4d5ce5f9459e4c7fe4fab95df1a1e4c9be61ca (diff) | |
download | gitpython-d97a8bb75098ad643d1a8853fe1b59cbb8e2338c.tar.gz |
config parsers as context mangers can now be reentered for locks
Diffstat (limited to 'git/config.py')
-rw-r--r-- | git/config.py | 36 |
1 files changed, 21 insertions, 15 deletions
diff --git a/git/config.py b/git/config.py index 21af6159..5bd10975 100644 --- a/git/config.py +++ b/git/config.py @@ -133,10 +133,11 @@ class SectionConstraint(object): return self._config.release() def __enter__(self): + self._config.__enter__() return self def __exit__(self, exception_type, exception_value, traceback): - self.release() + self._config.__exit__(exception_type, exception_value, traceback) class GitConfigParser(with_metaclass(MetaParserBuilder, cp.RawConfigParser, object)): @@ -155,8 +156,7 @@ class GitConfigParser(with_metaclass(MetaParserBuilder, cp.RawConfigParser, obje :note: The config is case-sensitive even when queried, hence section and option names must match perfectly. - If used as a context manager, will release the locked file. This parser cannot - be used afterwards.""" + If used as a context manager, will release the locked file.""" #{ Configuration # The lock type determines the type of lock to use in new configuration readers. @@ -206,18 +206,23 @@ class GitConfigParser(with_metaclass(MetaParserBuilder, cp.RawConfigParser, obje self._is_initialized = False self._merge_includes = merge_includes self._lock = None - - if not read_only: - if isinstance(file_or_files, (tuple, list)): - raise ValueError( - "Write-ConfigParsers can operate on a single file only, multiple files have been passed") - # END single file check - - if not isinstance(file_or_files, string_types): - file_or_files = file_or_files.name - # END get filename from handle/stream - # initialize lock base - we want to write - self._lock = self.t_lock(file_or_files) + self._aquire_lock() + + def _aquire_lock(self): + if not self._read_only: + if not self._lock: + if isinstance(self._file_or_files, (tuple, list)): + raise ValueError( + "Write-ConfigParsers can operate on a single file only, multiple files have been passed") + # END single file check + + file_or_files = self._file_or_files + if not isinstance(self._file_or_files, string_types): + file_or_files = self._file_or_files.name + # END get filename from handle/stream + # initialize lock base - we want to write + self._lock = self.t_lock(file_or_files) + # END lock check self._lock._obtain_lock() # END read-only check @@ -228,6 +233,7 @@ class GitConfigParser(with_metaclass(MetaParserBuilder, cp.RawConfigParser, obje self.release() def __enter__(self): + self._aquire_lock() return self def __exit__(self, exception_type, exception_value, traceback): |