#
# Created by Pedro Paulo de Magalhaes Oliveira Junior
# Sept, 09 at Sao Paulo, Brazil
# Convert multiple aseg files into an excel csv
# Questions: ppj at netfilter dot com dot br
#

import os
import sys
import getopt
import tokenize
import re

ListaCasos = dict();

def main():
        try:
                opts, args = getopt.getopt(sys.argv[1:], "lh")
        except getopt.error, msg:
                usage(msg)
                return 2
        if not args:
                usage("at least one file argument is required")
                return 2
        listnames = 0
        for o, a in opts:
                if o == "-h":
                        print __doc__
                        return
                if o == "-l":
                        listnames = 1
        exit = None
        for filename in args:
                x = process(filename, listnames)
                exit = exit or x
        header=0
        for LC,Caract in ListaCasos.iteritems():
                if header==0:
                        print "Case ",
                        for nome,val in Caract.iteritems():
                                print nome,
                        header=1
                        print ""
                LC = LC[LC.find('\\')+1:]
                LC = LC[:LC.find('\\')]
                Study = re.match('([A-Z]+)([0-9]+)',LC)
                print "%s%02d"%(Study.group(1),int(Study.group(2))),
                for nome,val in Caract.iteritems():
                        print "%.01f"%float(val),
                print " "
        return exit


def usage(msg):
        sys.stderr.write("%s: %s\n" % (sys.argv[0], msg))
        sys.stderr.write("Usage: %s [-l] dir fileout...\n" % sys.argv[0])
        sys.stderr.write("Try `%s -h' for more information.\n" % sys.argv[0])

def process(filename, listnames):
        if os.path.isdir(filename):
                return processdir(filename, listnames)
        f=open(filename,"r")
        column = dict()
        for line in f:
                if (line.startswith("# Measure")):
                        lll = line.split(',')
                        column[lll[1]] = lll[3]
        ListaCasos[filename]=column

                                
def processdir(dir, listnames):
        try:
                names = os.listdir(dir)
        except os.error, msg:
                sys.stderr.write("Can't list directory: %s\n" % dir)
                return 1
        files = []
        for name in names:
                fn = os.path.join(dir, name)
                if os.path.normcase(fn).endswith("aseg.stats") or os.path.isdir(fn):
                        files.append(fn)
        files.sort(lambda a, b: cmp(os.path.normcase(a), os.path.normcase(b)))
        exit = None
        for fn in files:
                x = process(fn, listnames)
                exit = exit or x     
        return exit

if __name__ == "__main__":
        sys.exit(main())

UserContributions/Scripts/ppaulo/excelICV-aseg (last edited 2008-09-09 11:35:02 by PedroOliveira)