Debugger/VariablesViewer.py

changeset 5171
f1e9eebd5469
parent 5169
74e000797a93
child 5173
632257ad7337
--- a/Debugger/VariablesViewer.py	Wed Sep 14 20:08:16 2016 +0200
+++ b/Debugger/VariablesViewer.py	Fri Sep 16 19:28:39 2016 +0200
@@ -16,12 +16,9 @@
 from PyQt5.QtCore import Qt, QRegExp, qVersion, QCoreApplication
 from PyQt5.QtWidgets import QTreeWidget, QTreeWidgetItem, QAbstractItemView, \
     QMenu
-from PyQt5.QtGui import QTextDocument   # __IGNORE_WARNING__
 
 from E5Gui.E5Application import e5App
 
-from DebugClients.Python3.DebugConfig import ConfigVarTypeStrings
-    
 from .Config import ConfigVarTypeDispStrings
 
 import Preferences
@@ -32,6 +29,16 @@
     """
     Class implementing the data structure for variable items.
     """
+    Indicators = ("()", "[]", "{:}", "{}")      # __IGNORE_WARNING_M613__
+    Type2Indicators = {
+        # Python types
+        'list': '[]',
+        'tuple': '()',
+        'dict': '{:}',                          # __IGNORE_WARNING_M613__
+        'set': '{}',                            # __IGNORE_WARNING_M613__
+        'frozenset': '{}',                      # __IGNORE_WARNING_M613__
+    }
+    
     def __init__(self, parent, dvar, dvalue, dtype):
         """
         Constructor
@@ -41,6 +48,8 @@
         @param dvalue value string (string)
         @param dtype type string (string)
         """
+        dvar, self.__varID = VariableItem.extractId(dvar)
+        
         self.__value = dvalue
         if len(dvalue) > 2048:     # 1024 * 2
             dvalue = QCoreApplication.translate(
@@ -77,7 +86,71 @@
         @return value of the item (string)
         """
         return self.__value
+    
+    def getId(self):
+        """
+        Public method to get the ID string.
         
+        @return ID string
+        @rtype str
+        """
+        return self.__varID
+    
+    @classmethod
+    def extractId(cls, var):
+        """
+        Class method to extract the ID string from a variable text.
+        
+        @param var variable text
+        @type str
+        @return tuple containing the variable text without ID and the ID string
+        @rtype tuple of two str
+        """
+        if " (ID:" in var:
+            dvar, varID = var.rsplit(None, 1)
+            if varID.endswith(VariableItem.Indicators):
+                varID, indicators = VariableItem.extractIndicators(varID)
+                dvar += indicators
+        else:
+            dvar = var
+            varID = None
+        
+        return dvar, varID
+    
+    @classmethod
+    def extractIndicators(cls, var):
+        """
+        Class method to extract the indicator string from a variable text.
+        
+        @param var variable text
+        @type str
+        @return tuple containing the variable text without indicators and the
+            indicator string
+        @rtype tuple of two str
+        """
+        for indicator in VariableItem.Indicators:
+            if var.endswith(indicator):
+                return var[:-len(indicator)], indicator
+        
+        return var, ""
+    
+    def _buildKey(self):
+        """
+        Protected method to build the access key for the variable.
+        
+        @return access key
+        @type str
+        """
+        indicators = ""
+        txt = self.text(0)
+        if txt.endswith(VariableItem.Indicators):
+            txt, indicators = VariableItem.extractIndicators(txt)
+        if self.__varID:
+            txt = "{0} {1}{2}".format(txt, self.__varID, indicators)
+        else:
+            txt = "{0}{1}".format(txt, indicators)
+        return txt
+    
     def data(self, column, role):
         """
         Public method to return the data for the requested role.
@@ -156,12 +229,12 @@
         self.deleteChildren()
         self.populated = True
         
-        pathlist = [self.text(0)]
+        pathlist = [self._buildKey()]
         par = self.parent()
         
         # step 1: get a pathlist up to the requested variable
         while par is not None:
-            pathlist.insert(0, par.text(0))
+            pathlist.insert(0, par._buildKey())
             par = par.parent()
         
         # step 2: request the variable from the debugger
@@ -221,9 +294,9 @@
         element 2 will have a key of '000002' and appear before
         element 10 with a key of '000010'
         """
-        col0Str = self.text(0)[:-2]  # strip off [], () or {}
-        indicator = self.text(0)[-2:]
-        self.setText(0, "{0:6d}{1}".format(int(col0Str), indicator))
+        # strip off [], () or {}
+        col0Str, indicators = VariableItem.extractIndicators(self.text(0))
+        self.setText(0, "{0:6d}{1}".format(int(col0Str), indicators))
 
 
 class VariablesViewer(QTreeWidget):
@@ -254,14 +327,6 @@
         self.__debugViewer = viewer
         self.__globalScope = globalScope
         
-        self.indicators = {
-            # Python types
-            'list': '[]',
-            'tuple': '()',
-            'dict': '{}',                  # __IGNORE_WARNING__
-                           
-        }
-        
         self.rx_class = QRegExp('<.*(instance|object) at 0x.*>')
         self.rx_class2 = QRegExp('class .*')
         self.rx_class3 = QRegExp('<class .* at 0x.*>')
@@ -370,12 +435,17 @@
         else:
             count = node.childCount()
         
+        if column == 0:
+            searchStr = VariableItem.extractId(slist[0])[0]
+        else:
+            searchStr = slist[0]
+        
         for index in range(count):
             if node is None:
                 itm = self.topLevelItem(index)
             else:
                 itm = node.child(index)
-            if itm.text(column) == slist[0]:
+            if itm.text(column) == searchStr:
                 if len(slist) > 1:
                     itm = self.__findItem(slist[1:], column, itm)
                 return itm
@@ -542,12 +612,12 @@
         if parent is None:
             parent = self
         try:
-            dvar = '{0}{1}'.format(var, self.indicators[vtype])
+            dvar = '{0}{1}'.format(var, VariableItem.Type2Indicators[vtype])
         except KeyError:
             dvar = var
         dvtype = self.__getDispType(vtype)
         
-        if vtype in ['list', 'Array', 'tuple', 'dict', 'Hash']:
+        if vtype in ['list', 'tuple', 'dict', 'set', 'frozenset']:
             itm = self.__generateItem(parent, dvar,
                                       self.tr("{0} items").format(value),
                                       dvtype, True)
@@ -575,12 +645,10 @@
         @return displaystring (string)
         """
         try:
-            i = ConfigVarTypeStrings.index(vtype)
-            dvtype = self.tr(ConfigVarTypeDispStrings[i])
-        except ValueError:
+            dvtype = self.tr(ConfigVarTypeDispStrings[vtype])
+        except KeyError:
             if vtype == 'classobj':
-                dvtype = self.tr(ConfigVarTypeDispStrings[
-                    ConfigVarTypeStrings.index('instance')])
+                dvtype = self.tr(ConfigVarTypeDispStrings['instance'])
             else:
                 dvtype = vtype
         return dvtype
@@ -628,21 +696,20 @@
             return  # do not display anything, if the variable has no value
             
         vtype = itm.text(2)
-        name = itm.text(0)
-        if name[-2:] in ['[]', '{}', '()']:         # __IGNORE_WARNING__
-            name = name[:-2]
+        name = VariableItem.extractIndicators(itm.text(0).strip())[0]
         
         par = itm.parent()
         nlist = [name]
         # build up the fully qualified name
         while par is not None:
-            pname = par.text(0)
-            if pname[-2:] in ['[]', '{}', '()']:    # __IGNORE_WARNING__
+            pname, indicators = VariableItem.extractIndicators(
+                par.text(0).strip())
+            if indicators:
                 if nlist[0].endswith("."):
                     nlist[0] = '[{0}].'.format(nlist[0][:-1])
                 else:
                     nlist[0] = '[{0}]'.format(nlist[0])
-                nlist.insert(0, pname[:-2])
+                nlist.insert(0, pname)
             else:
                 nlist.insert(0, '{0}.'.format(pname))
             par = par.parent()

eric ide

mercurial