import glob
import os
import pickle
import bz2

def load_token(token_file):
    with bz2.open(token_file, "rt") as f:
        s = f.read()

    import json
    res = json.loads(s)

    def pres(res):
        for q, qval in res['details'].items():
            # print(qval)
            for k, v in qval['items'].items():
                # for k in qval:
                print(q, k, v)

    if False:
        sources = res['sources']
        l1 = list(set( [k.split("\\")[-1] for k in sources] ))
        for dl in l1:
            t = 0
            lines = 0
            for k, s in res['sources'].items():
                kk = k.split('\\')
                if kk[-1] != dl:
                    continue
                t += len(s)
                l = len(s.splitlines())
                lines += l
                # print(k, len(s), l)
            print(dl, "total", lines)

    return res

if __name__ == "__main__":
    import irlc
    import irlc.assignments.assignment_fruit as fa
    fa.__file__


    dn = os.path.dirname(fa.__file__ )

    l = glob.glob(dn+"/*fruit*.token")
    token_file = l[0]
    print(token_file)

    rs = load_token(token_file)

    a = 3455