示例#1
0
def main():
    opts, args = options.parse_args()
    denominatorFile = args[0]
    numeratorFile = args[1]
    outFile = opts.outfile

    denominators = {}
    numerators = {}
    ratios = {}

    with open(denominatorFile) as df:
        df.next()  # Get rid of header
        for line in df:
            line = line.rstrip("\r\n")
            rank, name, count = line.split(" ")
            denominators[name] = int(count)
    with open(numeratorFile) as nf:
        nf.next()  # Get rid of header
        for line in nf:
            line = line.rstrip("\r\n")
            rank, name, count = line.split(" ")
            numerators[name] = float(count)
    for name, count in denominators.items():
        if name in numerators:
            ratios[name] = numerators[name] / count

    rankings = counterToRank(ratios)
    if outFile == "-":
        rankToFile(sys.stdout, rankings)
    else:
        with open(outFile) as out:
            rankToFile(out, rankings)
示例#2
0
def main():
    opts, args = options.parse_args()
    if opts.listStats:
        for name in sorted(chatstatlib.counters):
            print name
        return 0
    if not args:
        print "No file names given!"
        return 1
    if opts.format not in chatstatlib.formats:
        print "unimplemented log format", opts.format
        return 1
    if opts.stat not in chatstatlib.counters:
        print "unimplemented stat type", opts.stat
        return 1
    if opts.out == "-":
        outfile = sys.stdout
    else:
        outfile = open(os.path.expanduser(opts.out), "w")

    # Choose log file format (irssi, weechat, bip, etc.)
    formatModule = chatstatlib.formats[opts.format]

    # Choose a search regexp based on --channel
    if opts.channelsOnly:
        fregexp = formatModule.LOG_CHANNEL_NAME_RE
    else:
        fregexp = formatModule.LOG_FILE_NAME_RE
    fileNames = sorted(chatstatlib.findFiles(args, fregexp))
    if not fileNames:
        print "no log files found"
        return 1

    lineParserGen = formatModule.lineParser  # one generator per file
    counter, sink = chatstatlib.counters[opts.stat]()
    messageCallbacks = [sink]
    for fileName in fileNames:
        if opts.verbose:
            print "processing", fileName,
            start = time.time()
        lineParser = formatModule.lineParser()
        chatstatlib.parseFile(fileName, lineParser, messageCallbacks, formatModule.ENCODING)
        if opts.verbose:
            print "({0:.3} seconds)".format(time.time() - start)

    rank = chatstatlib.counterToRank(counter)
    if opts.limit:
        iterable = itertools.islice(rank, opts.limit)
    else:
        iterable = rank

    print >> outfile, "rank nick count"
    for i, (nick, count) in zip(itertools.count(1), iterable):
        print >> outfile, i, nick, count
示例#3
0
def writeTarget(destDir, target, counters):
    for stat, counter in counters:
        ranked = counterToRank(counter)
        for limit in stat.limits:
            if limit == 0:
                flatName = formatDestination(destDir, target, stat, "txt")
                jsonName = formatDestination(destDir, target, stat, "json")
                segment = ranked
            else:
                flatName = formatDestination(destDir, target, stat,
                                             "txt", limit=limit)
                jsonName = formatDestination(destDir, target, stat,
                                             "json", limit=limit)
                segment = ranked[:limit]
            with codecs.open(flatName, 'w', 'utf8', 'replace') as f:
                os.chmod(flatName, 0755)
                rankToFile(f, segment)
            with codecs.open(jsonName, 'w', 'utf8', 'replace') as f:
                os.chmod(jsonName, 0755)
                rankToJSON(f, segment)