From 7b76664a3b71955d7e87208477630ee85ce4be87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Fri, 30 Aug 2013 14:27:29 +0200 Subject: [PATCH] program: handle extra episodes, that do not match the grid --- panikdb/emissions/utils.py | 43 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/panikdb/emissions/utils.py b/panikdb/emissions/utils.py index 93af087..ac06e61 100644 --- a/panikdb/emissions/utils.py +++ b/panikdb/emissions/utils.py @@ -84,13 +84,56 @@ def period_program(date_start, date_end): # look for a diffusion matching this schedule d = [x for x in diffusions if x.datetime.timetuple()[:5] == schedule.datetime.timetuple()[:5]] if d: + diffusions.remove(d[0]) program[i] = d[0] for j, other_schedule in enumerate(program[i+1:]): + # remove other emissions scheduled at the same time if other_schedule.datetime.timetuple()[:5] == schedule.datetime.timetuple()[:5]: program[i+1+j] = None else: break + # here we are with remaining diffusions, those that were not overriding a + # planned schedle + for diffusion in diffusions: + program = [x for x in program if x is not None] + try: + just_before_program = [x for x in program if x.datetime < diffusion.datetime][-1] + new_diff_index = program.index(just_before_program)+1 + except IndexError: + # nothing before + new_diff_index = 0 + program.insert(new_diff_index, diffusion) + + # cut (or even remove) programs that started earlier but continued over + # this program start time + i = new_diff_index + while i > 0: + previous = program[i-1] + previous_endtime = previous.datetime + timedelta(minutes=previous.get_duration()) + if previous_endtime > diffusion.datetime: + previous.duration = (diffusion.datetime - previous.datetime).seconds / 60 + if previous.duration <= 0: + program[i-1] = None + i -= 1 + + # push back (or remove) programs that started before this program ends + # (this may be unnecessary as next step does that again for all + # programs) + diffusion_endtime = diffusion.datetime + timedelta(minutes=diffusion.get_duration()) + i = new_diff_index + while i < len(program)-1: + next_prog = program[i+1] + if next_prog.datetime < diffusion_endtime: + diff = diffusion_endtime - next_prog.datetime + if (diff.seconds/60) > next_prog.get_duration(): + program[i+1] = None + else: + next_prog.datetime = diffusion_endtime + next_prog.duration = next_prog.get_duration() - (diff.seconds/60) + i += 1 + + # remove overlapping programs program = [x for x in program if x is not None] for i, slot in enumerate(program): if slot is None: -- GitLab