Просмотр исходного кода

Bug fixes and functionality testing in baseui

oz123 10 лет назад
Родитель
Сommit
b4ca4cf637
3 измененных файлов с 130 добавлено и 72 удалено
  1. 10 0
      pwman/tests/test_base_ui.py
  2. 66 71
      pwman/ui/base.py
  3. 54 1
      pwman/ui/baseui.py

+ 10 - 0
pwman/tests/test_base_ui.py

@@ -107,6 +107,16 @@ class TestBaseUI(unittest.TestCase):
         self.tester.cli.do_print('1')
         self.assertIn('\x1b[31mUsername:\x1b[0m alice', v.getvalue())
 
+    def test_get_ids(self):
+        # used by do_cp or do_open,
+        # this spits many time could not understand your input
+        self.assertEqual([1], self.tester.cli._get_ids('1'))
+        self.assertListEqual([1, 2, 3, 4, 5], self.tester.cli._get_ids('1-5'))
+        self.assertListEqual([], self.tester.cli._get_ids('5-1'))
+        self.assertListEqual([], self.tester.cli._get_ids('5x-1'))
+        self.assertListEqual([], self.tester.cli._get_ids('5x'))
+        self.assertListEqual([], self.tester.cli._get_ids('5\\'))
+
     def test_5_do_delete(self):
         self.assertIsNone(self.tester.cli._do_rm('x'))
         sys.stdin = StringIO("y\n")

+ 66 - 71
pwman/ui/base.py

@@ -22,13 +22,8 @@ Define the base CLI interface for pwman3
 """
 from __future__ import print_function
 from pwman.util.crypto_engine import zerome
-import re
 import sys
-import time
-import select as uselect
-from pwman.ui import tools
 from pwman.ui.tools import CliMenuItem
-from colorama import Fore
 from pwman.ui.tools import CMDLoop
 
 if sys.version_info.major > 2:  # pragma: no cover
@@ -199,48 +194,48 @@ class BaseCommands(object):
             except Exception as e:
                 self.error(e)
 
-    def print_node(self, node):
-        width = str(tools._defaultwidth)
-        print ("Node %d." % (node._id))
-        print (("%" + width + "s %s") % (tools.typeset("Username:", Fore.RED),
-                                         node.username))
-        print (("%" + width + "s %s") % (tools.typeset("Password:", Fore.RED),
-                                         node.password))
-        print (("%" + width + "s %s") % (tools.typeset("Url:", Fore.RED),
-                                         node.url))
-        print (("%" + width + "s %s") % (tools.typeset("Notes:", Fore.RED),
-                                         node.notes))
-        print (tools.typeset("Tags: ", Fore.RED)),
-        for t in node.tags:
-            print (" %s " % t)
-        print()
-
-        def heardEnter():
-            i, o, e = uselect.select([sys.stdin], [], [], 0.0001)
-            for s in i:
-                if s == sys.stdin:
-                    sys.stdin.readline()
-                    return True
-                return False
-
-        def waituntil_enter(somepredicate, timeout, period=0.25):
-            mustend = time.time() + timeout
-            while time.time() < mustend:
-                cond = somepredicate()
-                if cond:
-                    break
-                time.sleep(period)
-            self.do_cls('')
-
-        try:
-            flushtimeout = int(self.config.get_value("Global", "cls_timeout"))
-        except ValueError:
-            flushtimeout = 10
-
-        if flushtimeout > 0:
-            print ("Type Enter to flush screen (autoflash in "
-                   "%d sec.)" % flushtimeout)
-            waituntil_enter(heardEnter, flushtimeout)
+    #def print_node(self, node):
+    #    width = str(tools._defaultwidth)
+    #    print ("Node %d." % (node._id))
+    #    print (("%" + width + "s %s") % (tools.typeset("Username:", Fore.RED),
+    #                                     node.username))
+    #    print (("%" + width + "s %s") % (tools.typeset("Password:", Fore.RED),
+    #                                     node.password))
+    #    print (("%" + width + "s %s") % (tools.typeset("Url:", Fore.RED),
+    #                                     node.url))
+    #    print (("%" + width + "s %s") % (tools.typeset("Notes:", Fore.RED),
+    #                                     node.notes))
+    #    print (tools.typeset("Tags: ", Fore.RED)),
+    #    for t in node.tags:
+    #        print (" %s " % t)
+    #    print()
+
+    #   def heardEnter():
+    #        i, o, e = uselect.select([sys.stdin], [], [], 0.0001)
+    #        for s in i:
+    #            if s == sys.stdin:
+    #                sys.stdin.readline()
+    #                return True
+    #            return False
+
+    #    def waituntil_enter(somepredicate, timeout, period=0.25):
+    #        mustend = time.time() + timeout
+    #        while time.time() < mustend:
+    #            cond = somepredicate()
+    #            if cond:
+    #                break
+    #            time.sleep(period)
+    #        self.do_cls('')
+
+    #    try:
+    #        flushtimeout = int(self.config.get_value("Global", "cls_timeout"))
+    #    except ValueError:
+    #        flushtimeout = 10
+    #
+    #    if flushtimeout > 0:
+    #        print ("Type Enter to flush screen (autoflash in "
+    #               "%d sec.)" % flushtimeout)
+    #         waituntil_enter(heardEnter, flushtimeout)
 
     def do_passwd(self, args):
         raise Exception("Not Implemented ...")
@@ -279,30 +274,30 @@ class BaseCommands(object):
     #    except Exception as e:
     #        self.error(e)
 
-    def get_ids(self, args):
-        """
-        Command can get a single ID or
-        a range of IDs, with begin-end.
-        e.g. 1-3 , will get 1 to 3.
-        """
-        # TODO: add documentation and testing
-        ids = []
-        rex = re.compile("^(?P<begin>\d+)(?:-(?P<end>\d+))?$")
-        rex = rex.match(args)
-        if hasattr(rex, 'groupdict'):
-            try:
-                begin = int(rex.groupdict()['begin'])
-                end = int(rex.groupdict()['end'])
-                if not end > begin:
-                    print("Start node should be smaller than end node")
-                    return ids
-                ids += range(begin, end+1)
-                return ids
-            except TypeError:
-                ids.append(int(begin))
-        else:
-            print("Could not understand your input...")
-        return ids
+    #def get_ids(self, args):
+    #    """
+    #    Command can get a single ID or
+    #    a range of IDs, with begin-end.
+    #    e.g. 1-3 , will get 1 to 3.
+    #    """
+    #    # TODO: add documentation and testing
+    #    ids = []
+    #    rex = re.compile("^(?P<begin>\d+)(?:-(?P<end>\d+))?$")
+    #    rex = rex.match(args)
+    #    if hasattr(rex, 'groupdict'):
+    #        try:
+    #            begin = int(rex.groupdict()['begin'])
+    #            end = int(rex.groupdict()['end'])
+    #            if not end > begin:
+    #                print("Start node should be smaller than end node")
+    #                return ids
+    #            ids += range(begin, end+1)
+    #            return ids
+    #        except TypeError:
+    #            ids.append(int(begin))
+    #    else:
+    #        print("Could not understand your input...")
+    #    return ids
 
    # def get_password(self, argsgiven, numerics=False, leetify=False,
    #                  symbols=False, special_signs=False,

+ 54 - 1
pwman/ui/baseui.py

@@ -23,6 +23,8 @@ import getpass
 import ast
 import csv
 import time
+import re
+import select as uselect
 from colorama import Fore
 from pwman.data.nodes import Node
 from pwman.ui import tools
@@ -33,6 +35,24 @@ if sys.version_info.major > 2:  # pragma: no cover
     raw_input = input
 
 
+def _heard_enter():  # pragma: no cover
+    i, o, e = uselect.select([sys.stdin], [], [], 0.0001)
+    for s in i:
+        if s == sys.stdin:
+            sys.stdin.readline()
+            return True
+        return False
+
+
+def _wait_until_enter(predicate, timeout, period=0.25):  # pragma: no cover
+    mustend = time.time() + timeout
+    while time.time() < mustend:
+        cond = predicate()
+        if cond:
+            break
+        time.sleep(period)
+
+
 class BaseCommands(HelpUIMixin, AliasesMixin):
 
     @property
@@ -40,6 +60,30 @@ class BaseCommands(HelpUIMixin, AliasesMixin):
         if self.hasxsel:
             return True
 
+    def _get_ids(self, args):
+        """
+        Command can get a single ID or
+        a range of IDs, with begin-end.
+        e.g. 1-3 , will get 1 to 3.
+        """
+        ids = []
+        rex = re.compile("^(?P<begin>\d+)(?:-(?P<end>\d+))?$")
+        rex = rex.match(args)
+        if hasattr(rex, 'groupdict'):
+            try:
+                begin = int(rex.groupdict()['begin'])
+                end = int(rex.groupdict()['end'])
+                if not end > begin:
+                    print("Start node should be smaller than end node")
+                    return ids
+                ids += range(begin, end+1)
+                return ids
+            except TypeError:
+                ids.append(int(begin))
+        else:
+            print("Could not understand your input...")
+        return ids
+
     def error(self, exception):  # pragma: no cover
         if (isinstance(exception, KeyboardInterrupt)):
             print('')
@@ -69,7 +113,7 @@ class BaseCommands(HelpUIMixin, AliasesMixin):
             tools.text_to_clipboards("")
 
     def do_open(self, args):  # pragma: no cover
-        ids = self.get_ids(args)
+        ids = self._get_ids(args)
         if not args:
             self.help_open()
             return
@@ -252,8 +296,17 @@ class BaseCommands(HelpUIMixin, AliasesMixin):
             return
         nodes = self._db.getnodes([args])
         node = self._db_entries_to_nodes(nodes)[0]
+
         print(node)
 
+        flushtimeout = self.config.get_value('Global', 'cls_timeout')
+        flushtimeout = flushtimeout or 10
+
+        print("Type Enter to flush screen or wait %s sec. " % flushtimeout)
+
+        _wait_until_enter(_heard_enter, float(flushtimeout))
+        self.do_cls('')
+
     def _do_rm(self, args):
         for i in args.split():
             if not i.isdigit():