Package ndg :: Package xacml :: Package core :: Package functions :: Package v1 :: Module regexp_match
[hide private]

Source Code for Module ndg.xacml.core.functions.v1.regexp_match

  1  """NDG XACML1.0 Regular expression matching function module 
  2   
  3  NERC 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: regexp_match.py 8017 2012-02-16 08:39:08Z pjkersha $' 
 11  import re 
 12   
 13  from ndg.xacml.core.attributevalue import AttributeValueClassFactory 
 14  from ndg.xacml.core.functions import (AbstractFunction,  
 15                                        FunctionClassFactoryInterface) 
 16   
 17   
18 -class RegexpMatchBase(AbstractFunction):
19 """XACML 2.0 Regular Expression matching base class function 20 21 @cvar TYPE: attribute type for the given implementation. Derived classes 22 should set appropriately 23 @type TYPE: NoneType 24 25 @cvar FUNCTION_SUFFIX: suffix for all regular expression type function class 26 names 27 @type FUNCTION_SUFFIX: string 28 29 @cvar FUNCTION_NS_SUFFIX: generic suffix for regular expression type 30 function URNs 31 @type FUNCTION_NS_SUFFIX: string 32 33 @cvar CLASS_NAME_SUFFIX: suffix for all regular expression class names 34 @type CLASS_NAME_SUFFIX: string 35 36 @cvar compiled_regexes: cache of compiled regular expressions 37 @type compiled_regexes: dict of string mappings to re.RegexObject 38 """ 39 FUNCTION_NS = None 40 FUNCTION_NS_SUFFIX = '-regexp-match' 41 CLASS_NAME_SUFFIX = 'RegexpMatch' 42 TYPE = None 43 44 compiled_regexes = {} 45
46 - def evaluate(self, pat, input):
47 """Match URI against regular expression pattern 48 49 @param pat: regular expression 50 @type pat: basestring 51 @param input: URI to match 52 @type input: type 53 @return: True if URI matches pattern, False otherwise 54 @rtype: bool 55 """ 56 if not isinstance(pat, self.__class__.TYPE): 57 raise TypeError('Expecting %r derived type for "pat"; got %r' % 58 (self.__class__.TYPE, type(pat))) 59 60 if not isinstance(input, self.__class__.TYPE): 61 raise TypeError('Expecting %r derived type for "input"; got %r' % 62 (self.__class__.TYPE, type(input))) 63 64 compiled_regex = self.compiled_regexes.get(pat.value, None) 65 if compiled_regex is None: 66 compiled_regex = re.compile(pat.value) 67 self.compiled_regexes[pat.value] = compiled_regex 68 return bool(compiled_regex.match(input.value))
69 70 71 attributeValueClassFactory = AttributeValueClassFactory() 72 73
74 -class StringRegexMatch(RegexpMatchBase):
75 """String regular expression match function class representation 76 77 @cvar FUNCTION_NS: String regular expression match function URN 78 @type FUNCTION_NS: string 79 80 @cvar TYPE: string attribute value type 81 @type TYPE: dynamically generated string type derived from 82 ndg.xacml.core.attributevalue.AttributeValue 83 """ 84 FUNCTION_NS = 'urn:oasis:names:tc:xacml:1.0:function:string-regexp-match' 85 TYPE = attributeValueClassFactory('http://www.w3.org/2001/XMLSchema#string')
86 87
88 -class FunctionClassFactory(FunctionClassFactoryInterface):
89 """Class Factory for string-regexp-match XACML function class. Go to 90 ndg.xacml.core.functions.v2.regexp_match module for additional functions 91 for other types. 92 """
93 - def __call__(self, identifier):
94 '''Create class for the Round XACML function identifier 95 96 @param identifier: XACML string regular expression function identifier 97 @type identifier: basestring 98 @return: at least one member of class corresponding to the given input 99 identifier, if no match is found returns None 100 @rtype: ndg.xacml.core.functions.v1.regexp_match.StringRegexMatch / None 101 ''' 102 if identifier == StringRegexMatch.FUNCTION_NS: 103 return StringRegexMatch 104 else: 105 return None
106