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

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

Refactored function modules with class factories to generate classes for each variable type.

Line 
1"""NDG XACML any URI 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, FunctionClassFactoryBase
15from ndg.xacml.core.context.exceptions import XacmlContextTypeError
16
17
18class RegexpMatchBase(AbstractFunction):
19    """XACML 2.0 Regular Expression matching base class function
20    """
21    FUNCTION_NS = None
22    FUNCTION_NS_SUFFIX = '-regexp-match'
23    CLASS_NAME_SUFFIX = 'RegexpMatch'
24    TYPE = None
25#    TYPE_MAP = {}.fromkeys(
26#        ('AnyURI',
27#        'IpAddress',
28#        'DnsName',
29#        'Rfc822Name',
30#        'X500Name'),
31#        basestring)
32   
33    def evaluate(self, pat, input):
34        """Match URI against regular expression pattern
35       
36        @param pat: regular expression
37        @type pat: basestring
38        @param input: URI to match
39        @type input: type
40        @return: True if URI matches pattern, False otherwise
41        @rtype: bool
42        """
43        if not isinstance(pat, basestring):
44            raise TypeError('Expecting %r derived type for "pat"; got %r' %
45                            (basestring, type(pat)))
46           
47        if not isinstance(input, self.__class__.TYPE):
48            raise TypeError('Expecting %r derived type for "input"; got %r' %
49                            (self.__class__.TYPE, type(input)))
50           
51        return bool(re.match(pat, input))
52   
53
54class FunctionClassFactory(FunctionClassFactoryBase):
55    """Class Factory for *-regexp-match XACML function classes
56    """
57    FUNCTION_NS_SUFFIX = RegexpMatchBase.FUNCTION_NS_SUFFIX
58    FUNCTION_BASE_CLASS = RegexpMatchBase
59   
60       
61#_thisModule = sys.modules[__name__]
62#
63#for typeName, _type in RegexpMatchBase.TYPE_MAP.items():
64#    className = typeName + RegexpMatchBase.CLASS_NAME_SUFFIX
65#    classVars = {
66#        'TYPE': _type,
67#        'FUNCTION_NS': AbstractFunction.V2_0_FUNCTION_NS + typeName[0
68#            ].lower() + typeName[1:] + RegexpMatchBase.FUNCTION_NS_SUFFIX
69#    }
70#    setattr(_thisModule,
71#            className,
72#            type(className, (RegexpMatchBase, ), classVars))
Note: See TracBrowser for help on using the repository browser.