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

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

Working class factories for each function module. Function class evaluate methods need to be refactored to accept *AttributeValue? types

Line 
1"""NDG XACML one and only functions module
2
3NERC DataGrid Project
4"""
5__author__ = "P J Kershaw"
6__date__ = "01/04/10"
7__copyright__ = ""
8__license__ = "BSD - see LICENSE file in top-level directory"
9__contact__ = "Philip.Kershaw@stfc.ac.uk"
10__revision__ = '$Id: $'
11#from datetime import datetime, timedelta
12
13from ndg.xacml.core.functions import AbstractFunction, FunctionClassFactoryBase
14from ndg.xacml.utils import TypedList as Bag
15from ndg.xacml.core.context.exceptions import XacmlContextError
16
17
18class OneAndOnlyBase(AbstractFunction):
19    """Base class for XACML <type>-one-and-only functions"""
20    BAG_TYPE = None
21   
22    def evaluate(self, bag):
23        """Check a bag has one element only and return it
24       
25        @param bag: bag containing one element
26        @rtype: bool
27        """
28        if not isinstance(bag, Bag):
29            raise XacmlContextError('Expecting %r derived type for "bag"; '
30                                    'got %r' % (Bag, type(bag)))
31           
32        if bag.elementType != self.__class__.BAG_TYPE:
33            raise XacmlContextError('Expecting %r type elements for "bag"; '
34                                    'got %r' %
35                                    (self.__class__.BAG_TYPE, bag.elementType))
36                       
37        nBagElems = len(bag)
38        if nBagElems != 1:
39            raise XacmlContextError('Expecting single element for %r bag; got '
40                                    '%r' %
41                                    (self.__class__.BAG_TYPE, nBagElems))
42           
43        return bag[0]
44   
45
46class FunctionClassFactory(FunctionClassFactoryBase):
47    """Class Factory for *-one-and-only XACML function classes
48    """
49    FUNCTION_NAMES = (
50        'urn:oasis:names:tc:xacml:1.0:function:string-one-and-only',
51        'urn:oasis:names:tc:xacml:1.0:function:boolean-one-and-only',
52        'urn:oasis:names:tc:xacml:1.0:function:integer-one-and-only',
53        'urn:oasis:names:tc:xacml:1.0:function:double-one-and-only',
54        'urn:oasis:names:tc:xacml:1.0:function:time-one-and-only',
55        'urn:oasis:names:tc:xacml:1.0:function:date-one-and-only',
56        'urn:oasis:names:tc:xacml:1.0:function:dateTime-one-and-only',
57        'urn:oasis:names:tc:xacml:1.0:function:anyURI-one-and-only',
58        'urn:oasis:names:tc:xacml:1.0:function:hexBinary-one-and-only',
59        'urn:oasis:names:tc:xacml:1.0:function:base64Binary-one-and-only',
60        'urn:oasis:names:tc:xacml:1.0:function:dayTimeDuration-one-and-only',
61        'urn:oasis:names:tc:xacml:1.0:function:yearMonthDuration-one-and-only',
62        'urn:oasis:names:tc:xacml:1.0:function:x500Name-one-and-only',
63        'urn:oasis:names:tc:xacml:1.0:function:rfc822Name-one-and-only'
64    )
65    FUNCTION_NS_SUFFIX = '-one-and-only'
66    FUNCTION_BASE_CLASS = OneAndOnlyBase
67   
Note: See TracBrowser for help on using the repository browser.