示例#1
0
def _ParseResponse(response):
    """Parses an HTTP response into a dict.

  Args:
    response: A str containing the HTTP response.

  Returns:
    A dict with fields:
      body: A str containing the body.
      headers: A list containing tuples (key, value) of key and value pairs.
      response_code: An int containing the HTTP response code.
  """
    parser = feedparser.FeedParser()

    parser._set_headersonly()
    parser.feed(response)
    parsed_response = parser.close()
    if 'Status' in parsed_response:
        status = int(parsed_response['Status'].split(' ', 1)[0])
        del parsed_response['Status']
    else:
        status = 200
    return {
        'body': parsed_response.get_payload(),
        'headers': parsed_response.items(),
        'response_code': status
    }
示例#2
0
    def moveBack(self, box):
        self.imap.select(box.name)
        status, data = self.imap.uid('search', None, 'HEADER X-SNOOZE ""')
        assert (status == "OK")
        mails = data[0].decode("utf-8").split(" ")
        toDelete = []
        if mails == ['']:
            return

        for mail in mails:
            status, body = self.imap.uid('fetch', mail, '(RFC822)')
            assert (status == "OK")

            keepmail = True
            try:
                body = body[0][1].decode("utf-8")
            except UnicodeDecodeError:
                print("Problem found")
                keepmail = False
                continue

            import email.feedparser as parser
            p = parser.FeedParser()
            p.feed(body)
            email = p.close()

            if body.find("X-SNOOZE:") != -1:
                import re
                moveTime = int(re.findall(r'\d+', body)[0])
                import time
                currentTime = time.time()
                if moveTime > currentTime and keepmail:
                    remainingTime = int(moveTime - currentTime)
                    print(remainingTime)
                    remainingSeconds = remainingTime % 60
                    remainingMinutes = remainingTime % (60 * 60)
                    remainingHours = remainingTime % (60 * 60 * 60)
                    remainingDays = remainingTime

                    remainingSeconds = remainingSeconds
                    remainingMinutes = remainingMinutes / 60
                    remainingHours = remainingHours / (60 * 60)
                    remainingDays = remainingDays / (60 * 60 * 24)

                    print("# Igoring\n\"%s\" from %s" %
                          (email["Subject"], email["From"]))
                    print("Time left: %d d, %d h, %d m, %d s" %
                          (remainingDays, remainingHours, remainingMinutes,
                           remainingSeconds))
                    continue
                body = body[body.find('\n') + 1:body.rfind('\n')]
                self.imap.append("INBOX", None, None, body.encode("utf-8"))
                res = self.imap.uid('STORE', mail, '+FLAGS', '(\Deleted)')

        self.imap.expunge()
        return
示例#3
0
 def _ParseResponse(self, response):
   # Modelled after appengine/runtime/nacl/python/cgi.py
   parser = feedparser.FeedParser()
   # Set headersonly as we never want to parse the body as an email message.
   parser._set_headersonly()  # pylint: disable-msg=W0212
   parser.feed(response)
   parsed_response = parser.close()
   if 'Status' in parsed_response:
     self._status = parsed_response['Status']
     del parsed_response['Status']
   else:
     self._status = '200 OK'
   self._headers = dict(parsed_response.items())
   self._body = parsed_response.get_payload()
示例#4
0
    def _NormalResponse(self, response):
        """Generate a non-error response."""

        # Modelled after appengine/runtime/nacl/python/cgi.py
        parser = feedparser.FeedParser()
        # Set headersonly as we never want to parse the body as an email message.
        parser._set_headersonly()  # pylint: disable-msg=W0212
        parser.feed(response)
        parsed_response = parser.close()
        if 'Status' in parsed_response:
            status = parsed_response['Status']
            del parsed_response['Status']
        else:
            status = '200 OK'
        response_headers = parsed_response.items()
        self.start_response(status, response_headers)
        return parsed_response.get_payload()
示例#5
0
def _ParseResponse(response):
    """Parses an HTTP response into a dict.

  Args:
    response: A cStringIO.StringIO (StringO) containing the HTTP response.

  Returns:
    A dict with fields:
      body: A str containing the body.
      headers: A list containing tuples (key, value) of key and value pairs.
      response_code: An int containing the HTTP response code.
  """

    response.reset()
    parser = feedparser.FeedParser()

    parser._set_headersonly()

    while True:
        line = response.readline()
        if not feedparser.headerRE.match(line):
            if not feedparser.NLCRE.match(line):
                parser.feed(line)
            break
        parser.feed(line)
    parsed_response = parser.close()

    if 'Status' in parsed_response:
        status = int(parsed_response['Status'].split(' ', 1)[0])
        del parsed_response['Status']
    else:
        status = 200
    return {
        'body': parsed_response.get_payload() + response.read(),
        'headers': parsed_response.items(),
        'response_code': status
    }
示例#6
0
    def raw_info(self, pkg, chkdistro):
        if not pkg.strip():
            return ''
        _pkg = ''.join([
            x for x in pkg.strip().split(None, 1)[0]
            if x.isalnum() or x in '.-_+'
        ])
        distro = chkdistro
        if len(pkg.strip().split()) > 1:
            distro = ''.join([
                x for x in pkg.strip().split(None, 2)[1]
                if x.isalnum() or x in '-._+'
            ])
        if distro not in self.distros:
            return "%r is not a valid distribution: %s" % (distro, ", ".join(
                self.distros))

        pkg = _pkg

        try:
            data = apt_cache(self.aptdir, distro, ['show', pkg])
        except subprocess.CalledProcessError as e:
            data = e.output
        try:
            data2 = apt_cache(self.aptdir, distro, ['showsrc', pkg])
        except subprocess.CalledProcessError as e:
            data2 = e.output
        if not data or 'E: No packages found' in data:
            return 'Package %s does not exist in %s' % (pkg, distro)
        maxp = {'Version': '0~'}
        packages = [x.strip() for x in data.split('\n\n')]
        for p in packages:
            if not p.strip():
                continue
            parser = feedparser.FeedParser()
            parser.feed(p)
            p = parser.close()
            if type(p) == type(""):
                self.log.error(
                    "PackageInfo/packages: apt returned an error, do you have the deb-src URLs in %s.list?"
                    % distro)
                return "Package lookup faild"
            if not p.get("Version", None):
                continue
            if apt.apt_pkg.version_compare(maxp['Version'], p['Version']) <= 0:
                maxp = p
            del parser
        maxp2 = {'Version': '0~'}
        packages2 = [x.strip() for x in data2.split('\n\n')]
        for p in packages2:
            if not p.strip():
                continue
            parser = feedparser.FeedParser()
            parser.feed(p)
            p = parser.close()
            if type(p) == type(""):
                self.log.error(
                    "PackageInfo/packages: apt returned an error, do you have the deb-src URLs in %s.list?"
                    % distro)
                return "Package lookup faild"
            if not p['Version']:
                continue
            if apt.apt_pkg.version_compare(maxp2['Version'],
                                           p['Version']) <= 0:
                maxp2 = p
            del parser
        archs = ''
        if 'Architecture' in maxp2:
            archs = [
                _.strip() for _ in maxp2['Architecture'].split() if _.strip()
            ]
            for arch in archs:
                if arch not in ('any', 'all'):
                    continue
                else:
                    archs = ''
                    break

            if archs:
                archs = ' (Only available for %s)' % '; '.join(archs)

        maxp["Distribution"] = distro
        maxp["Architectures"] = archs
        return maxp