def new_from_string(collection, calendar_name, conf, date_list, location=None, repeat=None, until=None): """construct a new event from a string and add it""" try: event = aux.construct_event( date_list, location=location, repeat=repeat, until=until, locale=conf['locale']) except FatalError: sys.exit(1) event = Event.fromVEvents( [event], calendar=calendar_name, locale=conf['locale']) try: collection.new(event) except ReadOnlyCalendarError: logger.fatal('ERROR: Cannot modify calendar "{}" as it is ' 'read-only'.format(calendar_name)) sys.exit(1) if conf['default']['print_new'] == 'event': echo(event.event_description) elif conf['default']['print_new'] == 'path': path = collection._calnames[event.calendar].path + event.href echo(path.encode(conf['locale']['encoding']))
def new_from_args(collection, calendar_name, conf, dtstart=None, dtend=None, summary=None, description=None, allday=None, location=None, categories=None, repeat=None, until=None, alarms=None, timezone=None, format=None, env=None): try: event = aux.new_event(locale=conf['locale'], location=location, categories=categories, repeat=repeat, until=until, alarms=alarms, dtstart=dtstart, dtend=dtend, summary=summary, description=description, timezone=timezone) except ValueError as e: logger.fatal('ERROR: '+str(e)) sys.exit(1) except FatalError: sys.exit(1) event = Event.fromVEvents( [event], calendar=calendar_name, locale=conf['locale']) try: collection.new(event) except ReadOnlyCalendarError: logger.fatal('ERROR: Cannot modify calendar "{}" as it is ' 'read-only'.format(calendar_name)) sys.exit(1) if conf['default']['print_new'] == 'event': if format is None: format = conf['view']['event_format'] echo(event.format(format, datetime.now(), env=env)) elif conf['default']['print_new'] == 'path': path = collection._calnames[event.calendar].path + event.href echo(path) return event
def new_from_args(collection, calendar_name, conf, dtstart=None, dtend=None, summary=None, description=None, allday=None, location=None, categories=None, repeat=None, until=None, alarms=None, timezone=None, format=None, env=None): """Create a new event from arguments and add to vdirs""" try: event = utils.new_event( locale=conf['locale'], location=location, categories=categories, repeat=repeat, until=until, alarms=alarms, dtstart=dtstart, dtend=dtend, summary=summary, description=description, timezone=timezone, ) except ValueError as error: raise FatalError(error) event = Event.fromVEvents( [event], calendar=calendar_name, locale=conf['locale']) try: collection.new(event) except ReadOnlyCalendarError: raise FatalError( 'ERROR: Cannot modify calendar "{}" as it is read-only'.format(calendar_name) ) if conf['default']['print_new'] == 'event': if format is None: format = conf['view']['event_format'] echo(event.format(format, dt.datetime.now(), env=env)) elif conf['default']['print_new'] == 'path': path = os.path.join( collection._calendars[event.calendar]['path'], event.href ) echo(path) return event
def import_event(vevent, collection, locale, batch, format=None, env=None): """import one event into collection, let user choose the collection :type vevent: list of vevents, which can be more than one VEVENT, i.e., the same UID, i.e., one "master" event and (optionally) 1+ RECURRENCE-ID events :type vevent: list(str) """ # print all sub-events if not batch: for item in cal_from_ics(vevent).walk(): if item.name == 'VEVENT': event = Event.fromVEvents( [item], calendar=collection.default_calendar_name, locale=locale) echo(event.format(format, dt.datetime.now(), env=env)) # get the calendar to insert into if not collection.writable_names: raise ConfigurationError( 'No writable calendars found, aborting import.') if len(collection.writable_names) == 1: calendar_name = collection.writable_names[0] elif batch: calendar_name = collection.default_calendar_name else: calendar_names = sorted(collection.writable_names) choices = ', '.join( [f'{name}({num})' for num, name in enumerate(calendar_names)]) while True: value = prompt( "Which calendar do you want to import to? (unique prefixes are fine)\n" f"{choices}", default=collection.default_calendar_name, ) try: calendar_name = calendar_names[int(value)] break except (ValueError, IndexError): matches = [ x for x in collection.writable_names if x.startswith(value) ] if len(matches) == 1: calendar_name = matches[0] break echo('invalid choice') assert calendar_name in collection.writable_names if batch or confirm( f"Do you want to import this event into `{calendar_name}`?"): try: collection.new(Item(vevent), collection=calendar_name) except DuplicateUid: if batch or confirm( "An event with the same UID already exists. Do you want to update it?" ): collection.force_update(Item(vevent), collection=calendar_name) else: logger.warning(f"Not importing event with UID `{event.uid}`")
def new_from_args(collection, calendar_name, conf, dtstart=None, dtend=None, summary=None, description=None, allday=None, location=None, categories=None, repeat=None, until=None, alarms=None, timezone=None, format=None, env=None): try: event = utils.new_event( locale=conf['locale'], location=location, categories=categories, repeat=repeat, until=until, alarms=alarms, dtstart=dtstart, dtend=dtend, summary=summary, description=description, timezone=timezone, ) except ValueError as e: logger.fatal('ERROR: ' + str(e)) sys.exit(1) except FatalError: sys.exit(1) event = Event.fromVEvents([event], calendar=calendar_name, locale=conf['locale']) try: collection.new(event) except ReadOnlyCalendarError: logger.fatal( 'ERROR: Cannot modify calendar "{}" as it is read-only'.format( calendar_name)) sys.exit(1) if conf['default']['print_new'] == 'event': if format is None: format = conf['view']['event_format'] echo(event.format(format, datetime.now(), env=env)) elif conf['default']['print_new'] == 'path': path = collection._calnames[event.calendar].path + event.href echo(path) return event
def import_event(vevent, collection, locale, batch, format=None, env=None): """import one event into collection, let user choose the collection :type vevent: list of vevents, which can be more than one VEVENT, i.e., the same UID, i.e., one "master" event and (optionally) 1+ RECURRENCE-ID events :type vevent: list(str) """ # print all sub-events if not batch: for item in icalendar.Calendar.from_ical(vevent).walk(): if item.name == 'VEVENT': event = Event.fromVEvents( [item], calendar=collection.default_calendar_name, locale=locale) echo(event.format(format, datetime.now(), env=env)) # get the calendar to insert into if not collection.writable_names: raise ConfigurationError('No writable calendars found, aborting import.') if len(collection.writable_names) == 1: calendar_name = collection.writable_names[0] elif batch: calendar_name = collection.default_calendar_name else: calendar_names = sorted(collection.writable_names) choices = ', '.join( ['{}({})'.format(name, num) for num, name in enumerate(calendar_names)]) while True: value = prompt( "Which calendar do you want to import to? (unique prefixes are fine)\n" "{}".format(choices), default=collection.default_calendar_name, ) try: calendar_name = calendar_names[int(value)] break except (ValueError, IndexError): matches = [x for x in collection.writable_names if x.startswith(value)] if len(matches) == 1: calendar_name = matches[0] break echo('invalid choice') assert calendar_name in collection.writable_names if batch or confirm("Do you want to import this event into `{}`?".format(calendar_name)): try: collection.new(Item(vevent), collection=calendar_name) except DuplicateUid: if batch or confirm( "An event with the same UID already exists. Do you want to update it?"): collection.force_update(Item(vevent), collection=calendar_name) else: logger.warning("Not importing event with UID `{}`".format(event.uid))
def import_event(vevent, collection, locale, batch, random_uid): """import one event into collection, let user choose the collection""" # print all sub-events for sub_event in vevent: if not batch: event = Event.fromVEvents( [sub_event], calendar=collection.default_calendar_name, locale=locale) echo(event.event_description) # get the calendar to insert into if batch or len(collection.writable_names) == 1: calendar_name = collection.writable_names[0] else: choice = list() for num, name in enumerate(collection.writable_names): choice.append('{}({})'.format(name, num)) choice = ', '.join(choice) while True: value = prompt('Which calendar do you want to import to? \n' '{}'.format(choice), default=collection.default_calendar_name) try: number = int(value) calendar_name = collection.writable_names[number] break except (ValueError, IndexError): matches = filter(lambda x: x.startswith(value), collection.writable_names) if len(matches) == 1: calendar_name = matches[0] break echo('invalid choice') if batch or confirm(u"Do you want to import this event into `{}`?" u"".format(calendar_name)): ics = aux.ics_from_list(vevent, random_uid) try: collection.new(Item(ics.to_ical().decode('utf-8')), collection=calendar_name) except DuplicateUid: if batch or confirm(u"An event with the same UID already exists. " u"Do you want to update it?"): collection.force_update(Item(ics.to_ical().decode('utf-8')), collection=calendar_name) else: logger.warn(u"Not importing event with UID `{}`".format( event.uid))
def import_event(vevent, collection, locale, batch, random_uid): """import one event into collection, let user choose the collection""" # print all sub-events for sub_event in vevent: if not batch: event = Event.fromVEvents( [sub_event], calendar=collection.default_calendar_name, locale=locale) echo(event.event_description) # get the calendar to insert into if batch or len(collection.writable_names) == 1: calendar_name = collection.default_calendar_name else: choice = list() for num, name in enumerate(collection.writable_names): choice.append('{}({})'.format(name, num)) choice = ', '.join(choice) while True: value = prompt('Which calendar do you want to import to? \n' '{}'.format(choice), default=collection.default_calendar_name) try: number = int(value) calendar_name = collection.writable_names[number] break except (ValueError, IndexError): matches = filter(lambda x: x.startswith(value), collection.writable_names) if len(matches) == 1: calendar_name = matches[0] break echo('invalid choice') if batch or confirm("Do you want to import this event into `{}`?" "".format(calendar_name)): ics = aux.ics_from_list(vevent, random_uid) try: collection.new( Item(ics.to_ical().decode('utf-8')), collection=calendar_name) except DuplicateUid: if batch or confirm("An event with the same UID already exists. " "Do you want to update it?"): collection.force_update( Item(ics.to_ical().decode('utf-8')), collection=calendar_name) else: logger.warn("Not importing event with UID `{}`".format(event.uid))
def print_ics(conf, name, ics, format): if format is None: format = conf['view']['agenda_event_format'] cal = icalendar.Calendar.from_ical(ics) events = [item for item in cal.walk() if item.name == 'VEVENT'] events_grouped = defaultdict(list) for event in events: events_grouped[event['UID']].append(event) vevents = list() for uid in events_grouped: vevents.append(sorted(events_grouped[uid], key=utils.sort_key)) echo('{} events found in {}'.format(len(vevents), name)) for sub_event in vevents: event = Event.fromVEvents(sub_event, locale=conf['locale']) echo(event.format(format, dt.datetime.now()))
def print_ics(conf, name, ics, format): if format is None: format = conf['view']['event_format'] cal = cal_from_ics(ics) events = [item for item in cal.walk() if item.name == 'VEVENT'] events_grouped = defaultdict(list) for event in events: events_grouped[event['UID']].append(event) vevents = [] for uid in events_grouped: vevents.append(sorted(events_grouped[uid], key=sort_vevent_key)) echo(f'{len(vevents)} events found in {name}') for sub_event in vevents: event = Event.fromVEvents(sub_event, locale=conf['locale']) echo(event.format(format, dt.datetime.now()))
def print_ics(conf, name, ics, format): if format is None: format = conf['view']['agenda_event_format'] cal = icalendar.Calendar.from_ical(ics) events = [item for item in cal.walk() if item.name == 'VEVENT'] events_grouped = defaultdict(list) for event in events: events_grouped[event['UID']].append(event) vevents = list() for uid in events_grouped: vevents.append(sorted(events_grouped[uid], key=sort_key)) echo('{} events found in {}'.format(len(vevents), name)) for sub_event in vevents: event = Event.fromVEvents(sub_event, locale=conf['locale']) echo(event.format(format, datetime.now()))
def new_from_args(collection, calendar_name, conf, dtstart=None, dtend=None, summary=None, description=None, allday=None, location=None, categories=None, repeat=None, until=None, alarms=None, timezone=None, url=None, format=None, json=None, env=None): """Create a new event from arguments and add to vdirs""" if isinstance(categories, str): categories = [category.strip() for category in categories.split(',')] try: event = new_vevent( locale=conf['locale'], location=location, categories=categories, repeat=repeat, until=until, alarms=alarms, dtstart=dtstart, dtend=dtend, summary=summary, description=description, timezone=timezone, url=url, ) except ValueError as error: raise FatalError(error) event = Event.fromVEvents([event], calendar=calendar_name, locale=conf['locale']) try: collection.new(event) except ReadOnlyCalendarError: raise FatalError( f'ERROR: Cannot modify calendar "{calendar_name}" as it is read-only' ) if conf['default']['print_new'] == 'event': if json is None or len(json) == 0: if format is None: format = conf['view']['event_format'] formatter = human_formatter(format) else: formatter = json_formatter(json) echo(formatter(event.attributes(dt.datetime.now(), env=env))) elif conf['default']['print_new'] == 'path': path = os.path.join(collection._calendars[event.calendar]['path'], event.href) echo(path) return event