# Tai Sakuma <tai.sakuma@cern.ch>
from ..mkdir_p import mkdir_p
from ..listToAlignedText import listToAlignedText
import os
from ReadCounter import ReadCounter

##__________________________________________________________________||
class TblBrilCalc(object):
    """This class reads brilcalc results in csv.

    Args:
        outPath (str): a path to the output file
        csvFileName (str): the name of the CSV file with the brilcalc results
    """
    def __init__(self, outPath, csvFileName = 'brilcalc.csv'):
        self._outPath = outPath
        self._csvFileName = csvFileName
        self._rows = [['component', 'luminosity']]

    def begin(self): pass

    def read(self, component):

        path = os.path.join(component.path, self._csvFileName)

        try:
            csvfile = filter(lambda row: row[0]!='#', open(path))
        except IOError, e:
            import logging
            logging.warning(e)
            return

        import csv, re
        fieldnames = ('runfill', 'time', 'nls', 'ncms', 'delivered', 'recorded')
        lumi_csv = csv.DictReader(csvfile, fieldnames)
        lumi_csv = [l for l in lumi_csv] # read all
        lumi_csv = [l for l in lumi_csv if re.search(r'^[0-9]*:[0-9]*$', l['runfill'])] # e.g. '258159:4449' for the runfill field
        recorded = [l['recorded'] for l in lumi_csv]
        recorded = [float(l) for l in recorded] # /ub
        recorded = [l/1000000 for l in recorded] # /ub
        recorded = sum(recorded)

        row = [component.name, recorded]
        self._rows.append(row)

    def end(self):
        f = self._open(self._outPath)
        f.write(listToAlignedText(self._rows))
        self._close(f)

    def _open(self, path):
        mkdir_p(os.path.dirname(path))
        return open(path, 'w')

    def _close(self, file): file.close()

##__________________________________________________________________||

# Tai Sakuma <tai.sakuma@cern.ch>
from ..mkdir_p import mkdir_p
from ..listToAlignedText import listToAlignedText
import os
from ReadCounter import ReadCounter

##__________________________________________________________________||
class TblBrilCalc(object):
    """This class reads brilcalc results in csv.

    Args:
        outPath (str): a path to the output file
        csvFileName (str): the name of the CSV file with the brilcalc results
    """
    def __init__(self, outPath, csvFileName = 'brilcalc.csv'):
        self._outPath = outPath
        self._csvFileName = csvFileName
        self._rows = [['component', 'luminosity']]

    def begin(self): pass

    def read(self, component):

        path = os.path.join(component.path, self._csvFileName)

        if not os.path.exists(path): return

        try:
            csvfile = filter(lambda row: row[0]!='#', open(path))
        except IOError, e:
            import logging
            logging.warning(e)
            return

        import csv, re
        fieldnames = ('runfill', 'time', 'nls', 'ncms', 'delivered', 'recorded')
        lumi_csv = csv.DictReader(csvfile, fieldnames)
        lumi_csv = [l for l in lumi_csv] # read all
        lumi_csv = [l for l in lumi_csv if re.search(r'^[0-9]*:[0-9]*$', l['runfill'])] # e.g. '258159:4449' for the runfill field
        recorded = [l['recorded'] for l in lumi_csv]
        recorded = [float(l) for l in recorded] # /ub
        recorded = [l/1000000 for l in recorded] # /ub
        recorded = sum(recorded)

        row = [component.name, recorded]
        self._rows.append(row)

    def end(self):
        if len(self._rows) == 1: return

        f = self._open(self._outPath)
        f.write(listToAlignedText(self._rows))
        self._close(f)

    def _open(self, path):
        mkdir_p(os.path.dirname(path))
        return open(path, 'w')

    def _close(self, file): file.close()

##__________________________________________________________________||

check if input file exists in TblBrilCalc.py