import abc
import csv
from datetime import datetime

from pikos.abstract_monitors import PikosError


class RecorderError(PikosError): pass


def _make_filename():
    time = datetime.now()
    return '{0}.profile'.format(time.strftime('%Y-%m-%d_%H-%M-S'))


class AbstractRecorder(object):

    __metaclass__ = abc.ABCMeta

    @abc.abstractmethod
    def prepare(self):
        ''' Perform any setup required before the recorder is used
        '''

    @abc.abstractmethod
    def finalize(self):
        ''' Perform any tasks to finalize and clean up when the
        recording is completed
        '''

    @abc.abstractmethod
    def record(self, *args, **kwargs):
        ''' Record a measurement. '''


class CSVRecorder(AbstractRecorder):

    def __init__(self, filename=None):
        self._filename = _make_filename() if (filename is None) else filename
        self._csvfile = open(self._filename, 'wb', buffering=0)
        self._writer = csv.writer(self._csvfile)
        self._started = False

    def prepare(self, fields):
        if not self._started:
            self._writer.writerow(fields)
            self._started = True

    def finalize(self):
        pass

    def record(self, values):
        self._writer.writerow(values)

    def prepare(self, fields):
        self._output_fh = open(self._filename, 'wb', buffering=0)
        self._writer = csv.writer(self._output_fh)
        self._writer.writerow(fields)

    def finalize(self):
        self._output_fh.close()
        self._output_fh = None
        self._writer = None

    def record(self, fields, values):
        try:
            line = [values[field] for field in fields]
        except KeyError:
            raise RecorderError(
                'Invalid value dictionary. Expected keys {0!r}, got {1!r}'.\
                    format(fields, values.keys()))
        self._writer.writerow(line)

@@ -1251,193 +1251,48 @@
 s)%0A%0A
-    def prepare(self, fields):%0A        self._output_fh = open(self._filename, 'wb', buffering=0)%0A        self._writer = csv.writer(self._output_fh)%0A        self._writer.writerow(fields)
+class ConsoleRecorder(AbstractRecorder):
 %0A%0A  
@@ -1293,32 +1293,32 @@
 ):%0A%0A    def 
-finalize
+__init__
 (self):%0A    
@@ -1325,90 +1325,12 @@
     
-self._output_fh.close()%0A        self._output_fh = None%0A        self._writer = None
+pass
 %0A%0A  
@@ -1331,30 +1331,31 @@
 ss%0A%0A    def 
+p
 re
-cord
+pare
 (self, field
@@ -1355,24 +1355,16 @@
 , fields
-, values
 ):%0A     
@@ -1370,286 +1370,141 @@
     
-try:%0A            line = %5Bvalues%5Bfield%5D for field in fields%5D%0A        except KeyError:%0A            raise RecorderError(%0A                'Invalid value dictionary. Expected keys %7B0!r%7D, got %7B1!r%7D'.%5C%0A                    format(fields, values.keys()))%0A        self._writer.writerow(line)
+print %22RECORDING STARTS%22%0A%0A    def finalize(self):%0A        print %22RECORDING FINISHED%22%0A%0A    def record(self, values):%0A        print values
 %0A

Add a dummy console output recorder