catacomb/pwsafe.py: Abolish the `PWIter' class.
authorMark Wooding <mdw@distorted.org.uk>
Sun, 24 May 2015 17:06:03 +0000 (18:06 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Thu, 28 May 2015 10:21:01 +0000 (11:21 +0100)
There's no identifiable advantage to writing all of that out longhand
over a simple generator.  So do that instead.

catacomb/pwsafe.py

index 3af69d4..65aec71 100644 (file)
@@ -126,36 +126,6 @@ class PPK (Crypto):
 ###--------------------------------------------------------------------------
 ### Password storage.
 
-class PWIter (object):
-  """
-  I am an iterator over items in a password database.
-
-  I implement the usual Python iteration protocol.
-  """
-
-  def __init__(me, pw):
-    """
-    Initialize a PWIter object, to fetch items from PW.
-    """
-    me.pw = pw
-    me.k = me.pw.db.firstkey()
-
-  def next(me):
-    """
-    Return the next tag from the database.
-
-    Raises StopIteration if there are no more tags.
-    """
-    k = me.k
-    while True:
-      if k is None:
-        raise StopIteration
-      if k[0] == '$':
-        break
-      k = me.pw.db.nextkey(k)
-    me.k = me.pw.db.nextkey(k)
-    return me.pw.unpack(me.pw.db[k])[0]
-
 class PW (object):
   """
   I represent a secure (ish) password store.
@@ -324,6 +294,9 @@ class PW (object):
     """
     Iterate over the known password tags.
     """
-    return PWIter(me)
+    k = me.db.firstkey()
+    while k is not None:
+      if k[0] == '$': yield me.unpack(me.db[k])[0]
+      k = me.db.nextkey(k)
 
 ###----- That's all, folks --------------------------------------------------