#!/usr/bin/env python
# -*- coding: iso-8859-15 -*-

import sys
import os
import time

MODULE_LONG_NAME = "Macro Recording Module"
MODULE_VERSION = "0.1.0a"
MODULE_AUTHOR = "Matthew Cutone"
MODULE_COPYRIGHT = "2012"

DEFAULT_DURATION = 5

class MacroRecorder(object):
    def __init__(self, parent):
        self.macro_name = None
        self.macro_buffer = []
        self.macro_tzero = None
        
        self.StateObject = parent.StateObject
        
        self.time_scale = 0
        self.event_type = None
        self.event_tsiderial = None
        self.event_tzero = None
        self.file_name = None
        
        self.recording = False
    
    def saveMacro(self):
        f = open(self.file_name, 'a')
        f.write("# dome tracker macro, DO NOT EDIT THIS FILE!\n")
        f.write("NAME=%s\n" % self.macro_name)
        time_scale_string = {0 : "SIDERIAL", 1 : "LOCAL"}
        f.write("TIME_SCALE=%s\n" % time_scale_string[self.time_scale])
        
        for action in self.macro_buffer:
            type, time, duration = action
            action_line = "action[%s,%s,%s]:%s,%s\n" % (time[0], time[1], time[2], type, duration)
            f.write(action_line)
        
        f.close
    
    def setMacroName(self):
        self.macro_name = self.StateObject.macros_vars["MACRO_NAME"]
        
        return 1
        
    def resetRecorderVars(self):
        self.event_type = None
        self.event_tsiderial = None
        self.event_tzero = None
    
    def startEventRecorder(self, event_type):
        if not self.recording:
            self.recording = True
            self.event_tsiderial = self.StateObject.time_vars["CURRENT_LMST"]
            self.event_tzero = time.time()
            self.event_type = event_type
        else:
            print("MacroRecorder: Already recording, use stopEventRecorder()")
    
    def stopEventRecorder(self):
        if self.recording:
            duration = "%.1f" % (time.time() - self.event_tzero)
            self.macro_buffer.append((self.event_type, self.event_tsiderial, duration))
            self.resetRecorderVars()
            self.recording = False
            
            #for testing
            for line in self.macro_buffer:
                print(line)
            
        else:
            print("MacroRecorder: Not recording, use startEventRecorder()")
    
    def addEventRecord(self, event_type, time_stmp, duration=DEFAULT_DURATION):
        self.macro_buffer.append((event_type, time_stmp, duration))
    
    def createNewMacro(self, name, siderial_time, file_name):
        self.file_name = file_name
        self.macro_name = name
        self.clearMacroBuffer()
        self.macro_tzero = siderial_time
    
    def clearMacroBuffer(self):
        self.macro_buffer = []

##!/usr/bin/python
## URL that generated this code:
## http://txt2re.com/index-python.php3?s=[0,0,0]%20:%20(MOVE_LEFT,%202.60)&3&2&4

#import re

#txt='[0,0,0] : (MOVE_LEFT, 2.60)'

#re1='(\\[.*?\\])'  # Square Braces 1
#re2='.*?'  # Non-greedy match on filler
#re3='((?:[a-z][a-z0-9_]*))'    # Variable Name 1
#re4='.*?'  # Non-greedy match on filler
#re5='([+-]?\\d*\\.\\d+)(?![-+0-9\\.])' # Float 1

#rg = re.compile(re1+re2+re3+re4+re5,re.IGNORECASE|re.DOTALL)
#m = rg.search(txt)
#if m:
    #sbraces1=m.group(1)
    #var1=m.group(2)
    #float1=m.group(3)
    #print "("+sbraces1+")"+"("+var1+")"+"("+float1+")"+"\n"
        
if __name__ == "__main__":
    pass
