Commit 7b76664a authored by fred's avatar fred

program: handle extra episodes, that do not match the grid

parent c714c215
......@@ -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:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment