chiark / gitweb /
catacomb/__init__.py: Don't print secret bits of keys by default.
[catacomb-python] / catacomb / __init__.py
index d0411beca600e7b9427ab9c69bbe7b4cf0568b02..c2470adba97ea78b739654262f3abade94c97d4f 100644 (file)
@@ -83,17 +83,24 @@ def _checkend(r):
   return x
 
 ## Some pretty-printing utilities.
+PRINT_SECRETS = False
 def _clsname(me): return type(me).__name__
+def _repr_secret(thing, secretp = True):
+  if not secretp or PRINT_SECRETS: return repr(thing)
+  else: return '#<SECRET>'
 def _pp_str(me, pp, cyclep): pp.text(cyclep and '...' or str(me))
+def _pp_secret(pp, thing, secretp = True):
+  if not secretp or PRINT_SECRETS: pp.pretty(thing)
+  else: pp.text('#<SECRET>')
 def _pp_bgroup(pp, text):
   ind = len(text)
   pp.begin_group(ind, text)
   return ind
 def _pp_bgroup_tyname(pp, obj, open = '('):
   return _pp_bgroup(pp, _clsname(obj) + open)
-def _pp_kv(pp, k, v):
+def _pp_kv(pp, k, v, secretp = False):
   ind = _pp_bgroup(pp, k + ' = ')
-  pp.pretty(v)
+  _pp_secret(pp, v, secretp)
   pp.end_group(ind, '')
 def _pp_commas(pp, printfn, items):
   firstp = True
@@ -472,14 +479,17 @@ _augment(KeyAttributes, _tmp)
 
 class _tmp:
   def __repr__(me):
-    return '%s(%s, %r)' % \
-        (_clsname(me), repr(me._guts()), me.writeflags(me.flags))
+    return '%s(%s, %r)' % (_clsname(me),
+                           _repr_secret(me._guts(),
+                                        not (me.flags & KF_NONSECRET)),
+                           me.writeflags(me.flags))
   def _repr_pretty_(me, pp, cyclep):
     ind = _pp_bgroup_tyname(pp, me)
     if cyclep:
       pp.text('...')
     else:
-      pp.pretty(me.guts()); pp.text(','); pp.breakable()
+      _pp_secret(pp, me._guts(), not (me.flags & KF_NONSECRET))
+      pp.text(','); pp.breakable()
       pp.pretty(me.writeflags(me.flags))
     pp.end_group(ind, ')')
 _augment(KeyData, _tmp)