key.c, catacomb/__init__.py: Split key file I/O into Python and C pieces.
[catacomb-python] / catacomb / __init__.py
index 9e4d47f..bd8aa57 100644 (file)
@@ -180,10 +180,10 @@ class _tmp:
   def fromhex(x):
     return ByteString(_unhexify(x))
   fromhex = staticmethod(fromhex)
-  def __hex__(me):
-    return _hexify(me)
+  def hex(me): return _hexify(me)
+  __hex__ = hex
   def __repr__(me):
-    return 'bytes(%r)' % hex(me)
+    return 'bytes(%r)' % me.hex()
 _augment(ByteString, _tmp)
 ByteString.__hash__ = str.__hash__
 bytes = ByteString.fromhex
@@ -192,7 +192,7 @@ bytes = ByteString.fromhex
 ### Symmetric encryption.
 
 class _tmp:
-  def encrypt(me, n, m, tsz = None, h = ByteString('')):
+  def encrypt(me, n, m, tsz = None, h = ByteString.zero(0)):
     if tsz is None: tsz = me.__class__.tagsz.default
     e = me.enc(n, len(h), len(m), tsz)
     if not len(h): a = None
@@ -200,7 +200,7 @@ class _tmp:
     c0 = e.encrypt(m)
     c1, t = e.done(aad = a)
     return c0 + c1, t
-  def decrypt(me, n, c, t, h = ByteString('')):
+  def decrypt(me, n, c, t, h = ByteString.zero(0)):
     d = me.dec(n, len(h), len(c), len(t))
     if not len(h): a = None
     else: a = d.aad().hash(h)
@@ -387,12 +387,15 @@ class BaseRat (object):
     return type(me)(me._d*n, me._n*d)
   __div__ = __truediv__
   __rdiv__ = __rtruediv__
-  def __cmp__(me, you):
+  def _order(me, you, op):
     n, d = _split_rat(you)
-    return cmp(me._n*d, n*me._d)
-  def __rcmp__(me, you):
-    n, d = _split_rat(you)
-    return cmp(n*me._d, me._n*d)
+    return op(me._n*d, n*me._d)
+  def __eq__(me, you): return me._order(you, lambda x, y: x == y)
+  def __ne__(me, you): return me._order(you, lambda x, y: x != y)
+  def __le__(me, you): return me._order(you, lambda x, y: x <= y)
+  def __lt__(me, you): return me._order(you, lambda x, y: x <  y)
+  def __gt__(me, you): return me._order(you, lambda x, y: x >  y)
+  def __ge__(me, you): return me._order(you, lambda x, y: x >= y)
 
 class IntRat (BaseRat):
   RING = MP
@@ -652,10 +655,23 @@ _augment(KeySZSet, _tmp)
 ### Key data objects.
 
 class _tmp:
+  def merge(me, file, report = None):
+    """KF.merge(FILE, [report = <built-in-reporter>])"""
+    name = file.name
+    lno = 1
+    for line in file:
+      me.mergeline(name, lno, line, report)
+      lno += 1
+    return me
   def __repr__(me): return '%s(%r)' % (_clsname(me), me.name)
 _augment(KeyFile, _tmp)
 
 class _tmp:
+  def extract(me, file, filter = ''):
+    """KEY.extract(FILE, [filter = <any>])"""
+    line = me.extractline(filter)
+    file.write(line)
+    return me
   def __repr__(me): return '%s(%r)' % (_clsname(me), me.fulltag)
 _augment(Key, _tmp)