diff options
| author | Julien Danjou <julien@danjou.info> | 2016-07-27 15:11:42 +0200 |
|---|---|---|
| committer | Julien Danjou <julien@danjou.info> | 2016-09-30 17:17:47 +0200 |
| commit | 1ed3c26568e53490912ea9b93224343dc3b8124b (patch) | |
| tree | be9cabe583fe7a7663e15b6ddc5526dd0daa3416 /tooz/drivers/file.py | |
| parent | 3bab404574474e0e50a6bd995a14a686d79501bc (diff) | |
| download | tooz-1ed3c26568e53490912ea9b93224343dc3b8124b.tar.gz | |
file: move _load_and_validate to a method
This is only used in one class, so move it where it's used for more
clarity.
Change-Id: I6d73d75a2e633811f970ee8dd99873a5b96cf744
Diffstat (limited to 'tooz/drivers/file.py')
| -rw-r--r-- | tooz/drivers/file.py | 60 |
1 files changed, 28 insertions, 32 deletions
diff --git a/tooz/drivers/file.py b/tooz/drivers/file.py index 7586418..cd20932 100644 --- a/tooz/drivers/file.py +++ b/tooz/drivers/file.py @@ -59,23 +59,6 @@ def _translate_failures(): cause=e) -_SCHEMAS = { - 'group': voluptuous.Schema({ - voluptuous.Required('group_id'): voluptuous.Any(six.text_type, - six.binary_type), - # NOTE(sileht): tooz <1.36 was creating file without this - voluptuous.Optional('encoded'): bool, - }), - 'member': voluptuous.Schema({ - voluptuous.Required('member_id'): voluptuous.Any(six.text_type, - six.binary_type), - voluptuous.Required('joined_on'): datetime.datetime, - # NOTE(sileht): tooz <1.36 was creating file without this - voluptuous.Optional('encoded'): bool, - }, extra=voluptuous.ALLOW_EXTRA), -} - - def _convert_from_old_format(data): # NOTE(sileht): previous version of the driver was storing str as-is # making impossible to read from python3 something written with python2 @@ -102,14 +85,6 @@ def _convert_from_old_format(data): return data -def _load_and_validate(blob, schema_key): - data = utils.loads(blob) - data = _convert_from_old_format(data) - schema = _SCHEMAS[schema_key] - schema(data) - return data - - def _lock_me(lock): def wrapper(func): @@ -367,10 +342,32 @@ class FileDriver(coordination._RunWatchersMixin, fut = self._executor.submit(_do_leave_group) return FileFutureResult(fut) - @staticmethod - def _read_member_id(path): + _SCHEMAS = { + 'group': voluptuous.Schema({ + voluptuous.Required('group_id'): voluptuous.Any(six.text_type, + six.binary_type), + # NOTE(sileht): tooz <1.36 was creating file without this + voluptuous.Optional('encoded'): bool, + }), + 'member': voluptuous.Schema({ + voluptuous.Required('member_id'): voluptuous.Any(six.text_type, + six.binary_type), + voluptuous.Required('joined_on'): datetime.datetime, + # NOTE(sileht): tooz <1.36 was creating file without this + voluptuous.Optional('encoded'): bool, + }, extra=voluptuous.ALLOW_EXTRA), + } + + def _load_and_validate(self, blob, schema_key): + data = utils.loads(blob) + data = _convert_from_old_format(data) + schema = self._SCHEMAS[schema_key] + schema(data) + return data + + def _read_member_id(self, path): with open(path, 'rb') as fh: - details = _load_and_validate(fh.read(), 'member') + details = self._load_and_validate(fh.read(), 'member') if details.get("encoded"): return details[u'member_id'].decode("utf-8") return details[u'member_id'] @@ -428,7 +425,7 @@ class FileDriver(coordination._RunWatchersMixin, else: raise else: - details = _load_and_validate(contents, 'member') + details = self._load_and_validate(contents, 'member') return details.get(u"capabilities") fut = self._executor.submit(_do_get_member_capabilities) @@ -460,10 +457,9 @@ class FileDriver(coordination._RunWatchersMixin, fut = self._executor.submit(_do_delete_group) return FileFutureResult(fut) - @staticmethod - def _read_group_id(path): + def _read_group_id(self, path): with open(path, 'rb') as fh: - details = _load_and_validate(fh.read(), 'group') + details = self._load_and_validate(fh.read(), 'group') if details.get("encoded"): return details[u'group_id'].decode("utf-8") return details[u'group_id'] |
