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

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@6796
Revision 6796, 2.3 KB checked in by pjkersha, 10 years ago (diff)

Refactored functions package to enable dynamic creation of function classes for all the XACML primitive types for any given function e.g. equal module implements EqualBase? and dynamically creates the <type>-equal classes for all the types: string, AnyURI, Boolean etc.

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: $'
11import sys
12
13from ndg.xacml.core.functions import AbstractFunction, TYPE_MAP
14from ndg.xacml.core.context.exceptions import XacmlContextError
15from ndg.xacml.utils import TypedList as Bag
16
17
18class AtLeastOneMemberOfBase(AbstractFunction):
19    """Base class implementation for at least one member of XACML function -
20    check an item of a given type is contained in a given bag
21   
22    urn:oasis:names:tc:xacml:1.0:function:<type>-at-least-one-member-of
23    """
24    TYPE = None
25    FUNCTION_SUFFIX = 'AtLeastOneMemberOf'
26    FUNCTION_NS_SUFFIX = '-at-least-one-member-of'
27   
28    def evaluate(self, item, bag):
29        """Check input is contained in the bag
30       
31        @param item: check to see if this string is contained in the bag
32        @type item: type
33        @param bag: bag of <type> values
34        @type bag: TypedList(<type>)
35        @return: True if str is in bag, False otherwise
36        @rtype: bool
37        """
38        if not isinstance(item, self.__class__.TYPE):
39            raise XacmlContextError('Expecting %r derived type for "str"; '
40                                    'got %r' % (self.__class__.TYPE, type(str)))
41           
42        if not isinstance(bag, Bag):
43            raise XacmlContextError('Expecting %r derived type for "bag"; got '
44                                    '%r' % (Bag, type(bag)))
45           
46        if bag.elementType != self.__class__.TYPE:
47            raise XacmlContextError('Expecting %r type elements for "bag"; '
48                                    'got %r' %
49                                    (self.__class__.TYPE, bag.elementType))           
50        return str in bag
51   
52
53_thisModule = sys.modules[__name__]
54
55for typeName, _type in TYPE_MAP.items():
56    className = typeName + AtLeastOneMemberOfBase.FUNCTION_SUFFIX
57    classVars = {
58        'TYPE': _type,
59        'FUNCTION_NS': AbstractFunction.V1_0_FUNCTION_NS + typeName[0
60            ].lower() + typeName[1:] + AtLeastOneMemberOfBase.FUNCTION_NS_SUFFIX
61    }
62    setattr(_thisModule, 
63            className, 
64            type(className, (AtLeastOneMemberOfBase, ), classVars)) 
Note: See TracBrowser for help on using the repository browser.