mirror of
https://salsa.debian.org/freeipa-team/freeipa.git
synced 2025-01-23 06:43:13 -06:00
19: Removed depreciated base2.py and test_base2.py files
This commit is contained in:
parent
9b3e2f5cec
commit
89e9b7eaec
154
ipalib/base2.py
154
ipalib/base2.py
@ -1,154 +0,0 @@
|
||||
# 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 plug-in architecture and generative API.
|
||||
"""
|
||||
|
||||
import inspect
|
||||
import exceptions
|
||||
from base import NameSpace
|
||||
|
||||
|
||||
class Named(object):
|
||||
def __get_name(self):
|
||||
return self.__class__.__name__
|
||||
name = property(__get_name)
|
||||
|
||||
|
||||
class WithObj(Named):
|
||||
_obj = None
|
||||
__obj = None
|
||||
__obj_locked = False
|
||||
|
||||
def __get_obj(self):
|
||||
return self.__obj
|
||||
def __set_obj(self, obj):
|
||||
if self.__obj_locked:
|
||||
raise exceptions.TwiceSetError(self.__class__.__name__, 'obj')
|
||||
self.__obj_locked = True
|
||||
if obj is None:
|
||||
assert self.__obj is None
|
||||
assert self.obj is None
|
||||
else:
|
||||
assert isinstance(obj, Named)
|
||||
assert isinstance(self._obj, str)
|
||||
assert obj.name == self._obj
|
||||
self.__obj = obj
|
||||
assert self.obj is obj
|
||||
obj = property(__get_obj, __set_obj)
|
||||
|
||||
|
||||
class Command(WithObj):
|
||||
pass
|
||||
|
||||
class Property(WithObj):
|
||||
pass
|
||||
|
||||
class Object(Named):
|
||||
__commands = None
|
||||
|
||||
def __get_commands(self):
|
||||
return self.__commands
|
||||
def __set_commands(self, commands):
|
||||
if self.__commands is not None:
|
||||
raise exceptions.TwiceSetError(
|
||||
self.__class__.__name__, 'commands'
|
||||
)
|
||||
assert type(commands) is NameSpace
|
||||
self.__commands = commands
|
||||
assert self.commands is commands
|
||||
commands = property(__get_commands, __set_commands)
|
||||
|
||||
|
||||
class Collector(object):
|
||||
def __init__(self):
|
||||
self.__d = {}
|
||||
self.globals = []
|
||||
|
||||
def __getitem__(self, key):
|
||||
assert isinstance(key, str)
|
||||
if key not in self.__d:
|
||||
self.__d[key] = []
|
||||
return self.__d[key]
|
||||
|
||||
def __iter__(self):
|
||||
for key in self.__d:
|
||||
yield key
|
||||
|
||||
def add(self, i):
|
||||
assert isinstance(i, WithObj)
|
||||
if i._obj is None:
|
||||
self.globals.append(i)
|
||||
else:
|
||||
self[i._obj].append(i)
|
||||
|
||||
def namespaces(self):
|
||||
for key in self:
|
||||
d = dict((i.name, i) for i in self[key])
|
||||
yield (key, NameSpace(d))
|
||||
|
||||
|
||||
|
||||
class Registrar(object):
|
||||
__object = None
|
||||
__commands = None
|
||||
__properties = None
|
||||
|
||||
def __init__(self):
|
||||
self.__tmp_objects = {}
|
||||
self.__tmp_commands = {}
|
||||
self.__tmp_properties = {}
|
||||
|
||||
def __get_objects(self):
|
||||
return self.__objects
|
||||
objects = property(__get_objects)
|
||||
|
||||
def __get_commands(self):
|
||||
return self.__commands
|
||||
commands = property(__get_commands)
|
||||
|
||||
def __get_target(self, i):
|
||||
if isinstance(i, Object):
|
||||
return (self.__tmp_objects, i.name)
|
||||
if isinstance(i, Command):
|
||||
return (self.__tmp_commands, i.name)
|
||||
assert isinstance(i, Property)
|
||||
|
||||
|
||||
def register(self, cls):
|
||||
assert inspect.isclass(cls)
|
||||
assert issubclass(cls, Named)
|
||||
i = cls()
|
||||
(target, key) = self.__get_target(i)
|
||||
target[key] = i
|
||||
|
||||
def finalize(self):
|
||||
obj_cmd = Collector()
|
||||
for cmd in self.__tmp_commands.values():
|
||||
if cmd._obj is None:
|
||||
cmd.obj = None
|
||||
else:
|
||||
obj = self.__tmp_objects[cmd._obj]
|
||||
cmd.obj = obj
|
||||
obj_cmd.add(cmd)
|
||||
self.__objects = NameSpace(self.__tmp_objects)
|
||||
self.__commands = NameSpace(self.__tmp_commands)
|
||||
for (key, ns) in obj_cmd.namespaces():
|
||||
self.objects[key].commands = ns
|
@ -1,155 +0,0 @@
|
||||
# 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
|
||||
|
||||
"""
|
||||
Unit tests for `ipalib.base` module.
|
||||
"""
|
||||
|
||||
from ipalib import base2 as base
|
||||
from ipalib import exceptions
|
||||
|
||||
|
||||
def read_only(obj, name):
|
||||
"""
|
||||
Check that a given property is read-only.
|
||||
Returns the value of the property.
|
||||
"""
|
||||
assert isinstance(obj, object)
|
||||
assert hasattr(obj, name)
|
||||
raised = False
|
||||
try:
|
||||
setattr(obj, name, 'some new obj')
|
||||
except AttributeError:
|
||||
raised = True
|
||||
assert raised
|
||||
return getattr(obj, name)
|
||||
|
||||
|
||||
class ClassChecker(object):
|
||||
cls = None # Override this is subclasses
|
||||
|
||||
def new(self, *args, **kw):
|
||||
return self.cls(*args, **kw)
|
||||
|
||||
def args(self):
|
||||
return []
|
||||
|
||||
def kw(self):
|
||||
return {}
|
||||
|
||||
def std(self):
|
||||
return self.new(*self.args(), **self.kw())
|
||||
|
||||
|
||||
|
||||
def test_Named():
|
||||
class named_class(base.Named):
|
||||
pass
|
||||
|
||||
i = named_class()
|
||||
assert i.name == 'named_class'
|
||||
|
||||
|
||||
def test_WithObj():
|
||||
class some_object(base.Named):
|
||||
pass
|
||||
|
||||
class another_object(base.Named):
|
||||
pass
|
||||
|
||||
class some_command(base.WithObj):
|
||||
_obj = 'some_object'
|
||||
|
||||
obj = some_object()
|
||||
cmd = some_command()
|
||||
|
||||
# Test that it can be set:
|
||||
assert cmd.obj is None
|
||||
cmd.obj = obj
|
||||
assert cmd.obj is obj
|
||||
|
||||
# Test that it cannot be set twice:
|
||||
raised = False
|
||||
try:
|
||||
cmd.obj = obj
|
||||
except exceptions.TwiceSetError:
|
||||
raised = True
|
||||
assert raised
|
||||
|
||||
# Test that it can't be set with the wrong name:
|
||||
obj = another_object()
|
||||
cmd = some_command()
|
||||
raised = False
|
||||
try:
|
||||
cmd.obj = obj
|
||||
except AssertionError:
|
||||
raised = True
|
||||
assert raised
|
||||
|
||||
|
||||
def test_Registar():
|
||||
class adduser(base.Command):
|
||||
_obj = 'user'
|
||||
class moduser(base.Command):
|
||||
_obj = 'user'
|
||||
class deluser(base.Command):
|
||||
_obj = 'user'
|
||||
class finduser(base.Command):
|
||||
_obj = 'user'
|
||||
class kinit(base.Command):
|
||||
pass
|
||||
class user(base.Object):
|
||||
pass
|
||||
class group(base.Object):
|
||||
pass
|
||||
|
||||
r = base.Registrar()
|
||||
r.register(adduser)
|
||||
r.register(moduser)
|
||||
r.register(deluser)
|
||||
r.register(finduser)
|
||||
r.register(kinit)
|
||||
r.register(user)
|
||||
r.register(group)
|
||||
|
||||
r.finalize()
|
||||
assert len(r.commands) == 5
|
||||
assert len(r.objects) == 2
|
||||
|
||||
obj = r.objects.user
|
||||
assert type(obj) is user
|
||||
for name in ['adduser', 'moduser', 'deluser', 'finduser']:
|
||||
cmd = r.commands[name]
|
||||
assert type(cmd) is locals()[name]
|
||||
assert cmd.obj is obj
|
||||
|
||||
assert r.commands.kinit.obj is None
|
||||
|
||||
for cmd in r.commands():
|
||||
raised = False
|
||||
try:
|
||||
cmd.obj = None
|
||||
except exceptions.TwiceSetError:
|
||||
raised = True
|
||||
assert raised
|
||||
|
||||
u = r.objects.user
|
||||
assert isinstance(u.commands, base.NameSpace)
|
||||
assert len(u.commands) == 4
|
||||
assert list(u.commands) == ['adduser', 'deluser', 'finduser', 'moduser']
|
Loading…
Reference in New Issue
Block a user