1
1
Fork 0
mirror of https://github.com/pypa/pip synced 2023-12-13 21:30:23 +01:00
This commit is contained in:
Pradyun S. Gedam 2017-01-24 23:12:20 +05:30
parent 86777f1d48
commit 06367c0655

View file

@ -97,55 +97,65 @@ class Configuration(object):
# #
def get_value(self, key): def get_value(self, key):
"""Get a value from the configuration.
"""
return self._dictionary[key] return self._dictionary[key]
def set_value(self, key, value): def set_value(self, key, value):
"""Modify a value in the configuration. """Modify a value in the configuration.
Pre-Conditions:
- Need to be initialized with `load_only`.
- Need to be initialized with `load_only`.
""" """
assert self.load_only is not None, _need_file_err_msg assert self.load_only is not None, _need_file_err_msg
file, parser = self._get_parser_to_modify() file, parser = self._get_parser_to_modify()
section, name = _disassemble_key(key)
# logger.info("Setting: [%s]%s=%r", section, name, value) if parser is not None:
section, name = _disassemble_key(key)
# Modify the parser and the configuration
if not parser.has_section(section):
parser.add_section(section)
parser.set(section, name, value)
# Modify the parser and the configuration
if not parser.has_section(section):
parser.add_section(section)
parser.set(section, name, value)
self._config[self.load_only][key] = value self._config[self.load_only][key] = value
if parser not in self._modified_parsers: # MARK:: Maybe DRY this.
self._modified_parsers.append((file, parser)) file_parser_tuple = (file, parser)
if file_parser_tuple not in self._modified_parsers:
self._modified_parsers.append(file_parser_tuple)
def unset_value(self, key): def unset_value(self, key):
"""Unset a value in the configuration.
"""
assert self.load_only is not None, _need_file_err_msg assert self.load_only is not None, _need_file_err_msg
if key not in self._config[self.load_only]:
raise KeyError(key)
file, parser = self._get_parser_to_modify() file, parser = self._get_parser_to_modify()
section, name = _disassemble_key(key)
# Remove the key in the parser if parser is not None:
modified_something = ( section, name = _disassemble_key(key)
parser.has_section(section) and
parser.remove_option(section, name)
)
if modified_something: # Remove the key in the parser
# option removed, section may now be empty modified_something = (
if next(iter(parser.items(section)), None) is None: parser.has_section(section) and
parser.remove_section(section) parser.remove_option(section, name)
if parser not in self._modified_parsers: )
self._modified_parsers.append((file, parser))
else: if modified_something:
return False # name removed from parser, section may now be empty
if next(iter(parser.items(section)), None) is None:
parser.remove_section(section)
# MARK:: Maybe DRY this.
file_parser_tuple = (file, parser)
if file_parser_tuple not in self._modified_parsers:
self._modified_parsers.append(file_parser_tuple)
else:
# If here, something is there in the dictionary but not in the
# parser. This should not happen.
pass
# This is guarenteed
assert key in self._config[self.load_only]
del self._config[self.load_only][key] del self._config[self.load_only][key]
return True return True
@ -153,7 +163,6 @@ class Configuration(object):
def save(self): def save(self):
assert self.load_only is not None, _need_file_err_msg assert self.load_only is not None, _need_file_err_msg
# NOTE: Improve once the sat and unset routines are implemented.
for file, parser in self._modified_parsers: for file, parser in self._modified_parsers:
logger.info("Writing to %s", file) logger.info("Writing to %s", file)
@ -268,6 +277,7 @@ class Configuration(object):
# Determine which parser to modify # Determine which parser to modify
parsers = self._parsers[self.load_only] parsers = self._parsers[self.load_only]
if not parsers: if not parsers:
# This should not happen if we're doing it correctly.
raise Exception("What!?") raise Exception("What!?")
# Use the highest priority parser. # Use the highest priority parser.