import re import osc, log class Action: def __init__(self, text, **kwargs): self.text = text self.name = text for key, value in kwargs.items(): setattr(self, key, value) def __repr__(self): return 'Action(%s)' % ', '.join(['%s=%s' % (str(x[0]), str(x[1])) for x in self.__dict__.items()]) class ActionManager: def __init__(self, confirmer, highlighter, logger=log.ConsoleLogger()): #self.client = osc.Client(host, port) self.confirmer = confirmer self.highlighter = highlighter self.logger = logger def confirmed_perform(self, action): osc.client.send_action(action) self.highlighter.highlight(action) self.logger.log(action) def perform(self, action, confirm=True, timeout=3): if confirm: self.confirmer.confirm('Perform action "%s"?' % action.name, timeout, lambda: self.confirmed_perform(action)) else: self.confirmed_perform(action) manager = None def setup(confirmer, highlighter, logger=log.ConsoleLogger()): global manager manager = ActionManager(confirmer, highlighter, logger) def perform_action(action, confirm=True, timeout=3): global manager manager.perform(action, confirm, timeout) #action(1,1,"#ENDSECTION(1)","") def parse_slu_action(text): found = re.search(r'^action\((\d+),(\d+),(\d+),"(([^"\\]|\\")*)","(([^"\\]|\\")*)"\)$', text) if found: section_id = int(found.group(1)) sequence_id = int(found.group(2)) action_id = int(found.group(3)) action_name = found.group(4) action_text = found.group(6) return Action(text, name=action_name, section=section_id, sequence=sequence_id, words=action_text, action_id=action_id) found = re.search(r'^change_section\((\d+),"(([^"\\]|\\")*)","(([^"\\]|\\")*)"\)$', text) if found: section_id = int(found.group(1)) action_name = found.group(2) action_text = found.group(3) return Action(text, name=action_name, section=section_id, words=action_text) print "Warning: could not parse slu action '%s'" % text return Action(text)