2015-07-22 18:57:53 +00:00
|
|
|
# Copyright 2015 Canonical, Ltd.
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
""" Re-usable input widgets
|
|
|
|
"""
|
|
|
|
|
|
|
|
from urwid import (Edit, IntEdit, RadioButton, WidgetWrap)
|
|
|
|
import logging
|
Implement username field protection
man 8 useradd says:
It is usually recommended to only use usernames that begin with a
lower case letter or an underscore, followed by lower case
letters, digits, underscores, or dashes. They can end with a
dollar sign. In regular expression terms: [a-z_][a-z0-9_-]*[$]?
On Debian, the only constraints are that usernames must neither
start with a dash ('-') nor plus ('+') nor tilde ('~') nor
contain a colon (':'), a comma (','), or a whitespace (space: ' ',
end of line: '\n', tabulation: '\t', etc.). Note that using a
slash ('/') may break the default algorithm for the definition
of the user's home directory.
Usernames may only be up to 32 characters long.
In this patch we implement most of this. Subset of the regular
expression suggested above is used to limit input into the username
field. We've not yet determined how to provide a fixed width widget
so at this time, user can input more than 32 characters, but upon
selectin done, we raise and error and reset the state.
Signed-off-by: Ryan Harper <ryan.harper@canonical.com>
2015-10-23 20:21:13 +00:00
|
|
|
import re
|
2015-07-22 18:57:53 +00:00
|
|
|
|
2016-06-30 18:17:01 +00:00
|
|
|
log = logging.getLogger("subiquitycore.ui.input")
|
2015-07-22 18:57:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
class StringEditor(WidgetWrap):
|
|
|
|
""" Edit input class
|
|
|
|
|
|
|
|
Initializes and Edit object and attachs its result
|
|
|
|
to the `value` accessor.
|
|
|
|
"""
|
|
|
|
def __init__(self, caption, **kwargs):
|
|
|
|
self._edit = Edit(caption=caption, **kwargs)
|
2015-08-28 19:50:17 +00:00
|
|
|
self.error = None
|
2015-07-22 18:57:53 +00:00
|
|
|
super().__init__(self._edit)
|
|
|
|
|
2015-08-28 19:50:17 +00:00
|
|
|
def keypress(self, size, key):
|
|
|
|
if self.error:
|
|
|
|
self._edit.set_edit_text("")
|
|
|
|
self.error = None
|
|
|
|
return super().keypress(size, key)
|
|
|
|
|
|
|
|
def set_error(self, msg):
|
|
|
|
self.error = msg
|
|
|
|
return self._edit.set_edit_text(msg)
|
|
|
|
|
2015-07-22 18:57:53 +00:00
|
|
|
@property
|
|
|
|
def value(self):
|
|
|
|
return self._edit.get_edit_text()
|
|
|
|
|
2015-08-21 18:49:45 +00:00
|
|
|
@value.setter # NOQA
|
|
|
|
def value(self, value):
|
|
|
|
self._edit.set_edit_text(value)
|
2015-08-27 14:28:48 +00:00
|
|
|
|
2015-07-22 18:57:53 +00:00
|
|
|
|
2015-08-18 18:34:59 +00:00
|
|
|
class PasswordEditor(StringEditor):
|
|
|
|
""" Password input prompt with masking
|
|
|
|
"""
|
|
|
|
def __init__(self, caption, mask="*"):
|
|
|
|
super().__init__(caption, mask=mask)
|
|
|
|
|
|
|
|
|
2015-11-03 20:00:05 +00:00
|
|
|
class RealnameEditor(StringEditor):
|
|
|
|
""" Username input prompt with input rules
|
|
|
|
"""
|
|
|
|
|
|
|
|
def keypress(self, size, key):
|
|
|
|
''' restrict what chars we allow for username '''
|
|
|
|
|
|
|
|
realname = r'[a-zA-Z0-9_\- ]'
|
|
|
|
if re.match(realname, key) is None:
|
|
|
|
return False
|
|
|
|
|
|
|
|
return super().keypress(size, key)
|
|
|
|
|
|
|
|
|
2016-07-27 10:15:22 +00:00
|
|
|
class EmailEditor(StringEditor):
|
|
|
|
""" Email input prompt with input rules
|
|
|
|
"""
|
|
|
|
|
|
|
|
def keypress(self, size, key):
|
|
|
|
''' restrict what chars we allow for username '''
|
|
|
|
|
|
|
|
realname = r'[a-zA-Z0-9_-.@]'
|
|
|
|
if re.match(realname, key) is None:
|
|
|
|
return False
|
|
|
|
|
|
|
|
return super().keypress(size, key)
|
|
|
|
|
|
|
|
|
Implement username field protection
man 8 useradd says:
It is usually recommended to only use usernames that begin with a
lower case letter or an underscore, followed by lower case
letters, digits, underscores, or dashes. They can end with a
dollar sign. In regular expression terms: [a-z_][a-z0-9_-]*[$]?
On Debian, the only constraints are that usernames must neither
start with a dash ('-') nor plus ('+') nor tilde ('~') nor
contain a colon (':'), a comma (','), or a whitespace (space: ' ',
end of line: '\n', tabulation: '\t', etc.). Note that using a
slash ('/') may break the default algorithm for the definition
of the user's home directory.
Usernames may only be up to 32 characters long.
In this patch we implement most of this. Subset of the regular
expression suggested above is used to limit input into the username
field. We've not yet determined how to provide a fixed width widget
so at this time, user can input more than 32 characters, but upon
selectin done, we raise and error and reset the state.
Signed-off-by: Ryan Harper <ryan.harper@canonical.com>
2015-10-23 20:21:13 +00:00
|
|
|
class UsernameEditor(StringEditor):
|
|
|
|
""" Username input prompt with input rules
|
|
|
|
"""
|
|
|
|
|
|
|
|
def keypress(self, size, key):
|
|
|
|
''' restrict what chars we allow for username '''
|
|
|
|
|
|
|
|
userlen = len(self.value)
|
|
|
|
if userlen == 0:
|
|
|
|
username = r'[a-z_]'
|
|
|
|
else:
|
|
|
|
username = r'[a-z0-9_-]'
|
|
|
|
|
|
|
|
# don't allow non username chars
|
|
|
|
if re.match(username, key) is None:
|
|
|
|
return False
|
|
|
|
|
|
|
|
return super().keypress(size, key)
|
2015-11-02 18:55:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
class MountEditor(StringEditor):
|
|
|
|
""" Mountpoint input prompt with input rules
|
|
|
|
"""
|
|
|
|
|
|
|
|
def keypress(self, size, key):
|
|
|
|
''' restrict what chars we allow for mountpoints '''
|
|
|
|
|
|
|
|
mountpoint = r'[a-zA-Z0-9_/\.\-]'
|
|
|
|
if re.match(mountpoint, key) is None:
|
|
|
|
return False
|
|
|
|
|
|
|
|
return super().keypress(size, key)
|
Implement username field protection
man 8 useradd says:
It is usually recommended to only use usernames that begin with a
lower case letter or an underscore, followed by lower case
letters, digits, underscores, or dashes. They can end with a
dollar sign. In regular expression terms: [a-z_][a-z0-9_-]*[$]?
On Debian, the only constraints are that usernames must neither
start with a dash ('-') nor plus ('+') nor tilde ('~') nor
contain a colon (':'), a comma (','), or a whitespace (space: ' ',
end of line: '\n', tabulation: '\t', etc.). Note that using a
slash ('/') may break the default algorithm for the definition
of the user's home directory.
Usernames may only be up to 32 characters long.
In this patch we implement most of this. Subset of the regular
expression suggested above is used to limit input into the username
field. We've not yet determined how to provide a fixed width widget
so at this time, user can input more than 32 characters, but upon
selectin done, we raise and error and reset the state.
Signed-off-by: Ryan Harper <ryan.harper@canonical.com>
2015-10-23 20:21:13 +00:00
|
|
|
|
|
|
|
|
2015-07-22 18:57:53 +00:00
|
|
|
class IntegerEditor(WidgetWrap):
|
|
|
|
""" IntEdit input class
|
|
|
|
"""
|
|
|
|
def __init__(self, caption, default=0):
|
|
|
|
self._edit = IntEdit(caption=caption, default=default)
|
|
|
|
super().__init__(self._edit)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def value(self):
|
|
|
|
return self._edit.get_edit_text()
|
|
|
|
|
|
|
|
|
|
|
|
class Selector(WidgetWrap):
|
|
|
|
""" Radio selection list of options
|
|
|
|
"""
|
2015-07-22 20:18:18 +00:00
|
|
|
def __init__(self, opts):
|
2015-07-22 18:57:53 +00:00
|
|
|
"""
|
|
|
|
:param list opts: list of options to display
|
|
|
|
"""
|
|
|
|
self.opts = opts
|
2015-07-22 20:18:18 +00:00
|
|
|
self.group = []
|
2015-07-22 18:57:53 +00:00
|
|
|
self._add_options()
|
|
|
|
|
|
|
|
def _add_options(self):
|
|
|
|
for item in self.opts:
|
|
|
|
RadioButton(self.group, item)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def value(self):
|
|
|
|
for item in self.group:
|
|
|
|
log.debug(item)
|
|
|
|
if item.get_state():
|
|
|
|
return item.label
|
|
|
|
return "Unknown option"
|
2015-09-09 19:18:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
class YesNo(Selector):
|
|
|
|
""" Yes/No selector
|
|
|
|
"""
|
|
|
|
def __init__(self):
|
|
|
|
opts = ['Yes', 'No']
|
|
|
|
super().__init__(opts)
|