#!/usr/bin/env python
# -*- coding: utf-8 -*-
import csv
import json
import re
import sys
import logging
import argparse
col_tmd = 'TMD File'
col_regex = 'regex to match Trace Class'
col_tag = 'Expected Tag'
def readUtmdToJson(utmd_filename):
    with open(utmd_filename, 'r') as utmdfile:
        j = json.load(utmdfile)
        utmdfile.close()
        return j
def writeJsonToUtmd(j, utmd_filename):
    with open(utmd_filename, 'w') as utmdfile:
        utmdfile.write(json.dumps(j,
            sort_keys=True,
            ensure_ascii=True,
            indent=2))
        utmdfile.close()

def updateUtmd(utmd_filename, csv_filename):
    if utmd_filename.startswith('./'):
        utmd_filename = utmd_filename[2::]
    csvfile = open(csv_filename, 'r')
    j = readUtmdToJson(utmd_filename)
    if j is None:
        logging.error('read UTMD failed')
        sys.exit()
    if j['traceFamily'] == 'PS':
        logging.warning('{}-{} not L1 / L2 UTMD'.format(j['module'], j['traceFamily']))
        sys.exit()
    fieldnames = (col_tmd, col_regex, col_tag)
    reader = csv.DictReader( csvfile, fieldnames)
    logging.info('Module: {} - {}'.format(j['module'], utmd_filename))
    debug_UH = 'Ultra-High'
    debug_H  = 'High'
    debug_M  = 'Medium'
    debug_L  = 'Low'
    debug_UL = 'Ultra-Low'
    #update debugLevel
    for traceClass in j['traceClassDefs']:
        for k, v in traceClass.iteritems():
            if k.endswith('_UH'):
                v['debugLevel'] = debug_UH
            elif k.endswith('_H'):
                v['debugLevel'] = debug_H
            elif k.endswith('_M'):
                v['debugLevel'] = debug_M
            elif k.endswith('_L'):
                v['debugLevel'] = debug_L
            elif k.endswith('_UL'):
                v['debugLevel'] = debug_UL
    #update tag
    for row in reader:
        if row[col_tmd] in utmd_filename:
            logging.info(row)
            for traceClass in j['traceClassDefs']:
                for k, v in traceClass.iteritems():
                    logging.info('Original: {} {}'.format(k, v))
                    #default mapping for entire TMD
                    if row[col_regex] == 'N/A':
                        logging.info('Matching N/A: {}'.format(k))
                        logging.info(k)
                        logging.info(v)
                        v['tag'] = [ row[col_tag] ]
                    #regex to match trace class name
                    else:
                        logging.info('Matching regex: {} {}'.format(k, row[col_regex]))
                        tag = re.sub(row[col_regex], row[col_tag], k)
                        if tag:
                            v['tag'] = [ tag ]
                    logging.info('Changed: {} {}'.format(k, v))
    writeJsonToUtmd(j, utmd_filename)

def init_logger(log_filename):
    logging.basicConfig(level=logging.DEBUG,
            format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
            datefmt='%m-%d %H:%M:%S',
            filename=log_filename)
    console = logging.StreamHandler()
    console.setLevel(logging.DEBUG)
    formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
    console.setFormatter(formatter)
    logging.getLogger('').addHandler(console)

def main():
    parser = argparse.ArgumentParser(description='update-l1-utmd:\n\
            Update tag of trace class',
            #formatter_class=argparse.ArgumentDefaultsHelpFormatter)
            formatter_class=argparse.RawDescriptionHelpFormatter)
    parser.add_argument("-v", action="version", version='1.0.0')
    parser.add_argument("utmd_file",
            help="input L1 UTMD file")
    parser.add_argument("csv_file",
            help="input CSV file(survey table)")
    parser.add_argument("-l", dest="log_file",
            help="log file",
            default='update-l1-utmd.log',
            action="store")
    args = parser.parse_args()
    if args.utmd_file is None:
        parser.print_help()
        quit()
    init_logger(args.log_file)
    updateUtmd(args.utmd_file, args.csv_file)
if __name__ == '__main__':
    main()

