Ver código fonte

add mechanism to find if xsel exists

oz123 12 anos atrás
pai
commit
8fe3b179df
2 arquivos alterados com 54 adições e 30 exclusões
  1. 28 22
      pwman/ui/cli.py
  2. 26 8
      scripts/pwman3

+ 28 - 22
pwman/ui/cli.py

@@ -509,21 +509,22 @@ class PwmanCli(cmd.Cmd):
         os.system('clear')
 
     def do_copy(self,args):
-        ids= self.get_ids(args)
-        if len(ids) > 1:
-            print "Can only 1 password at a time..."
-        try:
-            node = self._db.getnodes(ids)
-            node[0].get_password()
-            text_to_clipboards(node[0].get_password())
-            print """copied password for %s@%s clipboard... erasing in 10 sec...""" %\
-            (node[0].get_username(), node[0].get_url()) 
-            time.sleep(10)
-            text_to_clipboards("")
-
-        except Exception, e:
-            self.error(e)
-
+        if self.hasxsel:
+            ids= self.get_ids(args)
+            if len(ids) > 1:
+                print "Can only 1 password at a time..."
+            try:
+                node = self._db.getnodes(ids)
+                node[0].get_password()
+                text_to_clipboards(node[0].get_password())
+                print """copied password for %s@%s clipboard... erasing in 10 sec...""" %\
+                (node[0].get_username(), node[0].get_url()) 
+                time.sleep(10)
+                text_to_clipboards("")
+            except Exception, e:
+                self.error(e)
+        else:
+            print "Can't copy to clipboard, no xsel found in the system!"
 
     def do_cp(self,args):
         self.do_copy(args)
@@ -649,12 +650,12 @@ class PwmanCli(cmd.Cmd):
         except Exception, e:
             pass
     
-    def __init__(self, db):
+    def __init__(self, db, hasxsel):
         cmd.Cmd.__init__(self)
         self.intro = "%s %s (c) %s <%s>" % (pwman.appname, pwman.version,
                                             pwman.author, pwman.authoremail)
         self._historyfile = config.get_value("Readline", "history")
-
+        self.hasxsel = hasxsel
         try:
             enc = CryptoEngine.get()
             enc.set_callback(CLICallback())
@@ -774,11 +775,16 @@ def text_to_clipboards(text):
     https://pythonadventures.wordpress.com/tag/xclip/
     """
     # "primary":
-    xsel_proc = sp.Popen(['xsel', '-pi'], stdin=sp.PIPE)
-    xsel_proc.communicate(text)
-    # "clipboard":
-    xsel_proc = sp.Popen(['xsel', '-bi'], stdin=sp.PIPE)
-    xsel_proc.communicate(text) 
+    try:
+        xsel_proc = sp.Popen(['xsel', '-pi'], stdin=sp.PIPE)
+        xsel_proc.communicate(text)
+        # "clipboard":
+        xsel_proc = sp.Popen(['xsel', '-bi'], stdin=sp.PIPE)
+        xsel_proc.communicate(text) 
+    except OSError, e:
+        print e, "\nExecuting xsel failed, is it installed ?"
+        
+        
 
 class CliMenu(object):
     def __init__(self):

+ 26 - 8
scripts/pwman3

@@ -17,7 +17,6 @@
 #============================================================================
 # Copyright (C) 2006 Ivan Kelly <ivan@ivankelly.net>
 #============================================================================
-
 from pwman.util.crypto import CryptoEngine
 from pwman.ui.cli import PwmanCli
 import pwman.util.config as config
@@ -25,7 +24,7 @@ import pwman.data.factory
 import getopt,sys
 import os
 import os.path
-
+import subprocess as sp
 _saveconfig = True
 
 def print_help():
@@ -41,7 +40,21 @@ def print_help():
 
 Please report bugs at https://github.com/oz124/pwman3
 """ % (sys.argv[0])
-    
+
+
+def find_xsel():
+    """
+    look for xsel in the system
+    """
+    try:
+        print "inside"
+        xsel_proc = sp.Popen(['which', 'xsel'], stdin=sp.PIPE, stdout=sp.PIPE)
+        path, err = xsel_proc.communicate()
+        return path
+    except OSError, e:
+        print e, "\nExecuting xsel failed, is it installed ?"
+        return None
+
 try:
     config_dir = os.path.expanduser("~/.pwman")
     if not os.path.isdir(config_dir):
@@ -49,7 +62,7 @@ try:
     
     config_file = os.path.join(config_dir, "config")
     
-    default_config = {'Global': {'umask': '0100', 'colors' : 'yes', 'xclip': 'yes'},
+    default_config = {'Global': {'umask': '0100', 'colors' : 'yes'},
                       'Database':{'type':'SQLite',
                                   'filename':os.path.join(config_dir, "pwman.db")},
                       'Encryption':{'algorithm': 'Blowfish'},
@@ -67,10 +80,14 @@ try:
         if o[0] == '-h' or o[0] == "--help":
             print_help()
             sys.exit(0)
-
+    # if no config exists yet, look for xsel
     if os.path.exists(config_file):
         config.load(config_file)
-        
+        xselpath=config.get_value("Global","xselpath")
+    else:
+        xselpath=find_xsel()
+        set_value("Global", "xsel", xselpath)     
+
     for o in opts:
         if o[0] == '-d' or o[0] == "--database":
             config.set_value("Database", "filename",
@@ -79,7 +96,8 @@ try:
         if o[0] == '-e' or o[0] == "--encryption":
             config.set_value("Encryption", "algorithm", o[1])
             _saveconfig=False
-
+    
+        
     # set umask before creating/opening any files
     umask = int(config.get_value("Global", "umask"))
     os.umask(umask)
@@ -88,7 +106,7 @@ try:
 
     type = config.get_value("Database", "type")
     db = pwman.data.factory.create(type)
-    cli = PwmanCli(db)
+    cli = PwmanCli(db, xselpath)
 except SystemExit, e:
     sys.exit(e)
 except Exception, e: