From 3dd31a4f91ddd2ff849249b2d42ba486602fb128 Mon Sep 17 00:00:00 2001 From: Tony Breeds Date: Wed, 3 Feb 2016 11:52:57 +1100 Subject: [PATCH] Add functionality to batch the meetings list Created and export, to jinja2, a batch_meetings fuction that will pivot the sorted list of meetings into a number of colums. This can be used in a jinja template like: {% for column in batch_meetings(meetings, 4)|batch(4) %} So the list: [A, B, C, D, E, F , G, H] Dsisplays as: A C E G B D F H Rather than: A B C D E F G H Change-Id: I9d7c6ad9c14a9d6ec3d402a67b73fafae575d369 --- yaml2ical/index.py | 17 +++++++++++++++++ yaml2ical/meeting.py | 1 + 2 files changed, 18 insertions(+) diff --git a/yaml2ical/index.py b/yaml2ical/index.py index 384a2e0..43c4426 100644 --- a/yaml2ical/index.py +++ b/yaml2ical/index.py @@ -10,6 +10,8 @@ # License for the specific language governing permissions and limitations # under the License. +from __future__ import division + import datetime import jinja2 import logging @@ -17,6 +19,20 @@ import os import os.path +def batch_meetings(meetings, batch_size): + col_length = len(meetings) // batch_size + new_meetings = [None] * len(meetings) + src = 0 + + for row in range(batch_size): + for col in range(col_length): + dest = col * batch_size + row + new_meetings[dest] = meetings[src] + src += 1 + + return new_meetings + + def convert_meetings_to_index(meetings, template, output_file): """Creates index file from list of meetings. @@ -30,6 +46,7 @@ def convert_meetings_to_index(meetings, template, output_file): loader = jinja2.FileSystemLoader(template_dir) env = jinja2.environment.Environment(trim_blocks=True, loader=loader) template = env.get_template(template_file) + template.globals['batch_meetings'] = batch_meetings with open(output_file, "w") as out: out.write(template.render(meetings=meetings, diff --git a/yaml2ical/meeting.py b/yaml2ical/meeting.py index badeb55..da2579c 100644 --- a/yaml2ical/meeting.py +++ b/yaml2ical/meeting.py @@ -176,6 +176,7 @@ def load_meetings(yaml_source): raise ValueError("No .yaml file, directory containing .yaml files, " "or YAML data found.") else: + meetings.sort(key=lambda x: x.project) return meetings