File e530f36e1d968b4d5d5a9a221e67d4ea069011c5.patch of Package loxodo

From e530f36e1d968b4d5d5a9a221e67d4ea069011c5 Mon Sep 17 00:00:00 2001
From: Jason Ansel <jasonansel@jasonansel.com>
Date: Fri, 14 Mar 2014 19:22:23 -0700
Subject: [PATCH] Option for generating pronounceable passwords with pwgen

---
 src/config.py                   |  6 ++++++
 src/frontends/wx/recordframe.py | 19 +++++++++++++++++--
 src/frontends/wx/settings.py    | 12 ++++++++++++
 3 files changed, 35 insertions(+), 2 deletions(-)

diff --git a/src/config.py b/src/config.py
index a7cf60f..7577685 100755
--- a/src/config.py
+++ b/src/config.py
@@ -36,6 +36,7 @@ def __init__(self):
         self.reduction = False
         self.search_notes = False
         self.search_passwd = False
+        self.use_pwgen = False
         self.alphabet = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_"
 
         self._fname = self.get_config_filename()
@@ -70,6 +71,10 @@ def __init__(self):
             if self._parser.get("base", "search_passwd") == "True":
                 self.search_passwd = True
 
+        if self._parser.has_option("base", "use_pwgen"):
+            if self._parser.get("base", "use_pwgen") == "True":
+                self.use_pwgen = True
+
         if not os.path.exists(self._fname):
             self.save()
 
@@ -97,6 +102,7 @@ def save(self):
         self._parser.set("base", "alphabetreduction", str(self.reduction))
         self._parser.set("base", "search_notes", str(self.search_notes))
         self._parser.set("base", "search_passwd", str(self.search_passwd))
+        self._parser.set("base", "use_pwgen", str(self.use_pwgen))
         filehandle = open(self._fname, 'w')
         self._parser.write(filehandle)
         filehandle.close()
diff --git a/src/frontends/wx/recordframe.py b/src/frontends/wx/recordframe.py
index 0739dbf..2b21292 100755
--- a/src/frontends/wx/recordframe.py
+++ b/src/frontends/wx/recordframe.py
@@ -21,6 +21,7 @@
 import platform
 import random
 import struct
+import subprocess
 import wx
 
 from .wxlocale import _
@@ -176,7 +177,7 @@ def _on_toggle_passwd_mask(self, dummy):
             self._tc_passwd.SetFocus()
 
     def _on_generate_passwd(self, dummy):
-        _pwd = self.generate_password(alphabet=config.alphabet,pwd_length=config.pwlength,allow_reduction=config.reduction)
+        _pwd = self.generate_password(alphabet=config.alphabet,pwd_length=config.pwlength,allow_reduction=config.reduction,use_pwgen=config.use_pwgen)
         self._tc_passwd.SetValue(_pwd)
 
     @staticmethod
@@ -190,7 +191,21 @@ def _urandom(count):
             return retval
 
     @staticmethod
-    def generate_password(alphabet="abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_", pwd_length=8, allow_reduction=False):
+    def generate_password(alphabet="abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_",
+                          pwd_length=10, allow_reduction=False, use_pwgen=False):
+        if use_pwgen:
+            # use pwgen program to generate pronounceable passwords
+            options = '-cn'
+            if allow_reduction:
+                options += 'B'
+            stdout, stderr = subprocess.Popen(
+                    ['pwgen', options, str(pwd_length), '1'],
+                    stdout=subprocess.PIPE).communicate()
+            pwd = stdout.strip()
+            assert len(pwd) == pwd_length
+            return pwd
+
+
         # remove some easy-to-mistake characters
         if allow_reduction:
             for _chr in "0OjlI1":
diff --git a/src/frontends/wx/settings.py b/src/frontends/wx/settings.py
index 8b9145f..2e64f9e 100644
--- a/src/frontends/wx/settings.py
+++ b/src/frontends/wx/settings.py
@@ -21,6 +21,7 @@
 import platform
 import random
 import struct
+import subprocess
 import wx
 
 from .wxlocale import _
@@ -50,6 +51,13 @@ def __init__(self, parent):
 
         _sz_main.Add(_sz_fields, 1, wx.EXPAND | wx.GROW)
 
+        try:
+            # Throws exception of pwgen is not installed
+            subprocess.Popen(['pwgen'], stdout=subprocess.PIPE).communicate()
+            self._cb_pwgen = self._add_a_checkbox(_sz_fields,_("Use pwgen to generate passwords") + ":")
+        except OSError:
+            self._cb_pwgen = None
+
         self._cb_reduction = self._add_a_checkbox(_sz_fields,_("Avoid easy to mistake chars") + ":")
 
         self._tc_alphabet = self._add_a_textcontrol(_sz_fields,_("Alphabet")+ ":",config.alphabet)
@@ -115,6 +123,8 @@ def update_fields(self):
         """
         Update fields from source
         """
+        if self._cb_pwgen:
+            self._cb_pwgen.SetValue(config.use_pwgen)
         self._sc_length.SetValue(config.pwlength)
         self._tc_alphabet.SetValue(config.alphabet)
         self._cb_reduction.SetValue(config.reduction)
@@ -125,6 +135,8 @@ def _apply_changes(self, dummy):
         """
         Update source from fields
         """
+        if self._cb_pwgen:
+            config.use_pwgen = self._cb_pwgen.GetValue()
         config.pwlength = self._sc_length.GetValue()
         config.reduction = self._cb_reduction.GetValue()
         config.search_notes = self._search_notes.GetValue()
-- 
1.9.3

openSUSE Build Service is sponsored by