#!/usr/bin/env python

"""
A utility to fix PLY-generated lex and yacc tables to be Python 2 and
3 compatible.
"""

import os

for root, dirs, files in os.walk(os.path.join(os.path.dirname(__file__), '..')):
    for fname in files:
        if not (fname.endswith('lextab.py') or fname.endswith('parsetab.py')):
            continue

        path = os.path.join(root, fname)
        with open(path, 'rb') as fd:
            lines = fd.readlines()

        with open(path, 'wb') as fd:
            fd.write("# Licensed under a 3-clause BSD style license - see LICENSE.rst\n")
            fd.write("from __future__ import (absolute_import, division, print_function, unicode_literals)\n")
            fd.write('\n')

            lines = [x.replace("u'", "'").replace('u"', '"') for x in lines]
            lines = [x for x in lines if not (fname in x and x[0] == '#')]

            fd.write(''.join(lines))

@@ -112,98 +112,291 @@
 %22%22%22%0A
-%0Aimport os%0A%0Afor root, dirs, files in os.walk(os.path.join(os.path.dirname(__file__), '..')
+from __future__ import print_function%0A%0Aimport os%0Aimport argparse%0A%0Aparser = argparse.ArgumentParser(description=__doc__)%0Aparser.add_argument('dir', help='The directory to search through for lextab/parsetab files.')%0Aargs = parser.parse_args()%0A%0Afor root, dirs, files in os.walk(args.dir
 ):%0A 
@@ -560,16 +560,60 @@
  fname)%0A
+        print('Found file', path, 'to fix')%0A
         
@@ -706,32 +706,187 @@
 h, 'wb') as fd:%0A
+            if not lines%5B0%5D.startswith(%22# Licensed under a 3-clause BSD style license - see LICENSE.rst%22):%0A                print('Re-writing license')%0A    
             fd.w
@@ -955,24 +955,28 @@
 NSE.rst%5Cn%22)%0A
+    
             
@@ -1070,24 +1070,28 @@
 terals)%5Cn%22)%0A
+    
             

make clean_parse_tables.py a bit more robust for tools usage