Attachment 'ics-to-moinmoin.py'
Download 1 # Copyright (c) 2024 Pedro Vílchez
2 # SPDX-License-Identifier: AGPL-3.0-or-later
3
4 # With the help of an (AI) LLM, hence, all humanity participated on this
5
6 from icalendar import Calendar, Event
7 import datetime
8
9 def ics_to_moinmoin_table(ics_file_paths):
10 events = []
11 # Define a color mapping for each calendar file
12 color_map = {
13 'wbmv15_main.ics': '#D1D1FF', # Light blue
14 'wbmv15_talks.ics': '#A2D5AB', # Soft Green
15 'wbmv15_workshops.ics': '#FFD580', # Light Orange
16 }
17
18 # Process each calendar file
19 for ics_file_path in ics_file_paths:
20 with open(ics_file_path, 'rb') as file:
21 cal = Calendar.from_ical(file.read())
22
23 for component in cal.walk():
24 if component.name == "VEVENT":
25 summary = component.get('summary')
26 start_dt = component.get('dtstart').dt
27 end_dt = component.get('dtend').dt
28 description = component.get('description', '-').replace('\n', ' <<BR>> ')
29
30 event_details = {
31 'file': ics_file_path.split('/')[-1], # Extract filename only
32 'start_dt': start_dt,
33 'end_dt': end_dt,
34 'summary': summary,
35 'description': description
36 }
37 events.append(event_details)
38
39 # Sort events by start date and time
40 events.sort(key=lambda x: x['start_dt'])
41
42 # Building the MoinMoin table
43 moinmoin_table = "||'''When'''||'''Title'''||'''Description'''||'''Attachments and links'''||\n"
44 for event in events:
45 date_time_str = event['start_dt'].strftime('%Y-%m-%d %H:%M')
46 if event['start_dt'] != event['end_dt']:
47 date_time_str += f"-{event['end_dt'].strftime('%H:%M')}"
48
49 # Apply row style based on the event's source file using the color map
50 row_color = color_map.get(event['file'], '#FFFFFF') # Default to white if no match
51 row_style = f"rowstyle=\"background-color: {row_color};\""
52
53 moinmoin_table += f"||<style=\"white-space: nowrap;\" {row_style}>{date_time_str}||{event['summary']}||{event['description']} ||-||\n"
54
55 return moinmoin_table
56
57 def main():
58 # Example usage with multiple calendar files
59 # I used get-ics.sh script too
60 ics_paths = ['wbmv15_main.ics', 'wbmv15_talks.ics', 'wbmv15_workshops.ics']
61 wiki_content = ics_to_moinmoin_table(ics_paths)
62 print(wiki_content)
63
64 if __name__ == "__main__":
65 main()