FANDOM



#!/usr/bin/python
# example usage
# ./logparse.py -d "../../CF/logs/CharName20171*" -o charname.id
# Input path is "../../CF/logs/CharName20171*"
# Output file is "charname.id"
# Parsing 0 files.
# 0 id's found over all files.
#!/usr/bin/python
## Takes a file glob/wildcard as input from the command line, then goes through all files
## (presumably) CF logs, and pulls out identified/lore'd items and puts them into
## a file.

import sys, getopt, os, glob

def main(argv):
    inputdir = ""
    outputfile = ""
    try:
        opts, args = getopt.getopt(argv,"hd:o:",["help", "dir=","ofile="])
    except getopt.GetoptError:
        print "parse.py -d <inputdir> -o <outputfile>"
        sys.exit(2)

    if not opts:
        print "logparse.py -d <inputdir> -o <outputfile>"
        sys.exit()

    for opt, arg in opts:
        if opt in ("-h", "--help"):
            print "logparse.py -d <inputdir> -o <outputfile>"
            sys.exit()
        elif opt in ("-d", "--dir"):
            inputdir = arg
        elif opt in ("-o", "--ofile"):
            outputfile = arg

    print "Input path is " + inputdir.strip()
    print "Output file is " + outputfile.strip()

    fileList = glob.glob(inputdir)
    print "Parsing " + str(len(fileList)) + " files."
    
    totalcount = 0
    
    for file in fileList:
        try:
            open(file, "r")
        except IOError:
            print "Cannot open input file ", file, "."
            sys.exit(2)
        fsock = open(file)
        
        try:
            open(os.path.join(os.getcwd(), outputfile))
        except IOError:
            print "Cannot open output file " + outputfile + "."
        osock = open(os.path.join(os.getcwd(), outputfile), "a")
        
        count = 0        
        #hyph = "-------------------------------------------------------------------------------\n"
	hyph = "-------------------------------------------------------------------------------"
        idline = "can be referred to as"
        prev_line = ""
        
        for line in fsock:
            if hyph in prev_line and idline in line:
                osock.write(prev_line)
                count += 1
            if count % 2 == 1:
                osock.write(line)
                if hyph in line:
                    count += 1
            prev_line = line
        
        print str(count/2) + " ids found in " + file
        totalcount += count/2
    print str(totalcount) + " ids found over all files."
                                
if __name__ == "__main__":
    main(sys.argv[1:])