Commits

Jonathan Eunice committed 9c465ed

added show flag; added tests and docs for it

Comments (0)

Files changed (5)

 See ``say`` `say <http://pypi.python.org/pypi/say>`_ for additional detail on its
 operation.
 
+Turning Show On and Off
+=======================
+
+Often it's convenient to only display debugging information under some conditions,
+such as when a ``debug`` flag is set. That often leads to multi-line conditionals
+such as::
+
+    if debug:
+        print "x:", x, "y:", y, "z:", z
+
+With ``show`` it's a bit easier. There's a keyword argument, also called
+``show``, that controls whether anything is shown. Set it to ``True`` or ``False``
+to show or not show, or set it to the debug flag::
+
+    show(x, y, z, show=debug)
+
 Showing Collections
 ===================
 
 See ``say`` `say <http://pypi.python.org/pypi/say>`_ for additional detail on its
 operation.
 
+Turning Show On and Off
+=======================
+
+Often it's convenient to only display debugging information under some conditions,
+such as when a ``debug`` flag is set. That often leads to multi-line conditionals
+such as::
+
+    if debug:
+        print "x:", x, "y:", y, "z:", z
+
+With ``show`` it's a bit easier. There's a keyword argument, also called
+``show``, that controls whether anything is shown. Set it to ``True`` or ``False``
+to show or not show, or set it to the debug flag::
+
+    show(x, y, z, show=debug)
+
 Showing Collections
 ===================
 
 
 setup(
     name='show',
-    version='0.64',
+    version='0.65',
     author='Jonathan Eunice',
     author_email='jonathan.eunice@gmail.com',
     description='Debug print statements, done right. E.g. show(x)',
         props=Transient,    # props desired to print (given at call time)
         omit=Transient,     # vars not to print (for show.locals)
         fmtfunc=repr,       # formatting func used to format each value
+        show=True,          # show or not
     )
 
     def __init__(self, **kwargs):
         """
         Main entry point for Show objects.
         """
-        opts = self.options.push(kwargs)
+        opts = self.opts = self.options.push(kwargs)
+        if not opts.show:
+            return
         caller = inspect.currentframe().f_back
         formatter = self.arg_format if not opts.props else self.arg_format_props
         return self._showcore(args, kwargs, caller, formatter, opts)
         Show properties of objects.
         """
         opts = self.opts = self.options.push(kwargs)
+        if not opts.show:
+            return
         if len(args) > 1 and isinstance(args[-1], str):
             used = opts.addflat([ args[-1] ], ['props'])
             args = args[:-1]
     
         # should this check for and show (perhaps with ^ annotation), properties
         # of object inherited from class?
+        
+        # if no props, should show normally?
+        # Ie less difference between show, show.items, show.props
+        # also, maybe more automatic or easy-to-specify truncation of results?
     
     def locals(self, *args, **kwargs):
         """
         Show the local variables, then again only when changed.
         """
         opts = self.opts = self.options.push(kwargs)
+        if not opts.show:
+            return
+        
         caller = inspect.currentframe().f_back
         
         f_locals = caller.f_locals
         pf = lambda x: pformat(x, indent=indent, width=width, depth=depth)
         if mode == 'text':
             self.set(fmtfunc=pf)
+            return
         elif mode == 'ansi':
+            if 'Komodo' in os.environ.get('PYDBGP_PATH', ''):
+                self.set(fmtfunc=pf)
+                return
             try:
                 from pygments import highlight
                 from pygments.lexers import PythonLexer
                 from pygments.formatters import Terminal256Formatter
                 formatter = Terminal256Formatter(style=style)
                 self.set(fmtfunc=lambda x: highlight(pf(x), lexer, formatter).strip())
+                return
             except ImportError:
                 raise ImportWarning('install pygments for ANSI formatting; falling back to plain text')
                 self.set(fmtfunc=pf)
+                return
             except Exception as e:
                 raise e
         else:

test/test_show.py

     assert show.watch(_z) == '_z: 99'
     assert show.watch() == six.u('\u2205')
     
+def test_show_flag():
+    """
+    Test that the show keyword argument actually turns showing on or off.
+    """
+    x = 1
+    assert show(x) == 'x: 1'
+    assert show(x, show=True) == 'x: 1'
+    assert show(x, show=False) == None
+    
 def test_show_retval(capsys):
 
     # this tweak needed to make pytest's capsys work right