浏览代码

Nodes: drop dump_to_db method

This method was very similar to dump_edit_to_db. Thanks
to deactivating of the init method we could now discard that
method too.
oz123 11 年之前
父节点
当前提交
42253db921
共有 5 个文件被更改,包括 54 次插入36 次删除
  1. 9 3
      pwman/data/drivers/sqlite.py
  2. 4 15
      pwman/data/nodes.py
  3. 30 13
      pwman/tests/db_tests.py
  4. 10 5
      pwman/ui/ocli.py
  5. 1 0
      pwman/ui/tools.py

+ 9 - 3
pwman/data/drivers/sqlite.py

@@ -134,8 +134,14 @@ class SQLiteDatabaseNewForm(Database):
                 row = self._cur.fetchone()
                 if row is not None:
                     nodestring = str(row[0])
-                    nodeargs, tags = self.parse_node_string(nodestring)
-                    node = NewNode(**nodeargs)
+                    args, tags = self.parse_node_string(nodestring)
+
+                    #node = NewNode(**nodeargs)
+                    node = NewNode()
+                    node._password = args['password']
+                    node._username = args['username']
+                    node._url = args['url']
+                    node._notes = args['notes']
                     node.tags = tags
                     node._id = i
                     nodes.append(node)
@@ -158,7 +164,7 @@ class SQLiteDatabaseNewForm(Database):
         """
         for n in nodes:
             sql = "INSERT INTO NODES(DATA) VALUES(?)"
-            value = n.dump_to_db()
+            value = n.dump_edit_to_db()
             try:
                 self._cur.execute(sql, value)
             except sqlite.DatabaseError, e:  # pragma: no cover

+ 4 - 15
pwman/data/nodes.py

@@ -57,22 +57,11 @@ class NewNode(object):
         dump += "tags:"
         tagsloc = ""
         for tag in self._tags:
-            tagsloc += "tag:"+tag.strip()+"**endtag**"
-        dump += tagsloc
-        dump = [dump]
-        return dump
+            if isinstance(tag, str):
+                tagsloc += "tag:"+tag.strip()+"**endtag**"
+            else:
+                tagsloc += "tag:"+tag._name+"**endtag**"
 
-    def dump_to_db(self):
-        enc = CryptoEngine.get()
-        dump = ""
-        dump += "username:"+enc.encrypt(self._username)+"##"
-        dump += "password:"+enc.encrypt(self._password)+"##"
-        dump += "url:"+enc.encrypt(self._url)+"##"
-        dump += "notes:"+enc.encrypt(self._notes)+"##"
-        dump += "tags:"
-        tagsloc = ""
-        for tag in self._tags:
-            tagsloc += "tag:"+tag._name+"**endtag**"
         dump += tagsloc
         dump = [dump]
         return dump

+ 30 - 13
pwman/tests/db_tests.py

@@ -64,7 +64,7 @@ else:
 import pwman.util.config as config
 import pwman.data.factory
 from pwman.data.nodes import NewNode
-from pwman.data.tags import Tag, TagNew
+from pwman.data.tags import TagNew
 from pwman.util.crypto import CryptoEngine, CryptoBadKeyException
 from pwman import which, default_config
 from pwman.ui.cli import get_pass_conf
@@ -136,8 +136,14 @@ class DBTests(unittest.TestCase):
         password = 'Password'
         url = 'example.org'
         notes = 'some notes'
-        node = NewNode(username, password, url, notes)
-        tags = [Tag(tn) for tn in ['testing1', 'testing2']]
+        #node = NewNode(username, password, url, notes)
+        node = NewNode()
+        node.username = username
+        node.password = password
+        node.url = url
+        node.notes = notes
+        #node = NewNode(username, password, url, notes)
+        tags = [TagNew(tn) for tn in ['testing1', 'testing2']]
         node.tags = tags
         self.db.open()
         self.db.addnodes([node])
@@ -181,12 +187,12 @@ class DBTests(unittest.TestCase):
         node = self.tester.cli._db.getnodes([1])
         self.tester.cli._db.removenodes(node)
         # create the removed node again
-        username = 'tester'
-        password = 'Password'
-        url = 'example.org'
-        notes = 'some notes'
-        node = NewNode(username, password, url, notes)
-        tags = [Tag(tn) for tn in ['testing1', 'testing2']]
+        node = NewNode()
+        node.username = 'tester'
+        node.password = 'Password'
+        node.url = 'example.org'
+        node.notes = 'some notes'
+        tags = [TagNew(tn) for tn in ['testing1', 'testing2']]
         node.tags = tags
         self.db.open()
         self.db.addnodes([node])
@@ -272,8 +278,16 @@ class CLITests(unittest.TestCase):
     # the node is still not added !
 
     def test_add_new_entry(self):
-        node = NewNode('alice', 'dough!', 'example.com',
-                       'lorem impsum')
+        #node = NewNode('alice', 'dough!', 'example.com',
+        #               'lorem impsum')
+
+        node = NewNode()
+        node.username = 'alice'
+        node.password = 'dough!'
+        node.url = 'example.com'
+        node.notes = 'somenotes'
+        node.tags = 'lorem ipsum'
+
         tags = self.tester.cli.get_tags(reader=lambda: u'looking glass')
         node.tags = tags
         self.tester.cli._db.addnodes([node])
@@ -320,10 +334,13 @@ class CLITests(unittest.TestCase):
                              node.tags))
 
         import StringIO
-        s = StringIO.StringIO("4\nX")
-        sys.stdin = s
+        dummy_stdin = StringIO.StringIO('4\n\nX')
+        self.assertTrue(len(dummy_stdin.readlines()))
+        dummy_stdin.seek(0)
+        sys.stdin = dummy_stdin
         menu.run(node)
         self.tester.cli._db.editnode(2, node)
+        sys.stdin = sys.__stdin__
 
     def test_get_pass_conf(self):
         numerics, leet, s_chars = get_pass_conf()

+ 10 - 5
pwman/ui/ocli.py

@@ -47,7 +47,7 @@ from pwman.ui.tools import CLICallback
 from pwman.data.nodes import NewNode
 from pwman.ui.tools import CMDLoop
 import getpass
-from pwman.data.tags import TagNew as TagN
+from pwman.data.tags import TagNew
 
 try:
     import readline
@@ -180,7 +180,7 @@ class PwmanCliOld(cmd.Cmd, HelpUI, BaseUI):
         tagstrings = taglist.split()
         tags = []
         for tn in tagstrings:
-            tags.append(Tag(tn))
+            tags.append(TagNew(tn))
         return tags
 
     def print_node(self, node):
@@ -730,7 +730,7 @@ class BaseCommands(PwmanCliOld):
         taglist = tools.getinput("Tags: ", defaultstr, completer=complete,
                                  reader=reader)
         tagstrings = taglist.split()
-        tags = [TagN(tn) for tn in tagstrings]
+        tags = [TagNew(tn) for tn in tagstrings]
 
         return tags
 
@@ -787,7 +787,7 @@ class BaseCommands(PwmanCliOld):
     def do_filter(self, args):
         tagstrings = args.split()
         try:
-            tags = [TagN(ts) for ts in tagstrings]
+            tags = [TagNew(ts) for ts in tagstrings]
             self._db.filter(tags)
             tags = self._db.currenttags()
             print ("Current tags: ",)
@@ -827,7 +827,12 @@ class BaseCommands(PwmanCliOld):
                                              special_signs=s_chars)
             url = self.get_url()
             notes = self.get_notes()
-            node = NewNode(username, password, url, notes)
+            node = NewNode()
+            node.username = username
+            node.password = password
+            node.url = url
+            node.notes = notes
+            #node = NewNode(username, password, url, notes)
             node.tags = self.get_tags()
             self._db.addnodes([node])
             print ("Password ID: %d" % (node._id))

+ 1 - 0
pwman/ui/tools.py

@@ -339,6 +339,7 @@ class CMDLoop(CliMenu):  # pragma: no cover
                     self.items[2].getter = new_node.url
                     self.items[2].setter = new_node.url
                 elif selection == 3:  # for notes
+                    # new_node.notes = getinput("Notes:")
                     new_node.notes = getinput("Notes:")
                     self.items[3].getter = new_node.notes
                     self.items[3].setter = new_node.notes