#!/usr/bin/python
# encoding=UTF-8

# refresh-pyflakes-tags -- Refresh Lintian data about pyflakes tags
#
# Copyright © 2011, 2012, 2013 Jakub Wilk
#
# This program is free software.  It is distributed under the terms of the GNU
# General Public License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along with
# this program.  If not, you can find it on the World Wide Web at
# http://www.gnu.org/copyleft/gpl.html, or write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.

import os
import re

import debian.deb822 as deb822

import pyflakes.messages as messages

def main():
    LINTIAN_ROOT = os.getenv('LINTIAN_ROOT') or '.'
    tags = set()
    for cls in messages.Message.__subclasses__():
        tag = re.sub('[A-Z]', lambda m: '-' + m.group(0).lower(), cls.__name__)
        tags.add('pyflakes' + tag)
    t = 'pyflakes-undefined-name'
    if t in tags:
        tags.add(t + '-underscore')
    del t
    old_tags = {}
    first = True
    with open('{root}/checks/python/pyflakes.desc'.format(root=LINTIAN_ROOT), 'r') as file:
        for para in deb822.Deb822.iter_paragraphs(file):
            if first:
                first_para = para
            else:
                old_tags[para['tag']] = para
            first = False
    first = True
    with open('{root}/checks/python/pyflakes.desc'.format(root=LINTIAN_ROOT), 'w') as file:
        file.write(unicode(first_para).encode('UTF-8'))
        for tag in sorted(tags):
            try:
                para = old_tags[tag]
            except LookupError:
                para = deb822.Deb822()
                para['Tag'] = tag
                para['Severity'] = 'wishlist'
                para['Certainty'] = 'wild-guess'
                para['Experimental'] = 'yes'
            file.write('\n')
            file.write(unicode(para).encode('UTF-8'))
            first = False

if __name__ == '__main__':
    main()

# Local Variables:
# indent-tabs-mode: nil
# End:
# vim: syntax=python sw=4 sts=4 sr et
