# Path: stagehand/toolbox/net.py
# @asyncio.coroutine
# def download(url, target=None, resume=True, retry=0, progress=None, noraise=True, **kwargs):
#     while retry >= 0:
#         status = 0
#         try:
#             status, response = yield from _download(url, target, resume, progress, **kwargs)
#             return status, response
#         except (aiohttp.HttpException, aiohttp.ConnectionError, OSError) as e:
#             if isinstance(e, aiohttp.HttpException):
#                 status = e.code
#             if status == 416 and resume:
#                 # Server reported range not satisfiable and we're trying to
#                 # resume.  Retry again without resumption without counting
#                 # against the retry counter.
#                 resume = False
#                 continue
#             elif retry == 0:
#                 # Done retrying, reraise this exception.
#                 if noraise:
#                     return 0, str(e)
#                 else:
#                     raise
#             errmsg = str(e)
# 
#         if status != 0:
#             errmsg = 'status %d' % status
#             if status < 500 or status >= 600:
#                 # Not a temporary error that we can retry.  We're done.
#                 return status, None
# 
#         log.warning('download failed (%d retries left): %s', retry, errmsg)
#         retry -= 1
# 
#     # We shouldn't actually ever get here.
#     log.warning('BUG: download retry loop did not terminate properly')
#     return 0, None
# 
# Path: stagehand/utils.py
# class ElementParser(xml.sax.ContentHandler):
#     """
#     Handler for the SAX parser. The member function 'handle' will be
#     called everytime an element given on init is closed. The parameter
#     is the tree with this element as root. An element can either have
#     children or text content. The ElementParser is usefull for simple
#     xml files found in config files and information like epg data.
#     """
#     def __init__(self, *names):
#         """
#         Create handler with a list of element names.
#         """
#         self._names = names
#         self._elements = []
#         self.attr = {}
# 
#     def startElement(self, name, attr):
#         """
#         SAX callback
#         """
#         element = Element(name)
#         element._attr = dict(attr)
#         if len(self._elements):
#             self._elements[-1].append(element)
#         else:
#             self.attr = dict(attr)
#         self._elements.append(element)
# 
#     def endElement(self, name):
#         """
#         SAX callback
#         """
#         element = self._elements.pop()
#         element._content = element._content.strip()
#         if name in self._names or (not self._names and len(self._elements) == 1):
#             self.handle(element)
#         if not self._elements:
#             self.finalize()
# 
#     def characters(self, c):
#         """
#         SAX callback
#         """
#         if len(self._elements):
#             self._elements[-1]._content += c
# 
#     def handle(self, element):
#         """
#         ElementParser callback for a complete element.
#         """
#         pass
# 
#     def finalize(self):
#         """
#         ElementParser callback at the end of parsing.
#         """
#         pass


import xml.sax
import asyncio
from ..toolbox.net import download
from ..utils import ElementParser


@asyncio.coroutine
def parse_xml(what, nest=[]):
    results = []
    def handle(element):
        info = {}
        attrs = dict((a, getattr(element, a)) for a in element.attributes)

# Path: stagehand/toolbox/net.py
# @asyncio.coroutine
# def download(url, target=None, resume=True, retry=0, progress=None, noraise=True, **kwargs):
#     while retry >= 0:
#         status = 0
#         try:
#             status, response = yield from _download(url, target, resume, progress, **kwargs)
#             return status, response
#         except (aiohttp.HttpException, aiohttp.ConnectionError, OSError) as e:
#             if isinstance(e, aiohttp.HttpException):
#                 status = e.code
#             if status == 416 and resume:
#                 # Server reported range not satisfiable and we're trying to
#                 # resume.  Retry again without resumption without counting
#                 # against the retry counter.
#                 resume = False
#                 continue
#             elif retry == 0:
#                 # Done retrying, reraise this exception.
#                 if noraise:
#                     return 0, str(e)
#                 else:
#                     raise
#             errmsg = str(e)
# 
#         if status != 0:
#             errmsg = 'status %d' % status
#             if status < 500 or status >= 600:
#                 # Not a temporary error that we can retry.  We're done.
#                 return status, None
# 
#         log.warning('download failed (%d retries left): %s', retry, errmsg)
#         retry -= 1
# 
#     # We shouldn't actually ever get here.
#     log.warning('BUG: download retry loop did not terminate properly')
#     return 0, None
# 
# Path: stagehand/utils.py
# class ElementParser(xml.sax.ContentHandler):
#     """
#     Handler for the SAX parser. The member function 'handle' will be
#     called everytime an element given on init is closed. The parameter
#     is the tree with this element as root. An element can either have
#     children or text content. The ElementParser is usefull for simple
#     xml files found in config files and information like epg data.
#     """
#     def __init__(self, *names):
#         """
#         Create handler with a list of element names.
#         """
#         self._names = names
#         self._elements = []
#         self.attr = {}
# 
#     def startElement(self, name, attr):
#         """
#         SAX callback
#         """
#         element = Element(name)
#         element._attr = dict(attr)
#         if len(self._elements):
#             self._elements[-1].append(element)
#         else:
#             self.attr = dict(attr)
#         self._elements.append(element)
# 
#     def endElement(self, name):
#         """
#         SAX callback
#         """
#         element = self._elements.pop()
#         element._content = element._content.strip()
#         if name in self._names or (not self._names and len(self._elements) == 1):
#             self.handle(element)
#         if not self._elements:
#             self.finalize()
# 
#     def characters(self, c):
#         """
#         SAX callback
#         """
#         if len(self._elements):
#             self._elements[-1]._content += c
# 
#     def handle(self, element):
#         """
#         ElementParser callback for a complete element.
#         """
#         pass
# 
#     def finalize(self):
#         """
#         ElementParser callback at the end of parsing.
#         """
#         pass


# Path: stagehand/providers/base.py
import xml.sax
import asyncio
from ..toolbox.net import download
from ..utils import ElementParser



@asyncio.coroutine
def parse_xml(what, nest=[]):
    results = []
    def handle(element):
        info = {}
        attrs = dict((a, getattr(element, a)) for a in element.attributes)

        if element.content: