Przeglądaj źródła

Use webbrowser from stdlib to open URLS

 This remove deviation in UI classes for Linux\Mac\Windows

 Simplifies code ...

 Issue #48
Oz N Tiram 8 lat temu
rodzic
commit
5cb7aa9aef
3 zmienionych plików z 6 dodań i 44 usunięć
  1. 0 18
      pwman/ui/mac.py
  2. 5 7
      pwman/ui/tools.py
  3. 1 19
      pwman/ui/win.py

+ 0 - 18
pwman/ui/mac.py

@@ -51,21 +51,3 @@ class PwmanCliMac(PwmanCli):
             print("erasing in {} sec...".format(flushtimeout))
             time.sleep(int(flushtimeout))
             tools.text_to_mcclipboard("")
-
-    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
-
-        ce = CryptoEngine.get()
-        nodes = self._db.getnodes(ids)
-
-        for node in nodes:
-            url = ce.decrypt(node[3])
-            if not url.startswith(("http://", "https://")):
-                url = "https://" + url
-            tools.open_url(url, macosx=True)

+ 5 - 7
pwman/ui/tools.py

@@ -120,17 +120,15 @@ def text_to_mcclipboard(text):  # pragma: no cover
         print (e, "\nExecuting pbcoy failed...")
 
 
-def open_url(link, macosx=False, ):  # pragma: no cover
+def open_url(link, macosx=False,):  # pragma: no cover
     """
     launch xdg-open or open in MacOSX with url
     """
-    uopen = "xdg-open "
-    if macosx:
-        uopen = "open "
+    import webbrowser
     try:
-        sp.call(uopen + link, shell=True, stdout=sp.PIPE, stderr=sp.PIPE)
-    except OSError as e:
-        print("Executing open_url failed with:\n", e)
+        webbrowser.open(link)
+    except webbrowser.Error as E:
+         print("Executing open_url failed with:\n", e)
 
 
 def get_terminal_size():  # pragma: no cover

+ 1 - 19
pwman/ui/win.py

@@ -67,7 +67,7 @@ def winGetClipboard():
 
 
 def winSetClipboard(text):
-    
+
     GMEM_DDESHARE = 0x2000
     ctypes.windll.user32.OpenClipboard(0)
     ctypes.windll.user32.EmptyClipboard()
@@ -119,23 +119,5 @@ class PwmanCliWin(PwmanCli):
             time.sleep(int(flushtimeout))
             winSetClipboard(b"")
 
-    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
-
-        ce = CryptoEngine.get()
-        nodes = self._db.getnodes(ids)
-
-        for node in nodes:
-            url = ce.decrypt(node[3])
-            if not url.startswith(("http://", "https://")):
-                url = "https://" + url
-            os.system("start "+url)
-
     def do_cls(self, args):
         os.system('cls')