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

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

Added AttributeValueFactory? to generate <type>AttributeValue? classes.

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