# Python 3.4 on Mac OS X
import glob
import re
import csv

from pandas import DataFrame,Series

class Jd:
    def __init__(self):
        self.ls = []
    
    def file_list(self, path):
        self.file_list = glob.glob(path)

    def contents(self, f_name):
        with open(f_name) as f:
            # readlines don't work as regex need string instead of list
            self.f_c = f.read()

    def parse(self, tag):
        if tag == 'id':
            pattern = 'Auto\sreq\sID.*\n'
        elif tag == 'title':
            pattern = 'Job\sTitle.*\n'
        elif tag == 'status':
            pattern = 'Job\sStatus.*\n'

        result = ''.join(re.findall(pattern, self.f_c))
        _, value = re.split('\t', result)
        self.ls.append(value)

    def export_csv(self, path, rows):
        with open(path,'a') as f:
            writer = csv.writer(f)
            writer.writerow(rows)
            self.ls = []


if __name__ == '__main__':
    tags = ['id', 'title','status']
    path = '/Users/Sail/Desktop/*.txt'

    jd = Jd()
    jd.file_list(path)
    jd.export_csv('jd_modified.csv', tags)
    for f in jd.file_list:
        jd.contents(f)
        for tag in tags:
            jd.parse(tag)
        jd.export_csv('jd_modified.csv', jd.ls)
    print('Bingo')


