Source

show / show / core.py

Diff from to

show/core.py

 import textwrap
 
 from options.nulltype import NullType
-Private = NullType('Private')
+Private    = NullType('Private')
 Impossible = NullType('Impossible')
+Ignore     = NullType('Ignore')
 
 def wrapped_if(value, prefix="", suffix="", transform=None):
     """
     delivered via cache lookup.
     """
 
-    TARGET_FUNCS = set(['show', 'show.items', 'show.props'])  # functions we care about
+    TARGET_FUNCS = set(['show', 'show.items', 'show.props', 'show.watch'])  # functions we care about
 
     def __init__(self, filepath, lineno):
         ast.NodeVisitor.__init__(self)
         return data.strip().split(',')
     else:
         return data.strip().split()
+    
+def ellipsis(s, maxlen=232):
+    s = str(s)
+    if len(s) > maxlen:
+        return s[:maxlen-3] + '...'
+    else:
+        return s
 
+def _afunction(f): pass
+function = type(_afunction)
+module   = type(sys)
+class _XYZ(object):
+    def method(self): pass
+
+FUNKY = (function, module, type, type(_XYZ.method), type(len)) # funky => functional infrastructure
 
 class Show(object):
     """Show objects print debug output in a 'name: value' format that
             else:
                 return "{0}: {1!r}".format(name, value)
 
-    def arg_format_props(self, name, value, caller):
+    def arg_format_props(self, name, value, caller, ignore_double=True, ignore_funky=True):
         """
         Format a single argument to show properties.
         """
                     try:
                         propkeys = list(value.__dict__.keys())
                     except AttributeError:  # no __dict__ => __slots__ object
-                        propkeys = list(value.__slots__)
-                    cdict = value.__class__.__dict__
-                    realproperties = [ k for k in cdict.keys() if isinstance(cdict[k], property) ]
-                    propkeys.extend(realproperties)
+                        try:
+                            propkeys = list(value.__slots__)
+                        except AttributeError:
+                            propkeys = list(dir(value))
+                    try:
+                        cdict = value.__class__.__dict__
+                        realproperties = [ k for k in cdict.keys() if isinstance(cdict[k], property) ]
+                        propkeys.extend(realproperties)
+                    except AttributeError:
+                        pass
+            
+                    if ignore_double:
+                        propkeys = [ p for p in propkeys if not p.startswith('__') ]
+                    if ignore_funky:
+                        propkeys = [ p for p in propkeys if not isinstance(getattr(value, p), FUNKY) ]
+                    if self.opts.omit:
+                        propkeys = [ p for p in propkeys if not p in cwsv_or_list(self.opts.omit) ]
+
                     proplist = sorted(propkeys, key=lambda x: x.replace('_','~'))
-                propvals = [ "{0}={1}".format(p, self.value_repr(getattr(value, p))) for p in proplist ]
-                return "{0}: {1}".format(name, ' '.join(propvals))
+                #propvals = [ "{0}={1}".format(p, self.value_repr(getattr(value, p))) for p in proplist ]
+                #return "{0}: {1}".format(name, ' '.join(propvals))
+                propvals = [ "    {0}={1}".format(p, ellipsis(self.value_repr(getattr(value, p)))) for p in proplist ]
+                return "{0}:\n{1}".format(name, '\n'.join(propvals))
             except Exception:
                 return "{0}: {1}".format(name, self.value_repr(value))
        
         """
         Show properties of objects.
         """
-        opts = self.options.push(kwargs)
+        opts = self.opts = self.options.push(kwargs)
         if len(args) > 1 and isinstance(args[-1], str):
             used = opts.addflat([ args[-1] ], ['props'])
-            args = args[:-1]        
+            args = args[:-1]
+        if opts.sep == Show.options.sep:
+            opts.sep = '\n\n'
         caller = inspect.currentframe().f_back
         return self._showcore(args, kwargs, caller, self.arg_format_props, opts)
     
+        # should this check for and show (perhaps with ^ annotation), properties
+        # of object inherited from class?
+    
     def locals(self, *args, **kwargs):
         """
         Show all local vars, plus any other values mentioned.
         """
         opts = self.options.push(kwargs)
         caller = inspect.currentframe().f_back
-        assert not args # for now
         
-        locdict = dict([ (k, v) for (k, v) in caller.f_locals.items() if not k.startswith('@py_assert') ])
-        watching = self._watching.get(id(caller), None)
+        f_locals = caller.f_locals
+        _id = id(f_locals)
+
+        valitems  = [ (k, v) for (k, v) in f_locals.items() if \
+                                not k.startswith('@py_assert') and \
+                                not k.startswith('_') and \
+                                not isinstance(v, FUNKY) and \
+                                not getattr(v, '__module__', '').startswith( ('IPython', 'site', 'show')) and \
+                                not (isInteractive and (k == 'In' or k == 'Out'))
+                            ]        
+        if args:
+            self.say("args = {args!r}")
+            argtuples = self.get_arg_tuples(caller, args)
+            valitems.extend(argtuples)
+        
+        valdict = dict(valitems)
+        _id = id(f_locals)
+        watching = self._watching.get(_id, None)
         if watching is None:
-            to_show = {}
-            to_show.update(locdict)
-            self._watching[id(caller)] = watching = to_show
+            self._watching[_id] = watching = to_show = valdict
         else:
             to_show = {}
-            for k, v in locdict.items():
+            for k, v in valdict.items():
                 if k not in watching or v != watching[k]:
                     to_show[k] = v
                     watching[k] = v
+            
                 
         omit = cwsv_or_list(opts.omit)
             
         if names:
             valstr = opts.sep.join([ self.arg_format(name, to_show[name], caller) for name in names ])
         else:
-            valstr = '<unchanged>'
+            valstr = six.u('\u2205')
         locval = [ self.call_location(caller) + ":  ", valstr ] if opts.where else [ valstr ]
 
         # Emit the result string, and optionally return it
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.