5/19/2010

Gource et Dokuwiki : en couleur

ça y est suite aux différentes demandes, je viens d'ajouter la coloration de la représentation Gource. Vous pouvez donc télécharger la nouvelle version.

#!/bin/python

"""
This program parse logs of a dokuwiki
and tranform them for gource (a log viewer)
http://code.google.com/p/gource/

developped by WolverineX02
site : http://wolverinex02.blogspot.com

"""
import glob
import os.path
import getopt
import sys
import re

WHITE = "11FFAA"
GREEN = "00F000"
vector = (1,10,100)
start_page_name = "start"

def RGBToHTMLColor(rgb_tuple):
    """ convert an (R, G, B) tuple to #RRGGBB """
    hexcolor = '#%02x%02x%02x' % rgb_tuple
    # that's it! '%02x' means zero-padded, 2-digit hex values
    return hexcolor

def HTMLColorToRGB(colorstring):
    """ convert #RRGGBB to an (R, G, B) tuple """
    colorstring = colorstring.strip()
    if colorstring[0] == '#': colorstring = colorstring[1:]
    if len(colorstring) != 6:
        raise ValueError, "input #%s is not in #RRGGBB format" % colorstring
    r, g, b = colorstring[:2], colorstring[2:4], colorstring[4:]
    r, g, b = [int(n, 16) for n in (r, g, b)]
    return (r, g, b)

def colormodify(colorstring):
    rgb_tuple = HTMLColorToRGB(colorstring)
    r, g, b = (rgb_tuple[0]+vector[0]) % 255,(rgb_tuple[1]+vector[1]) % 255,(rgb_tuple[2]+vector[2]) % 255
    return RGBToHTMLColor((r, g, b))

def listdirectory(path,color):
    l = glob.glob(path+"/*")
    for i in l:
        if os.path.isdir(i):
                listdirectory(i,colormodify(color))
        else:
                readfile(i,color)

def listdirectory2(path):
    """list all the files like *.changes,
       read them and output them in gource's log syntax
    """
    for root, dirs, files in os.walk(path):
        for i in files:
            if  (re.search('\.changes$', i)):
                fichier = os.path.join(root, i)
                readfile(fichier,GREEN)

def readfile(fichier,color):
    """read the file and output for each line of this
       file a log line for Gource
    """

    myfile = open(fichier, 'r')
    for line in myfile.readlines():
        mots = line.split('\t')
        if len(mots)>=6:
            resultat = mots[0] + "|"
            if mots[4] == '':
                mots[4]  = 'Anonymous'
            resultat += mots[4] + "|"
            resultat += translate(mots[2]) + "|"
            resultat += mots[3].replace(':', '/')
            if mots[3].rfind(start_page_name) == len(mots[3])-len(start_page_name):
                resultat += "|" + color
            else:
                resultat += "|" + colormodify(color)
            print resultat
    myfile.close()


def translate(mot):
    """translate the dokuwiki vocabulary to the gource one
       C (also cc and sc from discussion plugin) ->A
       E (also ec from discussion plugin) -> M
       D (also dc and hc from discssion plugin) -> D
       other -> M
    """
    if mot.upper == "C" or mot == 'cc' or mot == 'sc':
        return "A"
    elif mot.upper == "E" or mot == 'ec':
        return "M"
    elif mot.upper == "D" or mot == 'dc' or mot == 'hc':
        return "D"
    else:
        return "M"

def main(argv):
    """principal function
    """
    try:
        opts, args = getopt.getopt(argv, "hd:", ["help", "dokuwiki="])
    except getopt.GetoptError:
        usage()
        sys.exit(2)
    for opt, arg in opts:
        if opt in ("-h","--help"):
            usage()
            sys.exit()
        elif opt in ("-d","--dokuwiki"):
            print listdirectory(arg,WHITE)


def usage():
    """this function will display how to use this script
    """
    print "This script will output change logs of a dokuwiki"
    print "in a friendly way for gource"
    print "how to use it :"
    print "python gourcedoku.py -d ~/Sites/MyDokuwiki/ | sort > dokusort.log"
    print "and then :"
    print "gource --log-format custom dokusort.log --stop-position 1.0 \ "
    print "--stop-on-idle --file-idle-time 10000000"
    print "---"
    print "-h : help "
    print "-d : meta directory of your dokuwiki"

if __name__ == "__main__":
    main(sys.argv[1:])