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

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI12-security/trunk/ndg_xacml/ndg/xacml/core/functions/v1/regexp_match.py@7087
Revision 7087, 2.6 KB checked in by pjkersha, 10 years ago (diff)

Incomplete - task 2: XACML-Security Integration

  • updating epydoc ready for release.
  • Property svn:keywords set to Id
Line 
1"""NDG XACML1.0 Regular expression matching function module
2
3NERC DataGrid
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
13from ndg.xacml.core.context.exceptions import XacmlContextTypeError
14from ndg.xacml.core.attributevalue import AttributeValueClassFactory
15from ndg.xacml.core.functions import (AbstractFunction, 
16                                      FunctionClassFactoryInterface)
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, self.__class__.TYPE):
38            raise TypeError('Expecting %r derived type for "pat"; got %r' %
39                            (self.__class__.TYPE, 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.value, input.value))
46   
47
48attributeValueClassFactory = AttributeValueClassFactory()
49
50
51class StringRegexMatch(RegexpMatchBase):
52    """String regular expression match function class representation
53    """
54    FUNCTION_NS = 'urn:oasis:names:tc:xacml:1.0:function:string-regexp-match'
55    TYPE = attributeValueClassFactory('http://www.w3.org/2001/XMLSchema#string')
56   
57   
58class FunctionClassFactory(FunctionClassFactoryInterface):
59    """Class Factory for string-regexp-match XACML function class.  Go to
60    ndg.xacml.core.functions.v2.regexp_match module for additional functions
61    for other types.
62    """
63    def __call__(self, identifier):
64        '''Create class for the Round XACML function identifier
65       
66        @param identifier: XACML string regular expression function identifier
67        @type identifier: basestring
68        @return: at least one member of class corresponding to the given input
69        identifier
70        @rtype: ndg.xacml.core.functions.v1.round.Round
71        '''
72        if identifier == StringRegexMatch.FUNCTION_NS:
73            return StringRegexMatch
74        else:
75            return None
Note: See TracBrowser for help on using the repository browser.