---
pyanaconda/ui/gui/spokes/user.py | 24 ++------------
pyanaconda/ui/tui/spokes/__init__.py | 23 ++++++++-----
pyanaconda/ui/tui/spokes/password.py | 2 +-
pyanaconda/ui/tui/spokes/user.py | 64 +++++++++++++++++++++++++++++++-----
4 files changed, 75 insertions(+), 38 deletions(-)
diff --git a/pyanaconda/ui/gui/spokes/user.py b/pyanaconda/ui/gui/spokes/user.py
index af39856..1154ed2 100644
--- a/pyanaconda/ui/gui/spokes/user.py
+++ b/pyanaconda/ui/gui/spokes/user.py
@@ -33,26 +33,12 @@ from pyanaconda.ui.gui import GUIObject
from pyanaconda.ui.gui.categories.user_settings import UserSettingsCategory
from pyanaconda.ui.common import FirstbootSpokeMixIn
from pyanaconda.ui.gui.utils import enlightbox
+from pyanaconda.ui.tui.spokes.user import strip_accents, guess_username
-import unicodedata
import pwquality
__all__ = ["UserSpoke", "AdvancedUserDialog"]
-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'))
-
class AdvancedUserDialog(GUIObject):
builderObjects = ["advancedUserDialog", "uid", "gid"]
mainWidgetName = "advancedUserDialog"
@@ -305,12 +291,8 @@ class UserSpoke(FirstbootSpokeMixIn, NormalSpoke):
It guesses the username and hostname, strips diacritics
and make those lowercase.
"""
-
- fullname = self.fullname.get_text().split()
- username = fullname[-1].decode("utf-8").lower()
- if len(fullname) > 1:
- username = fullname[0][0].decode("utf-8").lower() + username
- username = strip_accents(username).encode("utf-8")
+ fullname = self.fullname.get_text()
+ username = guess_username(fullname)
# after the text is updated in guesser, the guess has to be reenabled
if self.guesser[self.username]:
diff --git a/pyanaconda/ui/tui/spokes/__init__.py b/pyanaconda/ui/tui/spokes/__init__.py
index 6bac732..9e7289a 100644
--- a/pyanaconda/ui/tui/spokes/__init__.py
+++ b/pyanaconda/ui/tui/spokes/__init__.py
@@ -90,7 +90,7 @@ class EditTUIDialog(NormalTUISpoke):
self.value = None
return True
- def prompt(self, (title, value, regex)):
+ def prompt(self, (title, value, regex, cond)):
if regex == self.PASSWORD:
pw = self._app.raw_input(_("%s: ") % title, hidden=True)
confirm = self._app.raw_input(_("%s (confirm): ") % title,
hidden=True)
@@ -115,7 +115,7 @@ class EditTUIDialog(NormalTUISpoke):
else:
return _("Enter new value for '%s' and press enter\n") %
title
- def input(self, (title, value, regex), key):
+ def input(self, (title, value, regex, cond), key):
if regex.match(key):
self.value = key
self.close()
@@ -124,8 +124,8 @@ class EditTUIDialog(NormalTUISpoke):
return NormalTUISpoke.input(self, (title, value, regex), key)
class OneShotEditTUIDialog(EditTUIDialog):
- def prompt(self, (title, value, regex)):
- ret = EditTUIDialog.prompt(self, (title, value, regex))
+ def prompt(self, (title, value, regex, cond)):
+ ret = EditTUIDialog.prompt(self, (title, value, regex, cond))
if ret is None:
self.close()
return ret
@@ -137,7 +137,7 @@ class EditTUISpoke(NormalTUISpoke):
PASSWORD = EditTUIDialog.PASSWORD
CHECK = "check"
- # list of fields in the format (title, attribute, compiled_regexp)
+ # list of fields in the format (title, attribute, compiled_regexp, bool|func(self,
arg))
edit_fields = [
]
@@ -156,7 +156,7 @@ class EditTUISpoke(NormalTUISpoke):
for key in self.edit_data.split("."):
self.args = getattr(self.args, key)
- def _prep_text(i, (title, field, regexp)):
+ def _prep_text(i, (title, field, regexp, cond)):
number = tui.TextWidget("%2d)" % i)
title = tui.TextWidget(title)
value = self.args
@@ -166,7 +166,7 @@ class EditTUISpoke(NormalTUISpoke):
return tui.ColumnWidget([(3, [number]), (None, [title, value])], 1)
- def _prep_check(i, (title, field, regexp)):
+ def _prep_check(i, (title, field, regexp, cond)):
number = tui.TextWidget("%2d)" % i)
value = self.args
for key in field.split("."):
@@ -175,7 +175,7 @@ class EditTUISpoke(NormalTUISpoke):
return tui.ColumnWidget([(3, [number]), (None, [ch])], 1)
- def _prep_password(i, (title, field, regexp)):
+ def _prep_password(i, (title, field, regexp, cond)):
number = tui.TextWidget("%2d)" % i)
title = tui.TextWidget(title)
value = self.args
@@ -186,6 +186,11 @@ class EditTUISpoke(NormalTUISpoke):
return tui.ColumnWidget([(3, [number]), (None, [title, value])], 1)
for idx,field in enumerate(self.edit_fields):
+ if callable(field[3]) and not field[3](self, self.args):
+ continue
+ elif not callable(field[3]) and not field[3]:
+ continue
+
field_type = field[2]
if field_type == self.PASSWORD:
w = _prep_password(idx+1, field)
@@ -205,10 +210,12 @@ class EditTUISpoke(NormalTUISpoke):
if self.edit_fields[idx][2] == self.CHECK:
setattr(self.args, self.edit_fields[idx][1], not getattr(self.args,
self.edit_fields[idx][1]))
self.app.redraw()
+ self.apply()
else:
self.app.switch_screen_modal(self.dialog, self.edit_fields[idx])
if self.dialog.value is not None:
setattr(self.args, self.edit_fields[idx][1], self.dialog.value)
+ self.apply()
return True
except ValueError:
pass
diff --git a/pyanaconda/ui/tui/spokes/password.py b/pyanaconda/ui/tui/spokes/password.py
index b28bc3a..5e431c7 100644
--- a/pyanaconda/ui/tui/spokes/password.py
+++ b/pyanaconda/ui/tui/spokes/password.py
@@ -64,7 +64,7 @@ class PasswordSpoke(FirstbootSpokeMixIn, EditTUIDialog):
def prompt(self, args = None):
"""Overriden prompt as password typing is
special."""
- EditTUIDialog.prompt(self, (_("Password"), "",
EditTUIDialog.PASSWORD))
+ EditTUIDialog.prompt(self, (_("Password"), "",
EditTUIDialog.PASSWORD, True))
if self.value == None:
return
diff --git a/pyanaconda/ui/tui/spokes/user.py b/pyanaconda/ui/tui/spokes/user.py
index f4ee6f8..53e9af8 100644
--- a/pyanaconda/ui/tui/spokes/user.py
+++ b/pyanaconda/ui/tui/spokes/user.py
@@ -27,19 +27,55 @@ from pyanaconda.users import validatePassword
import re
import gettext
+import unicodedata
+
_ = lambda x: gettext.ldgettext("anaconda", x)
-__all__ = ["UserSpoke"]
+
+__all__ = ["UserSpoke", "strip_accents", "guess_username"]
+
+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'))
+
+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 UserSpoke(FirstbootSpokeMixIn, EditTUISpoke):
title = _("Create user")
category = "password"
edit_fields = [
- ("Username", "name", re.compile("^[a-z0-9_]+$")),
- ("Fullname", "gecos", re.compile("^[^:]*$")),
- ("Password", "password", EditTUISpoke.PASSWORD),
- ("Administrator", "_admin", EditTUISpoke.CHECK),
- ("Groups", "_groups", re.compile("^([a-z0-9_]+|
+)*$"))
+ ("Fullname", "gecos", re.compile("^[^:]*$"),
True),
+ ("Username", "name", re.compile("^[a-z0-9_]+$"),
True),
+ ("Use password", "_use_password", EditTUISpoke.CHECK, True),
+ ("Password", "password", EditTUISpoke.PASSWORD, lambda
self,args: args._use_password),
+ ("Administrator", "_admin", EditTUISpoke.CHECK, True),
+ ("Groups", "_groups", re.compile("^([a-z0-9_]+)?(,
?([a-z0-9_]+))*$"), True)
]
def __init__(self, app, data, storage, payload, instclass):
@@ -51,8 +87,12 @@ class UserSpoke(FirstbootSpokeMixIn, EditTUISpoke):
else:
self.args = self.data.UserData()
+ self.args._use_password = self.args.isCrypted or self.args.password
+
+ def refresh(self, args = None):
self.args._admin = "wheel" in self.args.groups
- self.args._groups = " ".join(self.args.groups)
+ self.args._groups = ", ".join(self.args.groups)
+ return EditTUISpoke.refresh(self, args)
@property
def completed(self):
@@ -67,4 +107,12 @@ class UserSpoke(FirstbootSpokeMixIn, EditTUISpoke):
return _("User %s will be created.") % self.args.name
def apply(self):
- pass
+ if self.args.gecos and not self.args.name:
+ self.args.name = guess_username(self.args.gecos)
+
+ self.args.groups = [g.strip() for g in self.args._groups.split(",") if
g]
+
+ if self.args._admin and "wheel" not in self.args.groups:
+ self.args.groups.append("wheel")
+ elif not self.args._admin and "wheel" in self.args.groups:
+ self.args.groups.remove("wheel")
--
1.7.11.7