#!/home/rocky/.pyenv/versions/2.5.6/bin/python
"""Print grammar reduce statistics for a series of spark-parser parses
"""
from spark_parser.version import VERSION
import getopt, os, sys
import pickle

def sort_profile_info(path, max_count=1000):

    profile_info = pickle.load(open(path, "rb"))

    # Classify unused rules. Some are "unused" because the have nullable
    # nonterminals and those show up as a different rule. Sothe rule
    # *is* used just not in the form where a nullable symbol hasn't been
    # nulled.

    # And in some cases this is intentional. Uncompyle6 creates such grammar
    # rules to ensure that positions of certain nonterminals in semantic
    # actions appear in the same place as similar grammar rules

    unused_rules = set() # just the grammar rules
    used_rules = []  # (count, grammar rule)
    for rule, count in profile_info.items():
        if count == 0:
            unused_rules.add(rule)
        else:
            used_rules.append((count, rule))

    for count, rule in used_rules:
        if rule.find('\e_') > -1:
            canonic_rule = rule.replace('\e_', '', 1000)
            if canonic_rule in unused_rules:
                unused_rules.remove(canonic_rule)
                pass
            pass
        pass

    unused_items = [(0, item) for item in sorted(unused_rules)]
    used_items = sorted(used_rules, reverse=False)
    return [item for item in unused_items + used_items
                if item[0] <= max_count]

program, ext = os.path.splitext(os.path.basename(__file__))

DEFAULT_COVERAGE_FILE = "/tmp/spark-grammar.cover",
DEFAULT_COUNT = 100
def run():

    Usage_short = """usage: %s --path coverage-file...
Type -h for for full help.""" % program


    try:
        opts, files = getopt.getopt(sys.argv[1:], 'hVp:m:',
                                    ['help', 'version', 'path=', 'max-count='])
    except getopt.GetoptError(e):
        sys.stderr.write('%s: %s\n' % (os.path.basename(sys.argv[0]), e))
        sys.exit(-1)

    max_count = DEFAULT_COUNT
    path = DEFAULT_COVERAGE_FILE
    for opt, val in opts:
        if opt in ('-h', '--help'):
            print(__doc__)
            sys.exit(1)
        elif opt in ('-V', '--version'):
            print("%s %s" % (program, VERSION))
            sys.exit(0)
        elif opt in ('-p', '--path'):
            path = val
        elif opt in ('-m', '--max_count'):
            max_count = int(val)
        else:
            print(opt)
            sys.stderr.write(Usage_short)
            sys.exit(1)


    """Print grammar reduce statistics for a series of spark-parser parses
    """
    for count, rule in sort_profile_info(path, max_count):
        print("%d: %s" % (count, rule))
        pass
    return

if __name__ == '__main__':
    run()
