Selaa lähdekoodia

update cli.get_password to use tools.getpassword

this completes the migrations to tools based input
oz123 11 vuotta sitten
vanhempi
commit
5a6605aaaf
2 muutettua tiedostoa jossa 41 lisäystä ja 24 poistoa
  1. 9 2
      pwman/ui/cli.py
  2. 32 22
      pwman/ui/tools.py

+ 9 - 2
pwman/ui/cli.py

@@ -47,6 +47,7 @@ from pwman.ui.tools import CliMenuItem
 from pwman.ui.tools import CLICallback
 from colorama import Fore
 from pwman.ui.base import HelpUI, BaseUI
+import getpass
 
 try:
     import readline
@@ -824,7 +825,7 @@ class BaseCommands(PwmanCliOld):
                     raise Exception(errmsg)
                 if not isinstance(args, dict):
                     raise Exception(errmsg)
-                password = self.get_password(1, **args)
+                password = self.get_password(argsgiven=1, **args)
             else:
                 numerics = config.get_value(
                     "Generator", "numerics").lower() == 'true'
@@ -833,7 +834,7 @@ class BaseCommands(PwmanCliOld):
                     "Generator", "leetify").lower() == 'true'
                 special_chars = config.get_value(
                     "Generator", "special_chars").lower() == 'true'
-                password = self.get_password(0,
+                password = self.get_password(argsgiven=0,
                                              numerics=numerics,
                                              symbols=leetify,
                                              special_signs=special_chars)
@@ -875,6 +876,12 @@ class BaseCommands(PwmanCliOld):
         except Exception, e:
             self.error(e)
 
+    def get_password(self, argsgiven, numerics=False, leetify=False,
+                     symbols=False, special_signs=False,
+                     reader=getpass.getpass):
+        return tools.getpassword("Password (Blank to generate): ",
+                                 reader=reader)
+
 
 class Aliases(BaseCommands, PwmanCliOld):
     """

+ 32 - 22
pwman/ui/tools.py

@@ -29,6 +29,7 @@ import struct
 import os
 import colorama
 from pwman.data.tags import TagNew as Tag
+import pwman.util.generator as generator
 
 if sys.platform != 'win32':
     import termios
@@ -138,27 +139,32 @@ def open_url(link, macosx=False):
         print ("Executing open_url failed with:\n", e)
 
 
-def getpassword(question, width=_defaultwidth, echo=False,
-                reader=getpass.getpass):
-    """
-    read password given by user
-    TODO: migrate all the fancy interface from ui.py to here.
-    This includes all the automatic generator password
-    policy etc.
-    """
-    if echo:
-        print(question.ljust(width),)
-        return sys.stdin.readline().rstrip()
-    else:
-        while True:
-            a1 = reader(question.ljust(width))
-            if not a1:
-                return a1
-            a2 = reader("[Repeat] %s" % (question.ljust(width)))
-            if a1 == a2:
-                return a1
-            else:
-                print ("Passwords don't match. Try again.")
+def getpassword(question, argsgiven=None,
+                width=_defaultwidth, echo=False,
+                reader=getpass.getpass, numerics=False, leetify=False,
+                symbols=False, special_signs=False):
+    # TODO: getpassword should recieve a config insatce
+    #       and generate the policy according to it,
+    #       so that getpassword in cli would be simplified
+    if argsgiven == 1:
+        length = getinput("Password length (default 7): ", default='7')
+        length = int(length)
+        password, dumpme = generator.generate_password(length, length,
+                                                       True, leetify,
+                                                       numerics,
+                                                       special_signs)
+        print ("New password: %s" % (password))
+        return password
+    # no args given
+    while True:
+        a1 = reader(question.ljust(width))
+        if not a1:
+            return getpassword('', argsgiven=1)
+        a2 = reader("[Repeat] %s" % (question.ljust(width)))
+        if a1 == a2:
+            return a1
+        else:
+            print ("Passwords don't match. Try again.")
 
 
 def gettermsize():
@@ -177,7 +183,11 @@ def getinput(question, default="", reader=raw_input,
     """
     if reader == raw_input:
         if not _readline_available:
-            return raw_input(question.ljust(width))
+            val = raw_input(question.ljust(width))
+            if val:
+                return val
+            else:
+                return default
         else:
             def defaulter():
                 """define default behavior startup"""