Browse Source

Re-implement do_copy

oz123 10 years ago
parent
commit
990f2f2481
3 changed files with 43 additions and 46 deletions
  1. 0 35
      pwman/ui/base.py
  2. 41 9
      pwman/ui/baseui.py
  3. 2 2
      scripts/pwman3

+ 0 - 35
pwman/ui/base.py

@@ -167,41 +167,6 @@ class BaseCommands(HelpUI):
     the class code really long and unclear.
     """
 
-    def do_copy(self, args):
-        if self.hasxsel:
-            ids = self.get_ids(args)
-            if len(ids) > 1:
-                print ("Can copy only 1 password at a time...")
-                return None
-            try:
-                node = self._db.getnodes(ids)
-                tools.text_to_clipboards(node[0].password)
-                print("copied password for {}@{} clipboard".format(
-                      node[0].username, node[0].url))
-
-                print("erasing in 10 sec...")
-                time.sleep(10)
-                tools.text_to_clipboards("")
-            except Exception as e:
-                self.error(e)
-        else:
-            print ("Can't copy to clipboard, no xsel found in the system!")
-
-   # def do_open(self, args):
-   #     ids = self.get_ids(args)
-   #     if not args:
-   #         self.help_open()
-   #         return
-   #     if len(ids) > 1:
-   #         print ("Can open only 1 link at a time ...")
-   #         return None
-   #     try:
-   #         node = self._db.getnodes(ids)
-   #         url = node[0].url
-   #         tools.open_url(url)
-   #     except Exception as e:
-   #         self.error(e)
-
     def do_edit(self, arg, menu=None):
         ids = self.get_ids(arg)
         for i in ids:

+ 41 - 9
pwman/ui/baseui.py

@@ -17,27 +17,55 @@
 # Copyright (C) 2013, 2014 Oz Nahum Tiram <nahumoz@gmail.com>
 # ============================================================================
 from __future__ import print_function
-from pwman.util.crypto_engine import CryptoEngine
 import sys
 import os
-from pwman.ui import tools
-from colorama import Fore
-from pwman.data.nodes import Node
 import getpass
 import ast
 import csv
+import time
+from colorama import Fore
+from pwman.data.nodes import Node
+from pwman.ui import tools
+from pwman.util.crypto_engine import CryptoEngine
+from .base import HelpUI
 
 if sys.version_info.major > 2:
     raw_input = input
 
-from .base import HelpUI
-
 
 class BaseCommands(HelpUI):
 
+    @property
+    def _xsel(self):
+        if self.hasxsel:
+            return True
+
+    def error(self, exception):
+        if (isinstance(exception, KeyboardInterrupt)):
+            print('')
+        else:
+            print("Error: {0} ".format(exception))
+
     def do_copy(self, args):  # pargma: no cover
         """copy item to clipboard"""
-        pass
+        if not self._xsel:
+            return
+        if not args.isdigit():
+            print("Copy accepts only IDs ...")
+
+        ids = args.split()
+        if len(ids) > 1:
+            print("Can copy only 1 password at a time...")
+            return
+
+        nodes = self._db.getnodes(ids)
+        for node in nodes:
+            ce = CryptoEngine.get()
+            password = ce.decrypt(node[2])
+            tools.text_to_clipboards(password)
+            print("erasing in 10 sec...")
+            time.sleep(10)  # TODO: this should be configurable!
+            tools.text_to_clipboards("")
 
     def do_open(self, args):  # pragma: no cover
         ids = self.get_ids(args)
@@ -186,7 +214,7 @@ class BaseCommands(HelpUI):
     def _get_input(self, prompt):
         print(prompt, end="")
         sys.stdout.flush()
-        return sys.stdin.readline()
+        return sys.stdin.readline().strip()
 
     def _get_secret(self):
         # TODO: enable old functionallity, with password generator.
@@ -205,4 +233,8 @@ class BaseCommands(HelpUI):
         node['tags'] = self._get_tags()
         node = Node(clear_text=True, **node)
         self._db.add_node(node)
-        return node
+        # The cmd module stops if and of do_* return something
+        # else than None ...
+        # This is bad for testing, so everything that is do_*
+        # should call _do_* method which is testable
+        # return node

+ 2 - 2
scripts/pwman3

@@ -34,7 +34,7 @@ if sys.version_info.major > 2:
 
 
 def main(args):
-    PwmanCliNew, OSX = get_ui_platform(sys.platform)
+    PwmanCli, OSX = get_ui_platform(sys.platform)
     xselpath, dbtype, config = get_conf_options(args, OSX)
     dbver = get_db_version(config, dbtype, args)
     CryptoEngine.get(dbver)
@@ -47,7 +47,7 @@ def main(args):
         importer.run()
         sys.exit(0)
 
-    cli = PwmanCliNew(db, xselpath, CLICallback, config)
+    cli = PwmanCli(db, xselpath, CLICallback, config)
 
     try:
         cli.cmdloop()