[PATCH 2/9] Add guess_username function to users.py

Martin Sivak msivak at redhat.com
Tue Mar 12 14:09:17 UTC 2013


---
 pyanaconda/iutil.py | 14 ++++++++++++++
 pyanaconda/users.py | 17 +++++++++++++++++
 2 files changed, 31 insertions(+)

diff --git a/pyanaconda/iutil.py b/pyanaconda/iutil.py
index 05e7753..30dbb84 100644
--- a/pyanaconda/iutil.py
+++ b/pyanaconda/iutil.py
@@ -29,6 +29,7 @@ import errno
 import subprocess
 import threading
 import re
+import unicodedata
 
 from flags import flags
 from constants import *
@@ -480,3 +481,16 @@ class ProxyString(object):
     def __str__(self):
         return self.url
 
+def strip_accents(s):
+    """This function takes arbitrary unicode string
+    and returns it with all the diacritics removed.
+
+    :param s: arbitrary string
+    :type s: unicode
+
+    :return: s with diacritics removed
+    :rtype: unicode
+
+    """
+    return ''.join((c for c in unicodedata.normalize('NFD', s)
+                      if unicodedata.category(c) != 'Mn'))
diff --git a/pyanaconda/users.py b/pyanaconda/users.py
index f4d92d4..f102a29 100644
--- a/pyanaconda/users.py
+++ b/pyanaconda/users.py
@@ -29,6 +29,7 @@ import os.path
 import iutil
 import pwquality
 from pyanaconda.constants import ROOT_PATH
+from pyanaconda.iutil import strip_accents
 
 import logging
 log = logging.getLogger("anaconda")
@@ -149,6 +150,22 @@ def validatePassword(pw, confirm, minlen=6):
 
     return None
 
+def guess_username(fullname):
+    fullname = fullname.split()
+
+    # use last name word (at the end in most of the western countries..)
+    if len(fullname) > 0:
+        username = fullname[-1].decode("utf-8").lower()
+    else:
+        username = u""
+
+    # and prefix it with the first name inital
+    if len(fullname) > 1:
+        username = fullname[0][0].decode("utf-8").lower() + username
+
+    username = strip_accents(username).encode("utf-8")
+    return username
+
 class Users:
     def __init__ (self):
         self.admin = libuser.admin()
-- 
1.7.11.7



More information about the anaconda-patches mailing list