Package ndg :: Package xacml :: Package parsers :: Package etree :: Module applyreader
[hide private]

Source Code for Module ndg.xacml.parsers.etree.applyreader

  1  """NDG XACML ElementTree based Apply type reader  
  2   
  3  NERC DataGrid 
  4  """ 
  5  __author__ = "P J Kershaw" 
  6  __date__ = "19/03/10" 
  7  __copyright__ = "(C) 2010 Science and Technology Facilities Council" 
  8  __contact__ = "Philip.Kershaw@stfc.ac.uk" 
  9  __license__ = "BSD - see LICENSE file in top-level directory" 
 10  __contact__ = "Philip.Kershaw@stfc.ac.uk" 
 11  __revision__ = "$Id: applyreader.py 8041 2012-03-16 11:51:01Z rwilkinson $" 
 12  import logging 
 13  log = logging.getLogger(__name__) 
 14   
 15  from ndg.xacml.core.apply import Apply 
 16  from ndg.xacml.core.attributevalue import AttributeValue 
 17  from ndg.xacml.core.condition import Condition 
 18  from ndg.xacml.core.variablereference import VariableReference 
 19  from ndg.xacml.core.attributeselector import AttributeSelector 
 20  from ndg.xacml.core.attributedesignator import (SubjectAttributeDesignator, 
 21                                                  ResourceAttributeDesignator, 
 22                                                  ActionAttributeDesignator, 
 23                                                  EnvironmentAttributeDesignator) 
 24  from ndg.xacml.parsers import XMLParseError 
 25  from ndg.xacml.parsers.etree import QName, getElementChildren 
 26  from ndg.xacml.parsers.etree.reader import ETreeAbstractReader 
 27  from ndg.xacml.parsers.etree.factory import ReaderFactory 
 28   
 29   
30 -class ApplyReader(ETreeAbstractReader):
31 '''ElementTree based XACML Apply type parser 32 33 @cvar FUNCTION_ELEMENT_LOCAL_NAME: XML local name for function element 34 @type FUNCTION_ELEMENT_LOCAL_NAME: string 35 36 @cvar VARIABLE_REFERENCE_ELEMENT_LOCAL_NAME: XML local name for variable 37 reference element 38 @type VARIABLE_REFERENCE_ELEMENT_LOCAL_NAME: string 39 40 @cvar TYPE: XACML class type that this reader will read values into 41 @type TYPE: abc.ABCMeta 42 ''' 43 TYPE = Apply 44 45 # These two are not currently implemented. When an implementation is made 46 # the ELEMENT_LOCAL_NAME may be referenced from the native class rather than 47 # a class variable here 48 FUNCTION_ELEMENT_LOCAL_NAME = 'Function' 49 VARIABLE_REFERENCE_ELEMENT_LOCAL_NAME = 'VariableReference' 50
51 - def __call__(self, obj, common):
52 """Parse Apply type object 53 54 @param obj: input object to parse 55 @type obj: ElementTree Element, or stream object 56 @return: ElementTree element 57 @rtype: xml.etree.Element 58 """ 59 elem = super(ApplyReader, self)._parse(obj) 60 61 xacmlType = self.__class__.TYPE 62 applyObj = xacmlType() 63 64 if QName.getLocalPart(elem.tag) != xacmlType.ELEMENT_LOCAL_NAME: 65 raise XMLParseError("No \"%s\" element found" % 66 xacmlType.ELEMENT_LOCAL_NAME) 67 68 # Unpack *required* attributes from top-level element 69 attributeValues = [] 70 for attributeName in (xacmlType.FUNCTION_ID_ATTRIB_NAME, ): 71 attributeValue = elem.attrib.get(attributeName) 72 if attributeValue is None: 73 raise XMLParseError('No "%s" attribute found in "%s" ' 74 'element' % (attributeName, 75 xacmlType.ELEMENT_LOCAL_NAME)) 76 77 attributeValues.append(attributeValue) 78 79 applyObj.functionId, = attributeValues 80 81 # Allow for any of the defined Expression sub-types in the child 82 # elements 83 for subElem in getElementChildren(elem): 84 localName = QName.getLocalPart(subElem.tag) 85 if localName == xacmlType.ELEMENT_LOCAL_NAME: 86 applyObj.expressions.append(ApplyReader.parse(subElem, common)) 87 88 elif localName == AttributeValue.ELEMENT_LOCAL_NAME: 89 AttributeValueReader = ReaderFactory.getReader(AttributeValue) 90 applyObj.expressions.append(AttributeValueReader.parse(subElem, 91 common)) 92 93 elif localName == SubjectAttributeDesignator.ELEMENT_LOCAL_NAME: 94 SubjectAttributeDesignatorReader = ReaderFactory.getReader( 95 SubjectAttributeDesignator) 96 applyObj.expressions.append( 97 SubjectAttributeDesignatorReader.parse(subElem, 98 common)) 99 100 elif localName == ResourceAttributeDesignator.ELEMENT_LOCAL_NAME: 101 ResourceAttributeDesignatorReader = ReaderFactory.getReader( 102 ResourceAttributeDesignator) 103 applyObj.expressions.append( 104 ResourceAttributeDesignatorReader.parse(subElem, 105 common)) 106 107 elif localName == EnvironmentAttributeDesignator.ELEMENT_LOCAL_NAME: 108 EnvironmentAttributeDesignatorReader = ReaderFactory.getReader( 109 EnvironmentAttributeDesignator) 110 applyObj.expressions.append( 111 EnvironmentAttributeDesignatorReader.parse(subElem, 112 common)) 113 114 elif localName == ActionAttributeDesignator.ELEMENT_LOCAL_NAME: 115 ActionAttributeDesignatorReader = ReaderFactory.getReader( 116 ActionAttributeDesignator) 117 applyObj.expressions.append( 118 ActionAttributeDesignatorReader.parse(subElem, 119 common)) 120 121 elif localName == EnvironmentAttributeDesignator.ELEMENT_LOCAL_NAME: 122 EnvironmentAttributeDesignatorReader = ReaderFactory.getReader( 123 EnvironmentAttributeDesignator) 124 applyObj.expressions.append( 125 EnvironmentAttributeDesignatorReader.parse(subElem, 126 common)) 127 128 elif localName == AttributeSelector.ELEMENT_LOCAL_NAME: 129 AttributeSelectorReader = ReaderFactory.getReader( 130 AttributeSelector) 131 applyObj.expressions.append( 132 AttributeSelectorReader.parse(subElem, 133 common)) 134 135 elif localName == Condition.ELEMENT_LOCAL_NAME: 136 ConditionReader = ReaderFactory.getReader(Condition) 137 applyObj.expressions.append(ConditionReader.parse(subElem, 138 common)) 139 140 elif localName == self.__class__.FUNCTION_ELEMENT_LOCAL_NAME: 141 raise NotImplementedError('%r Apply sub-element not ' 142 'implemented', localName) 143 144 elif (localName == VariableReference.ELEMENT_LOCAL_NAME): 145 raise NotImplementedError('%r Apply sub-element not ' 146 'implemented', localName) 147 else: 148 raise XMLParseError('%r Apply sub-element not recognised', 149 localName) 150 151 return applyObj
152