#!/usr/bin/env python

import csv, glob, sys, string

#class MarketImporter:
#    def importLog(self, path):
#        logReader = csv.DictReader(open(path, 'r'), delimiter=',')
#        return logReader

# unused currently, but COOL
def generator():
    for r in expList:
        if r['typeID'] == '17816':  # Minmatar UUB Nexus Chip
            yield r['price']

#mi = MarketImporter()

keyList = ['timestamp', 'price', 'volRemaining', 'typeID', 'range', 'orderID',
           'volEntered', 'minVolume', 'bid', 'issued', 'duration', 'stationID',
           'regionID', 'solarSystemID', 'jumps']
expList = []

# OLD METHOD, FOR SEPARATE EXPORT FILES (as generated by clicking export in-game)
#for entry in glob.glob('marketlogs/*.txt'):
#    
#    # glean the name of the file for labeling purposes (file key)
#    fileName = entry[11:len(entry)]
#    print 'Found file:', fileName
#    
#    # copy raw data into a value in expList
#    #for r in mi.importLog(entry):
#    #    r['file']=fileName
#    #    expList.append(r)
#    
#    # read each file (entry)
#    logReader = csv.DictReader(open(entry, 'r'), delimiter=',')
#    for r in logReader:
#        r['file']=fileName
#        expList.append(r)


# NEW METHOD, FOR ONE FILE CONTAINING MULTIPLE LOGS (dumper generated)

logFile = "marketlogs/all_logs.csv"
timestamp = -1

print 'Reading from:', logFile, '...'

with open(logFile, 'rb') as f:
    reader = csv.reader(f)
    try:
        for row in reader:
            if ( len(row) ):    # if this isn't a blank line
                if ( string.find(row[0], 'TS:') == 0 ):             # if row starts with 'TS:' we know it's a timestamp line
                    timestamp = convTS(string.split(row[0])[1])         # extract and convert raw timestamp
                elif ( not(string.find(row[0], 'price') == 0) ):    # if this isn't a keys row, it must be data
                    row.insert(0, timestamp)                            # insert the timestamp at front of the list (as per keyList)
                    expList.append(dict(zip(keyList, row)))             # add entry to expList
                    
    except csv.Error, e:
        sys.exit('file %s, line %d: %s' % (filename, reader.line_num, e))

# count num of data rows that were read across all logs
print 'Length of expList:', len(expList)

#for item in expList:
#    print item['file']

#print list(generator())
print expList[0]['typeID']

