import re
import Gnumeric

def estadistica_log (fichero):
    f = open(fichero)

    datos = {}

    for linea in f:
        encontrado = re.findall("([^ ]+) - .+? \[([^:]+)",linea)
        ip,fecha = encontrado[0]

        if datos.has_key(fecha) :
            d_ip = datos[fecha]
            if d_ip.has_key(ip):
                d_ip[ip] += 1
            else :
                d_ip[ip] = 1
            
            datos[fecha] = d_ip
        else :
            datos[fecha]=dict([(ip,1)])

    col = 0
    fila = 0

    print datos

    llaves = datos.keys()

    w = Gnumeric.workbooks()[0]
    s = w.sheets()[0]
    
    for fecha in llaves:

        c = s.cell_fetch(col,fila)
        c.set_text(str(fecha))

        fila += 1
        for ip in datos[fecha].keys():
            c = s.cell_fetch(col,fila)
            c.set_text( str(ip) )
        c = s.cell_fetch(col+1,fila)
        c.set_text(str(datos[fecha][ip]))
            fila += 1

funciones_functions = {
    'estadistica_log': estadistica_log
}
