2008-08-05 02:39:50 -05:00
|
|
|
# Authors:
|
|
|
|
# Jason Gerard DeRose <jderose@redhat.com>
|
|
|
|
#
|
|
|
|
# Copyright (C) 2008 Red Hat
|
|
|
|
# see file 'COPYING' for use and warranty information
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU General Public License as
|
|
|
|
# published by the Free Software Foundation; version 2 only
|
|
|
|
#
|
|
|
|
# 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 General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
|
|
|
|
"""
|
|
|
|
Base classes for the public plugable.API instance, which the XML-RPC, CLI,
|
|
|
|
and UI all use.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import re
|
2008-08-10 19:21:12 -05:00
|
|
|
import inspect
|
2008-08-05 02:39:50 -05:00
|
|
|
import plugable
|
2008-08-06 22:38:49 -05:00
|
|
|
import errors
|
|
|
|
|
|
|
|
|
2008-08-06 23:51:21 -05:00
|
|
|
RULE_FLAG = 'validation_rule'
|
|
|
|
|
|
|
|
def rule(obj):
|
2008-08-08 12:11:29 -05:00
|
|
|
assert not hasattr(obj, RULE_FLAG)
|
|
|
|
setattr(obj, RULE_FLAG, True)
|
|
|
|
return obj
|
2008-08-06 23:51:21 -05:00
|
|
|
|
|
|
|
def is_rule(obj):
|
2008-08-08 12:11:29 -05:00
|
|
|
return callable(obj) and getattr(obj, RULE_FLAG, False) is True
|
2008-08-06 23:51:21 -05:00
|
|
|
|
|
|
|
|
2008-08-10 17:23:22 -05:00
|
|
|
class option(plugable.Plugin):
|
2008-08-08 12:11:29 -05:00
|
|
|
"""
|
|
|
|
The option class represents a kw argument from a command.
|
|
|
|
"""
|
|
|
|
|
|
|
|
__public__ = frozenset((
|
2008-08-08 16:40:03 -05:00
|
|
|
'normalize',
|
|
|
|
'default',
|
2008-08-11 14:11:26 -05:00
|
|
|
'validate',
|
2008-08-08 16:40:03 -05:00
|
|
|
'required',
|
|
|
|
'type',
|
2008-08-08 12:11:29 -05:00
|
|
|
))
|
|
|
|
__rules = None
|
2008-08-11 11:29:37 -05:00
|
|
|
type = unicode
|
|
|
|
required = False
|
2008-08-08 12:11:29 -05:00
|
|
|
|
|
|
|
def normalize(self, value):
|
2008-08-08 16:40:03 -05:00
|
|
|
"""
|
|
|
|
Returns the normalized form of `value`. If `value` cannot be
|
|
|
|
normalized, NormalizationError is raised, which is a subclass of
|
|
|
|
ValidationError.
|
|
|
|
|
|
|
|
The base class implementation only does type coercion, but subclasses
|
|
|
|
might do other normalization (e.g., a unicode option might strip
|
|
|
|
leading and trailing white-space).
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
return self.type(value)
|
|
|
|
except (TypeError, ValueError):
|
|
|
|
raise errors.NormalizationError(
|
|
|
|
self.__class__.__name__, value, self.type
|
|
|
|
)
|
2008-08-08 12:11:29 -05:00
|
|
|
|
|
|
|
def validate(self, value):
|
2008-08-08 16:40:03 -05:00
|
|
|
"""
|
|
|
|
Calls each validation rule and if any rule fails, raises RuleError,
|
|
|
|
which is a subclass of ValidationError.
|
|
|
|
"""
|
|
|
|
for rule in self.rules:
|
|
|
|
msg = rule(value)
|
|
|
|
if msg is not None:
|
|
|
|
raise errors.RuleError(
|
|
|
|
self.__class__.__name__,
|
|
|
|
value,
|
|
|
|
rule,
|
|
|
|
msg,
|
|
|
|
)
|
2008-08-08 12:11:29 -05:00
|
|
|
|
|
|
|
def __get_rules(self):
|
2008-08-08 16:40:03 -05:00
|
|
|
"""
|
|
|
|
Returns the tuple of rule methods used for input validation. This
|
|
|
|
tuple is lazily initialized the first time the property is accessed.
|
|
|
|
"""
|
|
|
|
if self.__rules is None:
|
2008-08-11 14:11:26 -05:00
|
|
|
rules = tuple(sorted(
|
2008-08-08 16:40:03 -05:00
|
|
|
self.__rules_iter(),
|
|
|
|
key=lambda f: getattr(f, '__name__'),
|
|
|
|
))
|
2008-08-11 14:11:26 -05:00
|
|
|
object.__setattr__(self, '_option__rules', rules)
|
2008-08-08 16:40:03 -05:00
|
|
|
return self.__rules
|
2008-08-08 12:11:29 -05:00
|
|
|
rules = property(__get_rules)
|
|
|
|
|
|
|
|
def __rules_iter(self):
|
2008-08-08 16:40:03 -05:00
|
|
|
"""
|
|
|
|
Iterates through the attributes in this instance to retrieve the
|
|
|
|
methods implementing validation rules.
|
|
|
|
"""
|
|
|
|
for name in dir(self.__class__):
|
|
|
|
if name.startswith('_'):
|
|
|
|
continue
|
|
|
|
base_attr = getattr(self.__class__, name)
|
|
|
|
if is_rule(base_attr):
|
|
|
|
attr = getattr(self, name)
|
|
|
|
if is_rule(attr):
|
|
|
|
yield attr
|
2008-08-08 12:11:29 -05:00
|
|
|
|
|
|
|
def default(self, **kw):
|
2008-08-08 16:40:03 -05:00
|
|
|
"""
|
|
|
|
Returns a default or auto-completed value for this option. If no
|
|
|
|
default is available, this method should return None.
|
2008-08-08 14:53:45 -05:00
|
|
|
|
2008-08-08 16:40:03 -05:00
|
|
|
All the keywords are passed so it's possible to build an
|
|
|
|
auto-completed value from other options values, e.g., build 'initials'
|
|
|
|
from 'givenname' + 'sn'.
|
|
|
|
"""
|
|
|
|
return None
|
2008-08-06 22:38:49 -05:00
|
|
|
|
2008-08-05 02:39:50 -05:00
|
|
|
|
2008-08-06 19:14:38 -05:00
|
|
|
class cmd(plugable.Plugin):
|
2008-08-08 12:11:29 -05:00
|
|
|
__public__ = frozenset((
|
2008-08-08 16:40:03 -05:00
|
|
|
'normalize',
|
2008-08-11 12:37:33 -05:00
|
|
|
'default',
|
|
|
|
'validate',
|
2008-08-11 14:11:26 -05:00
|
|
|
'execute',
|
2008-08-08 16:40:03 -05:00
|
|
|
'__call__',
|
|
|
|
'get_doc',
|
2008-08-11 11:29:37 -05:00
|
|
|
'options',
|
2008-08-08 12:11:29 -05:00
|
|
|
|
|
|
|
))
|
2008-08-10 19:21:12 -05:00
|
|
|
__options = None
|
|
|
|
option_classes = tuple()
|
2008-08-08 12:11:29 -05:00
|
|
|
|
|
|
|
def get_doc(self, _):
|
2008-08-08 16:40:03 -05:00
|
|
|
"""
|
|
|
|
Returns the gettext translated doc-string for this command.
|
2008-08-08 12:11:29 -05:00
|
|
|
|
2008-08-08 16:40:03 -05:00
|
|
|
For example:
|
2008-08-08 12:11:29 -05:00
|
|
|
|
2008-08-08 16:40:03 -05:00
|
|
|
>>> def get_doc(self, _):
|
|
|
|
>>> return _('add new user')
|
|
|
|
"""
|
|
|
|
raise NotImplementedError('%s.get_doc()' % self.name)
|
2008-08-08 12:11:29 -05:00
|
|
|
|
|
|
|
def get_options(self):
|
2008-08-08 16:40:03 -05:00
|
|
|
"""
|
2008-08-10 19:21:12 -05:00
|
|
|
Returns iterable with option proxy objects used to create the option
|
|
|
|
NameSpace when __get_option() is called.
|
2008-08-08 16:40:03 -05:00
|
|
|
"""
|
2008-08-10 19:21:12 -05:00
|
|
|
for cls in self.option_classes:
|
|
|
|
assert inspect.isclass(cls)
|
2008-08-11 11:29:37 -05:00
|
|
|
o = cls()
|
|
|
|
o.__lock__()
|
|
|
|
yield plugable.Proxy(option, o)
|
2008-08-08 12:11:29 -05:00
|
|
|
|
2008-08-10 19:21:12 -05:00
|
|
|
def __get_options(self):
|
2008-08-08 16:40:03 -05:00
|
|
|
"""
|
2008-08-10 19:21:12 -05:00
|
|
|
Returns the NameSpace containing the option proxy objects.
|
2008-08-08 16:40:03 -05:00
|
|
|
"""
|
2008-08-10 19:21:12 -05:00
|
|
|
if self.__options is None:
|
|
|
|
self.__options = plugable.NameSpace(self.get_options())
|
2008-08-11 11:29:37 -05:00
|
|
|
return self.__options
|
2008-08-10 19:21:12 -05:00
|
|
|
options = property(__get_options)
|
2008-08-08 12:11:29 -05:00
|
|
|
|
|
|
|
def normalize_iter(self, kw):
|
2008-08-08 16:40:03 -05:00
|
|
|
for (key, value) in kw.items():
|
|
|
|
if key in self.options:
|
|
|
|
yield (
|
|
|
|
key, self.options[key].normalize(value)
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
yield (key, value)
|
2008-08-08 12:11:29 -05:00
|
|
|
|
|
|
|
def normalize(self, **kw):
|
2008-08-08 16:40:03 -05:00
|
|
|
return dict(self.normalize_iter(kw))
|
2008-08-08 12:11:29 -05:00
|
|
|
|
2008-08-11 12:57:07 -05:00
|
|
|
def default_iter(self, kw):
|
|
|
|
for option in self.options:
|
|
|
|
if option.name not in kw:
|
|
|
|
value = option.default(**kw)
|
2008-08-08 16:40:03 -05:00
|
|
|
if value is not None:
|
2008-08-11 12:57:07 -05:00
|
|
|
yield(option.name, value)
|
|
|
|
|
|
|
|
def default(self, **kw):
|
|
|
|
return dict(self.default_iter(kw))
|
2008-08-08 12:11:29 -05:00
|
|
|
|
2008-08-11 12:37:33 -05:00
|
|
|
def validate(self, **kw):
|
|
|
|
for (key, value) in kw.items():
|
|
|
|
if key in self.options:
|
2008-08-11 14:11:26 -05:00
|
|
|
self.options[key].validate(value)
|
|
|
|
|
2008-08-11 14:35:57 -05:00
|
|
|
def execute(self, **kw):
|
2008-08-11 14:11:26 -05:00
|
|
|
pass
|
|
|
|
|
|
|
|
def print_n_call(self, method, kw):
|
|
|
|
print '%s.%s(%s)' % (
|
|
|
|
self.name,
|
|
|
|
method,
|
|
|
|
' '.join('%s=%r' % (k, v) for (k, v) in kw.items()),
|
|
|
|
)
|
|
|
|
getattr(self, method)(**kw)
|
2008-08-11 12:37:33 -05:00
|
|
|
|
2008-08-08 12:11:29 -05:00
|
|
|
def __call__(self, **kw):
|
2008-08-11 12:37:33 -05:00
|
|
|
kw = self.normalize(**kw)
|
2008-08-11 12:57:07 -05:00
|
|
|
kw.update(self.default(**kw))
|
2008-08-11 12:37:33 -05:00
|
|
|
self.validate(**kw)
|
2008-08-11 14:35:57 -05:00
|
|
|
return self.execute(**kw)
|
2008-08-05 16:10:49 -05:00
|
|
|
|
2008-08-05 02:39:50 -05:00
|
|
|
|
2008-08-06 19:14:38 -05:00
|
|
|
class obj(plugable.Plugin):
|
2008-08-08 12:11:29 -05:00
|
|
|
__public__ = frozenset((
|
2008-08-08 16:40:03 -05:00
|
|
|
'mthd',
|
|
|
|
'prop',
|
2008-08-08 12:11:29 -05:00
|
|
|
))
|
|
|
|
__mthd = None
|
|
|
|
__prop = None
|
2008-08-05 21:00:18 -05:00
|
|
|
|
2008-08-08 12:11:29 -05:00
|
|
|
def __get_mthd(self):
|
2008-08-08 16:40:03 -05:00
|
|
|
return self.__mthd
|
2008-08-08 12:11:29 -05:00
|
|
|
mthd = property(__get_mthd)
|
2008-08-05 21:00:18 -05:00
|
|
|
|
2008-08-08 12:11:29 -05:00
|
|
|
def __get_prop(self):
|
2008-08-08 16:40:03 -05:00
|
|
|
return self.__prop
|
2008-08-08 12:11:29 -05:00
|
|
|
prop = property(__get_prop)
|
2008-08-05 21:00:18 -05:00
|
|
|
|
2008-08-08 12:11:29 -05:00
|
|
|
def finalize(self, api):
|
2008-08-08 16:40:03 -05:00
|
|
|
super(obj, self).finalize(api)
|
|
|
|
self.__mthd = self.__create_ns('mthd')
|
|
|
|
self.__prop = self.__create_ns('prop')
|
2008-08-05 21:00:18 -05:00
|
|
|
|
2008-08-08 12:11:29 -05:00
|
|
|
def __create_ns(self, name):
|
2008-08-08 16:40:03 -05:00
|
|
|
return plugable.NameSpace(self.__filter(name))
|
2008-08-05 21:00:18 -05:00
|
|
|
|
2008-08-08 12:11:29 -05:00
|
|
|
def __filter(self, name):
|
2008-08-08 16:40:03 -05:00
|
|
|
for i in getattr(self.api, name):
|
|
|
|
if i.obj_name == self.name:
|
2008-08-08 20:46:12 -05:00
|
|
|
yield i.__clone__('attr_name')
|
2008-08-05 02:39:50 -05:00
|
|
|
|
|
|
|
|
|
|
|
class attr(plugable.Plugin):
|
2008-08-08 12:11:29 -05:00
|
|
|
__obj = None
|
2008-08-05 02:39:50 -05:00
|
|
|
|
2008-08-08 12:11:29 -05:00
|
|
|
def __init__(self):
|
2008-08-08 16:40:03 -05:00
|
|
|
m = re.match('^([a-z]+)_([a-z]+)$', self.__class__.__name__)
|
|
|
|
assert m
|
|
|
|
self.__obj_name = m.group(1)
|
|
|
|
self.__attr_name = m.group(2)
|
2008-08-05 02:39:50 -05:00
|
|
|
|
2008-08-08 12:11:29 -05:00
|
|
|
def __get_obj_name(self):
|
2008-08-08 16:40:03 -05:00
|
|
|
return self.__obj_name
|
2008-08-08 12:11:29 -05:00
|
|
|
obj_name = property(__get_obj_name)
|
2008-08-05 02:39:50 -05:00
|
|
|
|
2008-08-08 12:11:29 -05:00
|
|
|
def __get_attr_name(self):
|
2008-08-08 16:40:03 -05:00
|
|
|
return self.__attr_name
|
2008-08-08 12:11:29 -05:00
|
|
|
attr_name = property(__get_attr_name)
|
2008-08-05 02:39:50 -05:00
|
|
|
|
2008-08-08 12:11:29 -05:00
|
|
|
def __get_obj(self):
|
2008-08-08 16:40:03 -05:00
|
|
|
"""
|
|
|
|
Returns the obj instance this attribute is associated with, or None
|
|
|
|
if no association has been set.
|
|
|
|
"""
|
|
|
|
return self.__obj
|
2008-08-08 12:11:29 -05:00
|
|
|
obj = property(__get_obj)
|
2008-08-05 02:39:50 -05:00
|
|
|
|
2008-08-08 12:11:29 -05:00
|
|
|
def finalize(self, api):
|
2008-08-08 16:40:03 -05:00
|
|
|
super(attr, self).finalize(api)
|
|
|
|
self.__obj = api.obj[self.obj_name]
|
2008-08-05 02:39:50 -05:00
|
|
|
|
|
|
|
|
|
|
|
class mthd(attr, cmd):
|
2008-08-08 12:11:29 -05:00
|
|
|
__public__ = frozenset((
|
2008-08-08 16:40:03 -05:00
|
|
|
'obj',
|
|
|
|
'obj_name',
|
2008-08-08 12:11:29 -05:00
|
|
|
))
|
2008-08-05 02:39:50 -05:00
|
|
|
|
|
|
|
|
|
|
|
class prop(attr):
|
2008-08-08 12:11:29 -05:00
|
|
|
__public__ = frozenset((
|
2008-08-08 16:40:03 -05:00
|
|
|
'obj',
|
|
|
|
'obj_name',
|
2008-08-08 12:11:29 -05:00
|
|
|
))
|
2008-08-05 16:10:49 -05:00
|
|
|
|
2008-08-08 12:11:29 -05:00
|
|
|
def get_doc(self, _):
|
2008-08-08 16:40:03 -05:00
|
|
|
return _('prop doc')
|
2008-08-05 17:21:57 -05:00
|
|
|
|
2008-08-05 16:10:49 -05:00
|
|
|
|
|
|
|
class PublicAPI(plugable.API):
|
2008-08-08 12:11:29 -05:00
|
|
|
__max_cmd_len = None
|
|
|
|
|
|
|
|
def __init__(self):
|
2008-08-08 16:40:03 -05:00
|
|
|
super(PublicAPI, self).__init__(cmd, obj, mthd, prop)
|
2008-08-08 12:11:29 -05:00
|
|
|
|
|
|
|
def __get_max_cmd_len(self):
|
2008-08-08 16:40:03 -05:00
|
|
|
if self.__max_cmd_len is None:
|
|
|
|
if not hasattr(self, 'cmd'):
|
|
|
|
return None
|
|
|
|
max_cmd_len = max(len(str(cmd)) for cmd in self.cmd)
|
|
|
|
object.__setattr__(self, '_PublicAPI__max_cmd_len', max_cmd_len)
|
|
|
|
return self.__max_cmd_len
|
2008-08-08 12:11:29 -05:00
|
|
|
max_cmd_len = property(__get_max_cmd_len)
|