On 01/28/2016 03:20 PM, Tomas Babej wrote:



On 01/27/2016 03:58 PM, Martin Babinsky wrote:
On 01/18/2016 06:43 PM, Martin Babinsky wrote:
A little patch that should make some future pylint errors disappear.



Attaching updated patch that does not promote direct molestation of
instance dictionaries.




Patch looks good, one thing I am concerened about though is that
__todict__ now returns a direct reference to the internal, mutable dict,
and no longer a (shallow) copy.

Maybe we should use dict.copy() there?

Tomas


Ah I didn't realize that. Fixed in updated patch.

--
Martin^3 Babinsky
From 81ab2ea956eda611717f24474b9c5f09cf4dcfff Mon Sep 17 00:00:00 2001
From: Martin Babinsky <mbabi...@redhat.com>
Date: Mon, 18 Jan 2016 18:35:52 +0100
Subject: [PATCH] ipalib/cli.py: pythonify Collector class

The implementation of Collector class is hard for pylint to chew and unwieldy.
This patch rewrites the class to a more readable and pythonic form.
---
 ipalib/cli.py | 22 ++++++++++++++++------
 1 file changed, 16 insertions(+), 6 deletions(-)

diff --git a/ipalib/cli.py b/ipalib/cli.py
index 136e0aeb8b876b2fe021f08e49a85a0fdeb4b21b..f4e173bc56c37088ba841369739bc2bd4e3e60a9 100644
--- a/ipalib/cli.py
+++ b/ipalib/cli.py
@@ -972,20 +972,30 @@ cli_application_commands = (
 
 class Collector(object):
     def __init__(self):
-        object.__setattr__(self, '_Collector__options', {})
+        self.__dict__.update(options={})
+        self.options = {}  # silence pylint
 
     def __setattr__(self, name, value):
-        if name in self.__options:
-            v = self.__options[name]
+        if name in dir(self):
+            object.__setattr__(self, name, value)
+        else:
+            self.__setitem__(name, value)
+
+    def __getitem__(self, item):
+        return self.options[item]
+
+    def __setitem__(self, key, value):
+        if key in self.options:
+            v = self.options[key]
             if type(v) is tuple:
                 value = v + (value,)
             else:
                 value = (v, value)
-        self.__options[name] = value
-        object.__setattr__(self, name, value)
+        self.options[key] = value
 
     def __todict__(self):
-        return dict(self.__options)
+        return dict.copy(self.options)
+
 
 class CLIOptionParserFormatter(optparse.IndentedHelpFormatter):
     def format_argument(self, name, help_string):
-- 
2.5.0

-- 
Manage your subscription for the Freeipa-devel mailing list:
https://www.redhat.com/mailman/listinfo/freeipa-devel
Contribute to FreeIPA: http://www.freeipa.org/page/Contribute/Code

Reply via email to