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

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI12-security/trunk/NDG_XACML/ndg/xacml/core/functions/v1/regexp_match.py@6804
Revision 6804, 2.4 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 any Regular expression matching function module
2
3NERC DataGrid Project
4"""
5__author__ = "P J Kershaw"
6__date__ = "26/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 re
12#import sys
13
14from ndg.xacml.core.functions import (AbstractFunction, 
15                                      FunctionClassFactoryInterface)
16from ndg.xacml.core.context.exceptions import XacmlContextTypeError
17
18
19class RegexpMatchBase(AbstractFunction):
20    """XACML 2.0 Regular Expression matching base class function
21    """
22    FUNCTION_NS = None
23    FUNCTION_NS_SUFFIX = '-regexp-match'
24    CLASS_NAME_SUFFIX = 'RegexpMatch'
25    TYPE = None
26   
27    def evaluate(self, pat, input):
28        """Match URI against regular expression pattern
29       
30        @param pat: regular expression
31        @type pat: basestring
32        @param input: URI to match
33        @type input: type
34        @return: True if URI matches pattern, False otherwise
35        @rtype: bool
36        """
37        if not isinstance(pat, basestring):
38            raise TypeError('Expecting %r derived type for "pat"; got %r' %
39                            (basestring, type(pat)))
40           
41        if not isinstance(input, self.__class__.TYPE):
42            raise TypeError('Expecting %r derived type for "input"; got %r' %
43                            (self.__class__.TYPE, type(input)))
44           
45        return bool(re.match(pat, input))
46   
47
48class StringRegexMatch(RegexpMatchBase):
49    FUNCTION_NS = 'urn:oasis:names:tc:xacml:1.0:function:string-regexp-match'
50    TYPE = basestring
51   
52   
53class FunctionClassFactory(FunctionClassFactoryInterface):
54    """Class Factory for string-regexp-match XACML function class.  Go to
55    ndg.xacml.core.functions.v2.regexp_match module for additional functions
56    for other types.
57    """
58    def __call__(self, identifier):
59        '''Create class for the Round XACML function identifier
60       
61        @param identifier: XACML string regular expression function identifier
62        @type identifier: basestring
63        @return: at least one member of class corresponding to the given input
64        identifier
65        @rtype: ndg.xacml.core.functions.v1.round.Round
66        '''
67        if identifier == StringRegexMatch.FUNCTION_NS:
68            return StringRegexMatch
69        else:
70            return None
Note: See TracBrowser for help on using the repository browser.