def parse_and_get_ns(file):
  '''Use ElementTree to parse an XML file, but retain namespace information.
  
  Args:
    file: XML file to parse.
  
  Returns:
    Tuple with this structure: (ElementTree, namespace dict)
    
  Raises:
    KeyError: A duplicate prefix was found that has a different
      URI.  This is perfectly valid XML, but shows how this parser
      is deficient.
  '''
  events = "start", "start-ns"
  root = None
  ns = {}
  for event, elem in ET.iterparse(file, events):
    if event == "start-ns":
      if elem[0] in ns and ns[elem[0]] != elem[1]:
        # NOTE: It is perfectly valid to have the same prefix refer
        #   to different URI namespaces in different parts of the
        #   document. This exception serves as a reminder that this
        #   solution is not robust.  Use at your own peril.
        raise KeyError("Duplicate prefix with different URI found.")
      ns[elem[0]] = "{%s}" % elem[1]
    elif event == "start":
      if root is None:
        root = elem
  return (ET.ElementTree(root), ns)
  
###
### Project specific utility functions
###
dataset/ETHPy150Open DeanCutlet/filestack/util.py/parse_and_get_ns/original