
from DebianChangesBot import MailParser

class SecurityAnnounce(MailParser):

    def parse(self, msg):
        if self._get_header(msg, 'List-Id') != '<debian-security-announce.lists.debian.org>':
            return False

        fmt = SecurityAnnounceFormatter()

        data = {
            'dsa_number' : None,
            'package' : None,
            'problem' : None,
            'year' : None,
            'url' : None,
        }

        m = re.match(r'^\[SECURITY\] \[DSA ([-\d]+)\] New (.*?) packages fix (.*)$', self._get_header(msg, 'Subject'))
        if m:
            fmt.dsa_number = m.group(1)
            fmt.package = m.group(2)
            fmt.problem = m.group(3)
        else:
            return False

        m = re.match(r'.*(20\d\d)', self._get_header(msg, 'Date'))
        if m:
            data['year'] = m.group(1)
        else:
            return False

        data['url'] = "http://www.debian.org/security/%s/dsa-%s" % (data['year'], re.sub(r'-\d+$', '', data['dsa_number']))
        data = self._tidy_data(data)

        for k, v in data.iteritems(): data[k] = str(v.decode('ascii'))
        return colourise(_("[red][Security][reset] ([yellow]DSA-%(dsa_number)s[reset]) - New [green]%(package)s[reset] packages fix %(problem)s. %(url)s") % data)


from DebianChangesBot import MailParser

class SecurityAnnounce(MailParser):

    def parse(self, msg):
        if self._get_header(msg, 'List-Id') != '<debian-security-announce.lists.debian.org>':
            return None

        fmt = SecurityAnnounceFormatter()

        m = re.match(r'^\[SECURITY\] \[DSA ([-\d]+)\] New (.*?) packages fix (.*)$', self._get_header(msg, 'Subject'))
        if m:
            fmt.dsa_number = m.group(1)
            fmt.package = m.group(2)
            fmt.problem = m.group(3)

        m = re.match(r'.*(20\d\d)', self._get_header(msg, 'Date'))
        if m:
            fmt.year = m.group(1)

        return fmt

Make formatter example more realistic
Make formatter example more realistic

Signed-off-by: Chris Lamb <711c73f64afdce07b7e38039a96d2224209e9a6c@chris-lamb.co.uk>

  
  from DebianChangesBot import MailParser
  
  class SecurityAnnounce(MailParser):
  
      def parse(self, msg):
          if self._get_header(msg, 'List-Id') != '<debian-security-announce.lists.debian.org>':
-             return False
+             return None
  
          fmt = SecurityAnnounceFormatter()
- 
-         data = {
-             'dsa_number' : None,
-             'package' : None,
-             'problem' : None,
-             'year' : None,
-             'url' : None,
-         }
  
          m = re.match(r'^\[SECURITY\] \[DSA ([-\d]+)\] New (.*?) packages fix (.*)$', self._get_header(msg, 'Subject'))
          if m:
              fmt.dsa_number = m.group(1)
              fmt.package = m.group(2)
              fmt.problem = m.group(3)
-         else:
-             return False
  
          m = re.match(r'.*(20\d\d)', self._get_header(msg, 'Date'))
          if m:
-             data['year'] = m.group(1)
+             fmt.year = m.group(1)
-         else:
-             return False
  
+         return fmt
-         data['url'] = "http://www.debian.org/security/%s/dsa-%s" % (data['year'], re.sub(r'-\d+$', '', data['dsa_number']))
-         data = self._tidy_data(data)
  
-         for k, v in data.iteritems(): data[k] = str(v.decode('ascii'))
-         return colourise(_("[red][Security][reset] ([yellow]DSA-%(dsa_number)s[reset]) - New [green]%(package)s[reset] packages fix %(problem)s. %(url)s") % data)
- 
Make formatter example more realistic
## Code Before:

from DebianChangesBot import MailParser

class SecurityAnnounce(MailParser):

    def parse(self, msg):
        if self._get_header(msg, 'List-Id') != '<debian-security-announce.lists.debian.org>':
            return False

        fmt = SecurityAnnounceFormatter()

        data = {
            'dsa_number' : None,
            'package' : None,
            'problem' : None,
            'year' : None,
            'url' : None,
        }

        m = re.match(r'^\[SECURITY\] \[DSA ([-\d]+)\] New (.*?) packages fix (.*)$', self._get_header(msg, 'Subject'))
        if m:
            fmt.dsa_number = m.group(1)
            fmt.package = m.group(2)
            fmt.problem = m.group(3)
        else:
            return False

        m = re.match(r'.*(20\d\d)', self._get_header(msg, 'Date'))
        if m:
            data['year'] = m.group(1)
        else:
            return False

        data['url'] = "http://www.debian.org/security/%s/dsa-%s" % (data['year'], re.sub(r'-\d+$', '', data['dsa_number']))
        data = self._tidy_data(data)

        for k, v in data.iteritems(): data[k] = str(v.decode('ascii'))
        return colourise(_("[red][Security][reset] ([yellow]DSA-%(dsa_number)s[reset]) - New [green]%(package)s[reset] packages fix %(problem)s. %(url)s") % data)

## Instruction:
Make formatter example more realistic
## Code After:

from DebianChangesBot import MailParser

class SecurityAnnounce(MailParser):

    def parse(self, msg):
        if self._get_header(msg, 'List-Id') != '<debian-security-announce.lists.debian.org>':
            return None

        fmt = SecurityAnnounceFormatter()

        m = re.match(r'^\[SECURITY\] \[DSA ([-\d]+)\] New (.*?) packages fix (.*)$', self._get_header(msg, 'Subject'))
        if m:
            fmt.dsa_number = m.group(1)
            fmt.package = m.group(2)
            fmt.problem = m.group(3)

        m = re.match(r'.*(20\d\d)', self._get_header(msg, 'Date'))
        if m:
            fmt.year = m.group(1)

        return fmt
