Example #1
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
Example #2
0
def parseFiles(tree, channel, srcDir, verbose):
    callbacks = []
    counters = []
    formatModule = chatstatlib.formats[tree.format]
    for stat in tree.stats:
        counter, callback = stat.new()
        counters.append((stat, counter))
        callbacks.append(callback)

    files = formatModule.findChannelFiles(srcDir,
        network=channel.network, channel=channel.source)
    for fileName in files:
        if verbose:
            print "parsing", fileName,
            start = time.time()
        lineParser = formatModule.lineParser()
        chatstatlib.parseFile(fileName, lineParser, callbacks,
                              formatModule.ENCODING)
        if verbose:
            print "({0} seconds)".format(time.time() - start)
    return counters