#!/usr/bin/env python
# vim: set fileencoding=utf-8 :
# Hvad er den mest populære holdning blandt logens brødre?

import simplejson as json
from os import environ, path
from sys import exit
import sys
from operator import attrgetter

def main():
    datafile = path.join(environ['CONCIEGGS_DB_DIR'], 'meningstats')

    if not path.exists(datafile):
        f = open(datafile, 'w+')
        f.write('{}')
        f.close()

    dbfile = open(datafile, 'r')
    db = json.load(dbfile)
    dbfile.close()

    topMeninger = sorted(db.items(), key=lambda e: e[1]['gennemsnit'], reverse=True)[0:5]
    out = "TopMeninger: "
    p = 1
    for (_,mening) in topMeninger:
        out += "#%d: %s (%.3f/10)" % (p, mening['kortNavn'], mening['gennemsnit'])
        if p != 5: out += ", "
        else: out += "."
        p += 1

    print out

if __name__ == '__main__':
    main()
