source: TI12-security/trunk/python/ndg_security_common/ndg/security/common/saml_utils/esg/xml/etree.py @ 6069

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg-security/TI12-security/trunk/python/ndg_security_common/ndg/security/common/saml_utils/esg/xml/etree.py@6069
Revision 6069, 5.6 KB checked in by pjkersha, 11 years ago (diff)

Re-release as rc1

Line 
1"""SAML 2.0 Earth System Grid Group/Role ElementTree representation
2
3NERC DataGrid Project
4"""
5__author__ = "P J Kershaw"
6__date__ = "09/11/09"
7__copyright__ = "(C) 2009 Science and Technology Facilities Council"
8__license__ = "BSD - see LICENSE file in top-level directory"
9__contact__ = "Philip.Kershaw@stfc.ac.uk"
10__revision__ = '$Id: $'
11import logging
12log = logging.getLogger(__name__)
13import re
14
15from xml.etree import ElementTree
16
17from saml.xml import XMLTypeParseError, UnknownAttrProfile
18from saml.xml.etree import AttributeValueElementTreeBase, QName
19
20from ndg.security.common.saml_utils.esg import XSGroupRoleAttributeValue
21
22
23class XSGroupRoleAttributeValueElementTree(AttributeValueElementTreeBase,
24                                           XSGroupRoleAttributeValue):
25    """ElementTree XML representation of Earth System Grid custom Group/Role
26    Attribute Value""" 
27
28    @classmethod
29    def toXML(cls, attributeValue):
30        """Create an XML representation of the input SAML ESG Group/Role type
31        Attribute Value
32       
33        @type assertion: saml.saml2.core.XSGroupRoleAttributeValue
34        @param assertion: XSGroupRoleAttributeValue to be represented as an
35        ElementTree Element
36        @rtype: ElementTree.Element
37        @return: ElementTree Element
38        """
39        elem = AttributeValueElementTreeBase.toXML(attributeValue)
40       
41        if not isinstance(attributeValue, XSGroupRoleAttributeValue):
42            raise TypeError("Expecting %r type; got: %r" % 
43                            (XSGroupRoleAttributeValue, type(attributeValue)))
44           
45        ElementTree._namespace_map[attributeValue.namespaceURI
46                                   ] = attributeValue.namespacePrefix
47                                   
48        tag = str(QName.fromGeneric(cls.TYPE_NAME))   
49        groupRoleElem = ElementTree.Element(tag)
50        ElementTree._namespace_map[cls.DEFAULT_ELEMENT_NAME.namespaceURI
51                                   ] = cls.DEFAULT_ELEMENT_NAME.prefix
52       
53        groupRoleElem.set(cls.GROUP_ATTRIB_NAME, attributeValue.group)
54        groupRoleElem.set(cls.ROLE_ATTRIB_NAME, attributeValue.role)
55
56        elem.append(groupRoleElem)
57       
58        return elem
59
60    @classmethod
61    def fromXML(cls, elem):
62        """Parse ElementTree ESG Group/Role attribute element into a SAML
63        XSGroupRoleAttributeValue object
64       
65        @type elem: ElementTree.Element
66        @param elem: Attribute value as ElementTree XML element
67        @rtype: saml.saml2.core.XSGroupRoleAttributeValue
68        @return: SAML ESG Group/Role Attribute value
69        """
70       
71        # Update namespace map for the Group/Role type referenced. 
72        ElementTree._namespace_map[cls.DEFAULT_NS] = cls.DEFAULT_PREFIX
73       
74        if not ElementTree.iselement(elem):
75            raise TypeError("Expecting %r input type for parsing; got %r" %
76                            (ElementTree.Element, elem))
77
78        localName = QName.getLocalPart(elem.tag)
79        if localName != cls.DEFAULT_ELEMENT_LOCAL_NAME:
80            raise XMLTypeParseError("No \"%s\" element found" %
81                                    cls.DEFAULT_ELEMENT_LOCAL_NAME)
82                                   
83        # Check for group/role child element
84        if len(elem) == 0:
85            raise XMLTypeParseError('Expecting "%s" child element to "%s" '
86                                    'element' % (cls.TYPE_LOCAL_NAME,
87                                               cls.DEFAULT_ELEMENT_LOCAL_NAME))
88       
89        childElem = elem[0]
90        childLocalName = QName.getLocalPart(childElem.tag)
91        if childLocalName != cls.TYPE_LOCAL_NAME:
92            raise XMLTypeParseError("No \"%s\" element found" %
93                                    cls.TYPE_LOCAL_NAME)
94
95                                     
96        attributeValue = XSGroupRoleAttributeValue()
97        groupName = childElem.attrib.get(cls.GROUP_ATTRIB_NAME)
98        if groupName is None:
99            raise XMLTypeParseError('No "%s" attribute found in Group/Role '
100                                    'attribute element' % 
101                                    cls.GROUP_ATTRIB_NAME)
102        attributeValue.group = groupName
103       
104        roleName = childElem.attrib.get(cls.ROLE_ATTRIB_NAME)
105        if roleName is None:
106            raise XMLTypeParseError('No "%s" attribute found in Group/Role '
107                                    'attribute element' % 
108                                    cls.GROUP_ATTRIB_NAME)
109        attributeValue.role = roleName
110
111        return attributeValue
112   
113    @classmethod
114    def factoryMatchFunc(cls, elem):
115        """Match function used by AttributeValueElementTreeFactory to
116        determine whether the given attribute is XSGroupRole type
117       
118        @type elem: ElementTree.Element
119        @param elem: Attribute value as ElementTree XML element
120        @rtype: saml.saml2.core.XSGroupRoleAttributeValue or None
121        @return: SAML ESG Group/Role Attribute Value class if elem is an
122        Group/role type element or None if if doesn't match this type
123        """
124       
125        # Group/role element is a child of the AttributeValue element
126        if len(elem) == 0:
127            return None
128       
129        childLocalName = QName.getLocalPart(elem[0].tag)
130        if childLocalName != cls.TYPE_LOCAL_NAME:
131            raise XMLTypeParseError('No "%s" child element found in '
132                                    'AttributeValue' % cls.TYPE_LOCAL_NAME)
133               
134        if cls.GROUP_ATTRIB_NAME in elem[0].attrib and \
135           cls.ROLE_ATTRIB_NAME in elem[0].attrib:
136            return cls
137
138        return None
139
Note: See TracBrowser for help on using the repository browser.