ungana

Unnamed repository; edit this file 'description' to name the repository.
Info | Log | Files | Refs | README

commit b52d4834ebd56a1b07c4ea2b9be6607741fb5d48
parent e1eb4dee7cb00942e6c3df490031447fa432ca62
Author: Carlosokumu <carlosokumu254@gmail.com>
Date:   Wed, 20 Aug 2025 21:20:58 +0300

pdate ArgParser’s handle_edit method to format the extracted iCalendar file for editing

Diffstat:
Mcalendarapp/cmd/args_parser.py | 114++++++++++++++++++++++++++++++++++++++++++++++---------------------------------
1 file changed, 66 insertions(+), 48 deletions(-)

diff --git a/calendarapp/cmd/args_parser.py b/calendarapp/cmd/args_parser.py @@ -1,6 +1,6 @@ import argparse from pathlib import Path -from datetime import datetime +from datetime import datetime, timedelta import re import logging from calendarapp.attachment.attachment_manager import AttachmentManager @@ -156,49 +156,27 @@ class ArgsParser: self._edit_most_recent_event(cal,args,ical_file_path) def _edit_most_recent_event(self, cal, args, ical_file_path): - """Edit the first event in the calendar (default).""" updates = {} attachments = [] - if args.start is not None: - updates["DTSTART"] = args.start - if args.duration is not None: - updates["DURATION"] = args.duration - if args.summary is not None: - updates["SUMMARY"] = args.summary - if args.location is not None: - updates["LOCATION"] = args.location - if args.description is not None: - updates["DESCRIPTION"] = args.description - if args.organizer is not None: - updates["ORGANIZER"] = args.organizer - - if args.poster is not None: - try: - prop, value, params = self.attachment_manager.create_attachment( - args.poster, ctx="poster" - ) - attachments.append((prop, value, params)) - except Exception as e: - self.parser.error(f"Unexpected error: {e}") - - if args.long is not None: - try: - prop, value, params = self.attachment_manager.create_attachment( - args.long, ctx="long" - ) - attachments.append((prop, value, params)) - except Exception as e: - self.parser.error(f"Unexpected error: {e}") + if args.start: updates["DTSTART"] = args.start + if args.duration: updates["DURATION"] = args.duration + if args.summary: updates["SUMMARY"] = args.summary + if args.location: updates["LOCATION"] = args.location + if args.description: updates["DESCRIPTION"] = args.description + if args.organizer: updates["ORGANIZER"] = args.organizer + + for ctx_name in ("poster", "long"): + arg_val = getattr(args, ctx_name, None) + if arg_val: + try: + prop, value, params = self.attachment_manager.create_attachment(arg_val, ctx=ctx_name) + attachments.append((prop, value, params)) + except Exception as e: + self.parser.error(f"Unexpected error: {e}") event = self.ical_manager.get_first_event(cal) - editable_fields = [f for f in event.keys() if f != "UID"] - - for field in editable_fields: - current_value = event.get(field, "") - new_value = input(f"{field} [{current_value}]: ").strip() - if new_value: - updates[field] = new_value + updates.update(self._get_user_event_updates(event)) if updates or attachments: self.ical_manager.update_event(cal, updates, attachments, ical_file_path) @@ -207,15 +185,14 @@ class ArgsParser: logging.info("No changes made to calendar file") - - def _edit_multiple_events(self, cal, ical_file_path): + def _edit_multiple_events(self, cal, ical_file_path): """Edit a specific event from multiple events (requires --all).""" events = self.ical_manager.get_all_events(cal) if not events: self.parser.error("no events found for the selected ical file") return - # List all available events + # list events for idx, e in enumerate(events, start=1): summary = e.get("SUMMARY", "No title") start = e.get("DTSTART") @@ -236,20 +213,61 @@ class ArgsParser: return event = events[choice - 1] - editable_fields = [f for f in event.keys() if f != "UID"] + updates = self._get_user_event_updates(event) + + if updates: + self.ical_manager.update_event(cal, updates, None, ical_file_path) + logging.info("Calendar updated successfully") + else: + logging.info("No changes made to calendar file") + + + + def _get_user_event_updates(self, event) -> dict: updates = {} + editable_fields = [ + f for f in event.keys() + if f not in ("UID", "DTSTAMP", "CATEGORIES") + ] + for field in editable_fields: current_value = event.get(field, "") + + if field == "DTSTART": + try: + dt_val = event.decoded("DTSTART") + if isinstance(dt_val, list): + dt_val = next((d for d in dt_val if d.tzinfo and d.tzinfo.key != "UTC"), dt_val[0]) + + if isinstance(dt_val, datetime): + tz = dt_val.tzinfo.tzname(dt_val) if dt_val.tzinfo else "UTC" + current_value = dt_val.strftime("%Y-%m-%d %H:%M:%S") + f" ({tz})" + else: + current_value = str(dt_val) + except Exception as e: + print(f"DTSTART formatting error: {e}") + + elif field == "DURATION": + try: + dur_val = event.decoded("DURATION") + if isinstance(dur_val, timedelta): + hours, remainder = divmod(dur_val.total_seconds(), 3600) + minutes, _ = divmod(remainder, 60) + current_value = f"{int(hours)}h {int(minutes)}m" + except Exception: + pass + + elif field == "ATTACH": + attaches = event.get("ATTACH", []) + current_value = [str(a)[:12] + "..." for a in attaches] + new_value = input(f"{field} [{current_value}]: ").strip() if new_value: updates[field] = new_value - if updates: - self.ical_manager.update_event(cal, updates, ical_file_path) - logging.info("Calendar updated successfully") - else: - logging.info("No changes made to calendar file") + return updates +