Compare configuration objects "smart" and "deep"

This commit is contained in:
Alexander Kukushkin
2016-05-31 16:16:29 +02:00
parent 1c2e1755cb
commit a55cbff865
2 changed files with 30 additions and 3 deletions
+4 -3
View File
@@ -6,6 +6,7 @@ import yaml
from copy import deepcopy
from patroni.postgresql import Postgresql
from patroni.utils import deep_compare
logger = logging.getLogger(__name__)
@@ -94,7 +95,7 @@ class Config(object):
logger.error('Can not remove temporary file %s', tmpfile)
def set_dynamic_configuration(self, configuration):
if self._dynamic_configuration != configuration:
if not deep_compare(self._dynamic_configuration, configuration):
try:
self.__effective_configuration = self._build_effective_configuration(configuration,
self._local_configuration)
@@ -108,10 +109,10 @@ class Config(object):
if self.config_file:
try:
configuration = self._load_config_file()
if self._local_configuration != configuration:
if not deep_compare(self._local_configuration, configuration):
new_configuration = self._build_effective_configuration(self._dynamic_configuration, configuration)
if dry_run:
return new_configuration != self.__effective_configuration
return not deep_compare(new_configuration, self.__effective_configuration)
self._local_configuration = configuration
self.__effective_configuration = new_configuration
return True
+26
View File
@@ -33,6 +33,32 @@ def calculate_ttl(expiration):
return int((expiration - now).total_seconds())
def deep_compare(obj1, obj2):
"""
>>> deep_compare({'1': None}, {})
False
>>> deep_compare({'1': {}}, {'1': None})
False
>>> deep_compare({'1': [1]}, {'1': [2]})
False
>>> deep_compare({'1': 2}, {'1': '2'})
True
>>> deep_compare({'1': {'2': [3, 4]}}, {'1': {'2': [3, 4]}})
True
"""
if set(list(obj1.keys())) != set(list(obj2.keys())): # Objects have different sets of keys
return False
for key, value in obj1.items():
if isinstance(value, dict):
if not (isinstance(obj2[key], dict) and deep_compare(value, obj2[key])):
return False
elif str(value) != str(obj2[key]):
return False
return True
def set_ignore_sigterm(value=True):
global __ignore_sigterm
__ignore_sigterm = value