Ignore:
Timestamp:
04/08/09 12:56:47 (11 years ago)
Author:
pjkersha
Message:
  • Started adding ElementTree based parsers for SAML classes in ndg.security.common.saml.xml.etree.
  • ndg.security.common.utils.prettyPrint needs a bug fix for namespace declarations
File:
1 edited

Legend:

Unmodified
Added
Removed
  • TI12-security/trunk/python/ndg.security.common/ndg/security/common/saml/xml/__init__.py

    r5538 r5554  
    2727__contact__ = "Philip.Kershaw@stfc.ac.uk" 
    2828__revision__ = "$Id$" 
     29from datetime import datetime 
     30try: 
     31    from datetime import strptime 
     32except ImportError: 
     33    # Allow for Python < 2.5 
     34    from time import strptime as _strptime 
     35    strptime = lambda datetimeStr, format: datetime(*(_strptime(datetimeStr,  
     36                                                                format)[0:6])) 
    2937 
    3038class XMLConstants(object): 
     
    132140    #  Liberty PAOS QName prefix. 
    133141    PAOS_PREFIX = "paos" 
    134  
    135142     
    136143    #    SAML 1.X 
     
    185192        "urn:oasis:names:tc:SAML:1.0:bindings:SOAP-binding" 
    186193     
    187      
    188194    #    SAML 2.0 
    189195     
     
    298304    SAML2_SOAP11_BINDING_URI = "urn:oasis:names:tc:SAML:2.0:bindings:SOAP" 
    299305 
     306class XMLObjectError(Exception): 
     307    pass 
     308 
     309class XMLObjectParsingError(Exception): 
     310    pass 
    300311 
    301312class XMLObject(object): 
    302313    """Abstract base class for XML representations of SAML objects""" 
    303314     
    304     def create(self, samlObject, makeNsDeclaration=True): 
    305         """Create an XML representation from the input SAML object""" 
     315    def create(self, samlObject): 
     316        """Create an XML representation from the input SAML object 
     317        @type samlObject: SAMLObject 
     318        param samlObject: SAML object to render into XML 
     319        """ 
    306320        raise NotImplementedError() 
    307321 
    308     def parse(self, source): 
     322    def parse(self, elem): 
    309323        """Parse into XML representation 
    310         @type source: basestring/file 
    311         @param source: file object or string content containing XML 
     324        @type elem: object 
     325        @param elem: XML object - type depends on XML class representation 
     326        @rtype: SAMLObject 
     327        @return: equivalent SAML object 
     328        @raise XMLObjectParsingError: error parsing content into SAML  
     329        representation 
    312330        """ 
    313331        raise NotImplementedError() 
     
    333351        @return: issue instance as a string 
    334352        """ 
     353        if not isinstance(dtIssueInstant, datetime): 
     354            raise TypeError("Expecting datetime type for string conversion, " 
     355                            "got %r" % dtIssueInstant) 
     356             
    335357        return dtIssueInstant.strftime(IssueInstantXMLObject.issueInstantFmt) 
    336358 
     359    @classmethod 
     360    def str2Datetime(cls, issueInstant): 
     361        """Convert issue instant string to datetime type 
     362        @type issueInstant: basestring 
     363        @param issueInstant: issue instance as a string 
     364        @rtype: datetime.datetime 
     365        @return: issue instance as a datetime 
     366        """ 
     367        if not isinstance(issueInstant, basestring): 
     368            raise TypeError("Expecting basestring derived type for string " 
     369                            "conversion, got %r" % issueInstant) 
     370             
     371        return datetime.strptime(issueInstant,  
     372                                 IssueInstantXMLObject.issueInstantFmt) 
Note: See TracChangeset for help on using the changeset viewer.