class SoundsController:

  def __init__(self, job_names):
    self.sound_player = Player()
    self.play_sounds = dict.fromkeys(job_names, False)

  def update_build_status(self, job, status):
    if (not self.play_sounds[job.name]):
      self.play_sounds[job.name] = True
      return

    if (status == SUCCESS and job.success != None):
      if (job.success == '__RANDOM'):
        self.sound_player.play_random_success_sound()
      else:
        self.sound_player.play_success(job.success)

    elif (status == FAILURE and job.failure != None):
      if (job.failure == '__RANDOM'):
        self.sound_player.play_random_failure_sound()
      else:
        self.sound_player.play_failure(job.failure)
class JenkinsMessageTranslator:

    def __init__(self, reporter_q):
        self.pipelines = []
        for pipeline_config in Config().pipelines():
            self.pipelines += [Pipeline(pipeline_config)]
        self.sound_player = Player()
        self.reporter_q = reporter_q

    def issue_directive(self, directive, play_sound=False):
        if directive == 'all_off':
            for pipeline in self.pipelines:
                pipeline.issue_all_off()
            self.reporter_q.put(self.__current_state())
            return

        pipeline = self.__determine_pipeline(directive)
        segment_number = self.__determine_segment_number(pipeline, directive)

        if segment_number == 0:
            pipeline.issue_start_build()
            if play_sound:
              self.sound_player.play_random_start_sound()
            self.reporter_q.put(self.__current_state())
            return

        colour = self.__determine_colour(directive)
        if play_sound:
          if colour == 'green':
            self.sound_player.play_random_success_sound()
          elif colour == 'red':
            self.sound_player.play_random_failure_sound()

        if segment_number == 1 and re.match('.*Unit.*', directive):
            pipeline.issue_all_stages_update(colour)
            self.reporter_q.put(self.__current_state())
            return

        pipeline.issue_update_segment(segment_number, colour)
        self.reporter_q.put(self.__current_state())

    def __determine_pipeline(self, directive):
        build_name = re.search(jenkins_regex, directive).group(2)
        for pipeline in self.pipelines:
            if pipeline.matches(build_name): return pipeline
        logging.getLogger().error("problem determining pipeline for directive: {0}".format(directive))
        raise UnrecognisedDirective

    def __determine_segment_number(self, pipeline, directive):
        match = re.search(jenkins_regex, directive)
        if match is None or match.group(2) not in pipeline.detail['STAGES']:
            logging.getLogger().error("problem determining segment for directive: {0}".format(directive))
            raise UnrecognisedDirective
        return pipeline.detail['STAGES'].index(match.group(2))

    def __determine_colour(self, directive):
        match = re.search(jenkins_regex, directive)
        return jenkins_colours[match.group(1)]

    def __current_state(self):
        state_of_all_pipelines = {}
        for pipeline in self.pipelines:
            state_of_all_pipelines.update(pipeline.current_state())
        now = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        return { "recorded_at" : now, "name" : "fmsystems", "pipelines" : state_of_all_pipelines }