On 6 February 2013 14:38, Michael Hanselmann <[email protected]> wrote:
> Up until now nodes were stored as a dictionary. The keys were hardcoded
> in a lot of places and entries modified directly.
>
> This patch introduces a new class for nodes in QA named “_QaNode”. It
> still supports accessing details via dictionary syntax, but that will be
> removed after a couple of patches changing all users. Unit tests for
> “qa_config.AcquireNode” are also included.
> ---
> qa/qa_config.py | 106
> ++++++++++++++++++++++++++++++++++-----
> qa/qa_node.py | 13 +++--
> test/py/qa.qa_config_unittest.py | 40 +++++++++++++++
> 3 files changed, 141 insertions(+), 18 deletions(-)
>
> diff --git a/qa/qa_config.py b/qa/qa_config.py
> index 4fe6a92..7036a15 100644
> --- a/qa/qa_config.py
> +++ b/qa/qa_config.py
> @@ -113,8 +113,85 @@ class _QaInstance(object):
> return default
>
>
> +class _QaNode(object):
> + __slots__ = [
> + "primary",
> + "secondary",
> + "_added",
> + "use_count",
> + ]
> +
> + def __init__(self, primary, secondary):
> + """Initializes instances of this class.
> +
> + """
> + self.primary = primary
> + self.secondary = secondary
> + self.use_count = 0
> + self._added = False
> +
> + @classmethod
> + def FromDict(cls, data):
> + """Creates node object from JSON dictionary.
> +
> + """
> + return cls(primary=data["primary"], secondary=data.get("secondary"))
> +
> + def __getitem__(self, key):
> + """Legacy dict-like interface.
> +
> + """
> + if key == "primary":
> + return self.primary
> + elif key == "secondary":
> + return self.secondary
> + else:
> + raise KeyError(key)
> +
> + def get(self, key, default):
> + """Legacy dict-like interface.
> +
> + """
> + try:
> + return self[key]
> + except KeyError:
> + return default
> +
> + def Use(self):
> + """Marks a node as being in use.
> +
> + """
> + assert self.use_count >= 0
> +
> + self.use_count += 1
> +
> + return self
This is not very intuitive (I would expect it to return the use count
if anything) and not very useful either, so please don't return
anything.
Rest LGTM.
Bernardo