github.com/minio/minio@v0.0.0-20240328213742-3f72439b8a27/docs/sts/client_grants/sts_element.py (about)

     1  # -*- coding: utf-8 -*-
     2  from xml.etree import cElementTree
     3  from xml.etree.cElementTree import ParseError
     4  
     5  if hasattr(cElementTree, 'ParseError'):
     6      _ETREE_EXCEPTIONS = (ParseError, AttributeError, ValueError, TypeError)
     7  else:
     8      _ETREE_EXCEPTIONS = (SyntaxError, AttributeError, ValueError, TypeError)
     9  
    10  _STS_NS = {'sts': 'https://sts.amazonaws.com/doc/2011-06-15/'}
    11  
    12  
    13  class STSElement(object):
    14      """STS aware XML parsing class. Wraps a root element name and
    15      cElementTree.Element instance. Provides STS namespace aware parsing
    16      functions.
    17  
    18      """
    19  
    20      def __init__(self, root_name, element):
    21          self.root_name = root_name
    22          self.element = element
    23  
    24      @classmethod
    25      def fromstring(cls, root_name, data):
    26          """Initialize STSElement from name and XML string data.
    27  
    28          :param name: Name for XML data. Used in XML errors.
    29          :param data: string data to be parsed.
    30          :return: Returns an STSElement.
    31          """
    32          try:
    33              return cls(root_name, cElementTree.fromstring(data))
    34          except _ETREE_EXCEPTIONS as error:
    35              raise InvalidXMLError(
    36                  '"{}" XML is not parsable. Message: {}'.format(
    37                      root_name, error.message
    38                  )
    39              )
    40  
    41      def findall(self, name):
    42          """Similar to ElementTree.Element.findall()
    43  
    44          """
    45          return [
    46              STSElement(self.root_name, elem)
    47              for elem in self.element.findall('sts:{}'.format(name), _STS_NS)
    48          ]
    49  
    50      def find(self, name):
    51          """Similar to ElementTree.Element.find()
    52  
    53          """
    54          elt = self.element.find('sts:{}'.format(name), _STS_NS)
    55          return STSElement(self.root_name, elt) if elt is not None else None
    56  
    57      def get_child_text(self, name, strict=True):
    58          """Extract text of a child element. If strict, and child element is
    59          not present, raises InvalidXMLError and otherwise returns
    60          None.
    61  
    62          """
    63          if strict:
    64              try:
    65                  return self.element.find('sts:{}'.format(name), _STS_NS).text
    66              except _ETREE_EXCEPTIONS as error:
    67                  raise InvalidXMLError(
    68                      ('Invalid XML provided for "{}" - erroring tag <{}>. '
    69                       'Message: {}').format(self.root_name, name, error.message)
    70                  )
    71          else:
    72              return self.element.findtext('sts:{}'.format(name), None, _STS_NS)
    73  
    74      def text(self):
    75          """Fetch the current node's text
    76  
    77          """
    78          return self.element.text