source: TI12-security/trunk/NDG_XACML/ndg/xacml/core/functions/v1/at_least_one_member_of.py @ 6803

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI12-security/trunk/NDG_XACML/ndg/xacml/core/functions/v1/at_least_one_member_of.py@6803
Revision 6803, 2.1 KB checked in by pjkersha, 9 years ago (diff)

Refactored function modules with class factories to generate classes for each variable type.

Line 
1"""NDG XACML URI equal matching function module
2
3NERC DataGrid Project
4"""
5__author__ = "P J Kershaw"
6__date__ = "30/03/10"
7__copyright__ = ""
8__license__ = "BSD - see LICENSE file in top-level directory"
9__contact__ = "Philip.Kershaw@stfc.ac.uk"
10__revision__ = '$Id: $'
11from ndg.xacml.core.functions import AbstractFunction, FunctionClassFactoryBase
12from ndg.xacml.core.context.exceptions import XacmlContextTypeError
13from ndg.xacml.utils import TypedList as Bag
14
15
16class AtLeastOneMemberOfBase(AbstractFunction):
17    """Base class implementation for at least one member of XACML function -
18    check an item of a given type is contained in a given bag
19   
20    urn:oasis:names:tc:xacml:1.0:function:<type>-at-least-one-member-of
21    """
22    TYPE = None
23   
24    def evaluate(self, item, bag):
25        """Check input is contained in the bag
26       
27        @param item: check to see if this string is contained in the bag
28        @type item: type
29        @param bag: bag of <type> values
30        @type bag: TypedList(<type>)
31        @return: True if str is in bag, False otherwise
32        @rtype: bool
33        """
34        if not isinstance(item, self.__class__.TYPE):
35            raise XacmlContextTypeError('Expecting %r derived type for "item"; '
36                                        'got %r' % (self.__class__.TYPE, 
37                                                    type(item)))
38           
39        if not isinstance(bag, Bag):
40            raise XacmlContextTypeError('Expecting %r derived type for "bag"; '
41                                        'got %r' % (Bag, type(bag)))
42           
43        if bag.elementType != self.__class__.TYPE:
44            raise XacmlContextTypeError('Expecting %r type elements for "bag"; '
45                                        'got %r' %
46                                        (self.__class__.TYPE, bag.elementType))           
47        return item in bag
48   
49
50class FunctionClassFactory(FunctionClassFactoryBase):
51    """Class Factory for *-at-least-one-member-of XACML function classes
52    """
53    FUNCTION_NS_SUFFIX = '-at-least-one-member-of'
54    FUNCTION_BASE_CLASS = AtLeastOneMemberOfBase
Note: See TracBrowser for help on using the repository browser.