Selaa lähdekoodia

Simplify edit menu

less code ... less bugs
oz123 10 vuotta sitten
vanhempi
commit
4b63a4f0bb
2 muutettua tiedostoa jossa 37 lisäystä ja 41 poistoa
  1. 9 6
      pwman/ui/baseui.py
  2. 28 35
      pwman/ui/tools.py

+ 9 - 6
pwman/ui/baseui.py

@@ -381,21 +381,20 @@ class BaseCommands(HelpUIMixin, AliasesMixin):
                 menu.add(CliMenuItem("Username",
                                      self._get_input,
                                      node.username,
-                                     node.username))
+                                     ))
                 menu.add(CliMenuItem("Password", self._get_secret,
                                      node.password,
-                                     node.password))
+                                     ))
                 menu.add(CliMenuItem("Url", self._get_input,
                                      node.url,
-                                     node.url))
+                                     ))
                 menunotes = CliMenuItem("Notes", self._get_input,
                                         node.notes,
-                                        node.notes)
+                                        )
                 menu.add(menunotes)
                 tgetter = lambda: ', '.join(t for t in node.tags)
                 menu.add(CliMenuItem("Tags", self._get_input,
-                                     tgetter(),
-                                     node.tags))
+                                     tgetter()))
             menu.run(node)
             self._db.editnode(i, **node.to_encdict())
             # when done with node erase it
@@ -450,6 +449,10 @@ class BaseCommands(HelpUIMixin, AliasesMixin):
             print("print accepts only a single ID ...")
             return
         nodes = self._db.getnodes([args])
+        if not nodes:
+            print("Node not found ...")
+            return
+
         node = self._db_entries_to_nodes(nodes)[0]
         print(node)
         flushtimeout = self.config.get_value('Global', 'cls_timeout')

+ 28 - 35
pwman/ui/tools.py

@@ -274,7 +274,30 @@ def _get_secret():
         p = get_or_create_pass()
     else:
         p = sys.stdin.readline().rstrip()
-        return p
+
+    return p
+
+
+def set_selection(new_node, items, selection, reader):
+    if selection == 0:
+        new_node.username = getinput("Username:")
+        items[0].getter = new_node.username
+    elif selection == 1:  # for password
+        new_node.password = _get_secret()
+        items[1].getter = new_node.password
+    elif selection == 2:
+        new_node.url = getinput("Url:")
+        items[2].getter = new_node.url
+    elif selection == 3:  # for notes
+        # new_node.notes = getinput("Notes:")
+        new_node.notes = reader("Notes:")
+        items[3].getter = new_node.notes
+    elif selection == 4:
+        taglist = getinput("Tags:")
+        tagstrings = taglist.split()
+        tags = [tn for tn in tagstrings]
+        new_node.tags = tags
+        items[4].getter = new_node.tags
 
 
 class CMDLoop(object):  # pragma: no cover
@@ -296,44 +319,15 @@ class CMDLoop(object):  # pragma: no cover
     def run(self, new_node=None, reader=raw_input):
         while True:
             for i, x in enumerate(self.items):
-                current = x.getter
-                print ("%s - %s: %s" % (i + 1, x.name, current))
+                print ("%s - %s: %s" % (i + 1, x.name, x.getter))
 
             print("X - Finish editing")
-            option = reader("Enter your choice:")[0]
+            option = reader("E)ter your choice:")[0]
             try:
                 print ("Selection, ", option)
                 # substract 1 because array subscripts start at 0
                 selection = int(option) - 1
-                # new value is created by calling the editor with the
-                # previous value as a parameter
-                # TODO: enable overriding password policy as if new node
-                # is created.
-                if selection == 0:
-                    new_node.username = getinput("Username:")
-                    self.items[0].getter = new_node.username
-                    self.items[0].setter = new_node.username
-                elif selection == 1:  # for password
-                    new_node.password = _get_secret()
-                    self.items[1].getter = new_node.password
-                    self.items[1].setter = new_node.password
-                elif selection == 2:
-                    new_node.url = getinput("Url:")
-                    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 = reader("Notes:")
-                    self.items[3].getter = new_node.notes
-                    self.items[3].setter = new_node.notes
-                elif selection == 4:
-                    taglist = getinput("Tags:")
-                    tagstrings = taglist.split()
-                    tags = [tn for tn in tagstrings]
-                    #tags = ''
-                    new_node.tags = tags
-                    self.items[4].setter = new_node.tags
-                    self.items[4].getter = new_node.tags
+                set_selection(new_node, self.items, selection, reader)
             except (ValueError, IndexError):
                 if (option.upper() == 'X'):
                     break
@@ -342,11 +336,10 @@ class CMDLoop(object):  # pragma: no cover
 
 class CliMenuItem(object):  # pragma: no cover
 
-    def __init__(self, name, editor, getter, setter):
+    def __init__(self, name, editor, getter):
         self.name = name
         self.editor = editor
         self.getter = getter
-        self.setter = setter
 
 
 class CLICallback(Callback):  # pragma: no cover