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

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI12-security/trunk/ndg_xacml/ndg/xacml/core/functions/v1/regexp_match.py@7088
Revision 7088, 3.4 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    @cvar TYPE: attribute type for the given implementation.  Derived classes
23    should set appropriately
24    @type TYPE: NoneType
25   
26    @cvar FUNCTION_SUFFIX: suffix for all regular expression type function class
27    names
28    @type FUNCTION_SUFFIX: string
29   
30    @cvar FUNCTION_NS_SUFFIX: generic suffix for regular expression type
31    function URNs
32    @type FUNCTION_NS_SUFFIX: string
33   
34    @cvar CLASS_NAME_SUFFIX: suffix for all regular expression class names
35    @type CLASS_NAME_SUFFIX: string
36    """
37    FUNCTION_NS = None
38    FUNCTION_NS_SUFFIX = '-regexp-match'
39    CLASS_NAME_SUFFIX = 'RegexpMatch'
40    TYPE = None
41   
42    def evaluate(self, pat, input):
43        """Match URI against regular expression pattern
44       
45        @param pat: regular expression
46        @type pat: basestring
47        @param input: URI to match
48        @type input: type
49        @return: True if URI matches pattern, False otherwise
50        @rtype: bool
51        """
52        if not isinstance(pat, self.__class__.TYPE):
53            raise TypeError('Expecting %r derived type for "pat"; got %r' %
54                            (self.__class__.TYPE, type(pat)))
55           
56        if not isinstance(input, self.__class__.TYPE):
57            raise TypeError('Expecting %r derived type for "input"; got %r' %
58                            (self.__class__.TYPE, type(input)))
59           
60        return bool(re.match(pat.value, input.value))
61   
62
63attributeValueClassFactory = AttributeValueClassFactory()
64
65
66class StringRegexMatch(RegexpMatchBase):
67    """String regular expression match function class representation
68   
69    @cvar FUNCTION_NS: String regular expression match function URN
70    @type FUNCTION_NS: string
71   
72    @cvar TYPE: string attribute value type
73    @type TYPE: dynamically generated string type derived from
74    ndg.xacml.core.attributevalue.AttributeValue
75    """
76    FUNCTION_NS = 'urn:oasis:names:tc:xacml:1.0:function:string-regexp-match'
77    TYPE = attributeValueClassFactory('http://www.w3.org/2001/XMLSchema#string')
78   
79   
80class FunctionClassFactory(FunctionClassFactoryInterface):
81    """Class Factory for string-regexp-match XACML function class.  Go to
82    ndg.xacml.core.functions.v2.regexp_match module for additional functions
83    for other types.
84    """
85    def __call__(self, identifier):
86        '''Create class for the Round XACML function identifier
87       
88        @param identifier: XACML string regular expression function identifier
89        @type identifier: basestring
90        @return: at least one member of class corresponding to the given input
91        identifier
92        @rtype: ndg.xacml.core.functions.v1.round.Round
93        '''
94        if identifier == StringRegexMatch.FUNCTION_NS:
95            return StringRegexMatch
96        else:
97            return None
Note: See TracBrowser for help on using the repository browser.