[pytango] 94/122: Rename DeviceProxy._getAttributeNames method to __dir__

Sandor Bodo-Merle sbodomerle-guest at moszumanska.debian.org
Thu Sep 28 19:18:22 UTC 2017


This is an automated email from the git hooks/post-receive script.

sbodomerle-guest pushed a commit to tag v9.2.1
in repository pytango.

commit b3b7b8c47a409830e352d5221e4cbfacdf248870
Author: Vincent Michel <vincent.michel at maxlab.lu.se>
Date:   Wed Dec 14 16:52:14 2016 +0100

    Rename DeviceProxy._getAttributeNames method to __dir__
---
 tango/device_proxy.py | 33 ++++++++++++++++++++++-----------
 tango/utils.py        | 38 +++++++++++++++++++++++++++++++++++++-
 2 files changed, 59 insertions(+), 12 deletions(-)

diff --git a/tango/device_proxy.py b/tango/device_proxy.py
index 951690e..a3ff3de 100644
--- a/tango/device_proxy.py
+++ b/tango/device_proxy.py
@@ -32,6 +32,7 @@ from .utils import is_pure_str, is_non_str_seq, is_integer
 from .utils import seq_2_StdStringVector, StdStringVector_2_seq
 from .utils import seq_2_DbData, DbData_2_dict
 from .utils import document_method as __document_method
+from .utils import dir2
 
 from .green import result, submit, green, green_cb
 from .green import get_green_mode, get_event_loop, get_wait_default_value
@@ -309,18 +310,29 @@ def __DeviceProxy__setattr(self, name, value):
     return super(DeviceProxy, self).__setattr__(name, value)
 
 
-def __DeviceProxy__getAttributeNames(self):
-    """Return list of magic attributes to extend introspection."""
+def __DeviceProxy__dir(self):
+    """Return the attribute list including tango objects."""
+    extra_entries = set()
+    # Add commands
     try:
-        lst = [cmd.cmd_name for cmd in self.command_list_query()]
-        lst += self.get_attribute_list()
-        lst += self.get_pipe_list()
-        lst += list(map(str.lower, lst))
-        lst.sort()
-        return lst
+        extra_entries.update(self.get_command_list())
     except Exception:
         pass
-    return []
+    # Add attributes
+    try:
+        extra_entries.update(self.get_attribute_list())
+    except Exception:
+        pass
+    # Add pipes
+    try:
+        extra_entries.update(self.get_pipe_list())
+    except Exception:
+        pass
+    # Merge with default dir implementation
+    extra_entries.update(map(str.lower, extra_entries))
+    entries = extra_entries.union(dir2(self))
+    return sorted(entries)
+
 
 def __DeviceProxy__getitem(self, key):
     return self.read_attribute(key)
@@ -1314,8 +1326,7 @@ def __init_DeviceProxy():
     DeviceProxy.__getitem__ = __DeviceProxy__getitem
     DeviceProxy.__setitem__ = __DeviceProxy__setitem
     DeviceProxy.__contains__ = __DeviceProxy__contains
-
-    DeviceProxy._getAttributeNames = __DeviceProxy__getAttributeNames
+    DeviceProxy.__dir__ = __DeviceProxy__dir
 
     DeviceProxy.__get_cmd_cache = __DeviceProxy__get_cmd_cache
     DeviceProxy.__get_attr_cache = __DeviceProxy__get_attr_cache
diff --git a/tango/utils.py b/tango/utils.py
index f81226a..8681b99 100644
--- a/tango/utils.py
+++ b/tango/utils.py
@@ -18,6 +18,7 @@ from __future__ import print_function
 
 import os
 import sys
+import types
 import numbers
 import collections
 
@@ -41,7 +42,7 @@ __all__ = [
     "CaselessList", "CaselessDict", "EventCallBack", "get_home",
     "from_version_str_to_hex_str", "from_version_str_to_int",
     "seq_2_StdStringVector", "StdStringVector_2_seq",
-    "TO_TANGO_TYPE"]
+    "dir2", "TO_TANGO_TYPE"]
 
 __docformat__ = "restructuredtext"
 
@@ -1591,3 +1592,38 @@ PyTango running on:
 """
     msg = msg.format(Release, Compile, Runtime)
     return msg
+
+
+def get_attrs(obj):
+    """Helper for dir2 implementation."""
+    if not hasattr(obj, '__dict__'):
+        return []  # slots only
+    if not isinstance(obj.__dict__, (dict, types.DictProxyType)):
+        raise TypeError("%s.__dict__ is not a dictionary" % obj.__name__)
+    return obj.__dict__.keys()
+
+
+def dir2(obj):
+    """Default dir implementation.
+
+    Inspired by gist: katyukha/dirmixin.py
+    https://gist.github.com/katyukha/c6e5e2b829e247c9b009
+    """
+    attrs = set()
+
+    if not hasattr(obj, '__bases__'):
+        # obj is an instance
+        if not hasattr(obj, '__class__'):
+            # slots
+            return sorted(get_attrs(obj))
+        klass = obj.__class__
+        attrs.update(get_attrs(klass))
+    else:
+        # obj is a class
+        klass = obj
+
+    for cls in klass.__bases__:
+        attrs.update(get_attrs(cls))
+        attrs.update(dir2(cls))
+    attrs.update(get_attrs(obj))
+    return list(attrs)

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/pytango.git



More information about the debian-science-commits mailing list