def parsable_date_validator(input_str): """ A filter allowing any string which can be parsed by dateutil. Raises ValidationError otherwise. """ try: get_time_from_str(input_str) return input_str except ValueError: raise ValidationError( 'Expected format: a date (e.g. 2019-01-01, tomorrow 10am, ' '2nd Jan, Jan 4th, etc) or valid time if today. ' '(Ctrl-C to exit)\n')
def parsable_date_validator(input_str): """ A filter allowing any string which can be parsed by dateutil. Raises ValidationError otherwise. """ try: get_time_from_str(input_str) return input_str except ValueError: raise ValidationError( 'Expected format: a date (e.g. 2019-01-01, tomorrow 10am, ' '2nd Jan, Jan 4th, etc) or valid time if today. ' '(Ctrl-C to exit)\n' )
def test_get_time_from_str(): begin_2018 = '2018-01-01' begin_2018_midnight = begin_2018 + 'T00:00:00+00:00' two_hrs_later = begin_2018 + 'T02:00:00+00:00' next_day = '2018-01-02' assert (begin_2018_midnight, two_hrs_later) == \ get_time_from_str(begin_2018_midnight, duration=120) assert (begin_2018, next_day) == \ get_time_from_str(begin_2018_midnight, duration=1, allday=True) with pytest.raises(ValueError): get_time_from_str('this is not a date') with pytest.raises(ValueError): get_time_from_str(begin_2018_midnight, duration='not a duration') with pytest.raises(ValueError): get_time_from_str(begin_2018_midnight, duration='not a duraction', allday=True)
def test_get_time_from_str(): assert utils.get_time_from_str('7am tomorrow')
def main(): args, flags = parse_args() if flags.version: version() sys.exit(0) if flags.help: usage(True) sys.exit(0) if flags.helpshort: usage() sys.exit(0) if not flags.color: colors.CLR.use_color = False if not flags.lineart: gcal.ART.useArt = False if flags.conky: colors.SetConkyColors() if flags.locale: try: locale.setlocale(locale.LC_ALL, flags.locale) except Exception as e: print_err_msg("Error: " + str(e) + "!\n" "Check supported locales of your system.\n") sys.exit(1) # pop executable off the stack args = args[1:] if len(args) == 0: print_err_msg('Error: no command\n') sys.exit(1) # No sense instaniating gcalcli for nothing if not args[0] in [ 'list', 'search', 'agenda', 'calw', 'calm', 'quick', 'add', 'delete', 'edit', 'remind', 'import', 'help' ]: print_err_msg('Error: %s is an invalid command' % args[0]) sys.exit(1) # all other commands require gcalcli be brought up if args[0] == 'help': usage() sys.exit(0) if len(flags.calendar) == 0: flags.calendar = flags.default_calendar cal_names = [] cal_name_colors = [] cal_colors = get_cal_colors(flags.calendar) cal_names_filtered = [] for cal_name in flags.calendar: cal_name_simple = cal_name.split("#")[0] cal_names_filtered.append(cal_name_simple) cal_name_colors.append(cal_colors[cal_name_simple]) cal_names = cal_names_filtered if 'all' in flags.details or flags.detail_all: if not flags['detail_calendar'].present: flags['detail_calendar'].value = True if not flags['detail_location'].present: flags['detail_location'].value = True if not flags['detail_length'].present: flags['detail_length'].value = True if not flags['detail_reminders'].present: flags['detail_reminders'].value = True if not flags['detail_description'].present: flags['detail_description'].value = True if not flags['detail_url'].present: flags['detail_url'].value = "long" if not flags['detail_attendees'].present: flags['detail_attendees'].value = True if not flags['detail_attachments'].present: flags['detail_attachments'].value = True if not flags['detail_email'].present: flags['detail_email'].value = True else: if 'calendar' in flags.details: flags['detail_calendar'].value = True if 'location' in flags.details: flags['detail_location'].value = True if 'attendees' in flags.details: flags['detail_attendees'].value = True if 'attachments' in flags.details: flags['detail_attachments'].value = True if 'length' in flags.details: flags['detail_length'].value = True if 'reminders' in flags.details: flags['detail_reminders'].value = True if 'description' in flags.details: flags['detail_description'].value = True if 'longurl' in flags.details or 'url' in flags.details: flags['detail_url'].value = 'long' elif 'shorturl' in flags.details: flags['detail_url'].value = 'short' if 'attendees' in flags.details: flags['detail_attendees'].value = True if 'email' in flags.details: flags['detail_email'].value = True gci = gcal.GoogleCalendarInterface( cal_names=cal_names, cal_name_colors=cal_name_colors, military=flags.military, detail_calendar=flags.detail_calendar, detail_location=flags.detail_location, detail_attendees=flags.detail_attendees, detail_attachments=flags.detail_attachments, detail_length=flags.detail_length, detail_reminder=flags.detail_reminders, detail_descr=flags.detail_description, detail_descr_width=flags.detail_description_width, detail_url=flags.detail_url, detail_email=flags.detail_email, ignore_started=not flags.started, ignoreDeclined=not flags.declined, calWidth=flags.width, calMonday=flags.monday, calOwnerColor=get_color(flags.color_owner), calWriterColor=get_color(flags.color_writer), calReaderColor=get_color(flags.color_reader), calFreeBusyColor=get_color(flags.color_freebusy), date_color=get_color(flags.color_date), nowMarkerColor=get_color(flags.color_now_marker), border_color=get_color(flags.color_border), tsv=flags.tsv, refresh_cache=flags.refresh, use_cache=flags.cache, config_folder=flags.config_folder, client_id=flags.client_id, client_secret=flags.client_secret, defaultReminders=flags.default_reminders, all_day=flags.allday) if args[0] == 'list': gci.list_all_calendars() elif args[0] == 'search': if len(args) == 4: # start and end gci.text_query(args[1], start_text=args[2], end_text=args[3]) elif len(args) == 3: # start gci.text_query(args[1], start_text=args[2]) elif len(args) == 2: # defaults gci.text_query(args[1]) else: print_err_msg('Error: invalid search string\n') sys.exit(1) if not flags.tsv: sys.stdout.write('\n') elif args[0] == 'agenda': if len(args) == 3: # start and end gci.agenda_query(start_text=args[1], end_text=args[2]) elif len(args) == 2: # start gci.agenda_query(start_text=args[1]) elif len(args) == 1: # defaults gci.agenda_query() else: print_err_msg('Error: invalid agenda arguments\n') sys.exit(1) if not flags.tsv: sys.stdout.write('\n') elif args[0] == 'calw': if not flags.width: print_err_msg('Error: invalid width, don\'t be an idiot!\n') sys.exit(1) if len(args) >= 2: try: # Test to make sure args[1] is a number int(args[1]) except Exception: print_err_msg('Error: invalid calw arguments\n') sys.exit(1) if len(args) == 3: # weeks and start gci.cal_query(args[0], count=int(args[1]), start_text=args[2]) elif len(args) == 2: # weeks gci.cal_query(args[0], count=int(args[1])) elif len(args) == 1: # defaults gci.cal_query(args[0]) else: print_err_msg('Error: invalid calw arguments\n') sys.exit(1) sys.stdout.write('\n') elif args[0] == 'calm': if not flags.width: print_err_msg('Error: invalid width, don\'t be an idiot!\n') sys.exit(1) if len(args) == 2: # start gci.cal_query(args[0], start_text=args[1]) elif len(args) == 1: # defaults gci.cal_query(args[0]) else: print_err_msg('Error: invalid calm arguments\n') sys.exit(1) sys.stdout.write('\n') elif args[0] == 'quick': if len(args) != 2: print_err_msg('Error: invalid event text\n') sys.exit(1) gci.quick_add_event(args[1], reminder=flags.reminder) elif (args[0] == 'add'): if flags.prompt: if flags.title is None: print_msg(colors.CLR_MAG(), "Title: ") flags.title = input() if flags.where is None: print_msg(colors.CLR_MAG(), "Location: ") flags.where = input() if flags.when is None: print_msg(colors.CLR_MAG(), "When: ") flags.when = input() if flags.duration is None: if flags.allday: print_msg(colors.CLR_MAG(), "Duration (days): ") else: print_msg(colors.CLR_MAG(), "Duration (mins): ") flags.duration = input() if flags.description is None: print_msg(colors.CLR_MAG(), "Description: ") flags.description = input() if not flags.reminder: while 1: print_msg(colors.CLR_MAG(), "Enter a valid reminder or '.' to end: ") r = input() if r == '.': break n, m = gcal.parse_reminder(str(r)) flags.reminder.append(str(n) + ' ' + m) # calculate "when" time: try: e_start, e_end = get_time_from_str(flags.when, flags.duration, flags.allday) except ValueError as exc: print_err_msg(str(exc)) sys.exit(1) gci.add_event(flags.title, flags.where, e_start, e_end, flags.description, flags.who, flags.reminder) elif args[0] == 'delete': event_start = None event_end = None if len(args) < 2: print_err_msg('Error: invalid search string\n') sys.exit(1) elif len(args) == 4: # search, start, end event_start = gci.date_parser.from_string(args[2]) event_end = gci.date_parser.from_string(args[3]) elif len(args) == 3: # search, start (default end) event_start = gci.date_parser.from_string(args[2]) gci.delete_events(args[1], flags.iamaexpert, event_start, event_end) sys.stdout.write('\n') elif args[0] == 'edit': if len(args) != 2: print_err_msg('Error: invalid search string\n') sys.exit(1) gci.EditEvents(args[1]) sys.stdout.write('\n') elif args[0] == 'remind': if len(args) == 3: # minutes and command gci.Remind(int(args[1]), args[2], use_reminders=flags.use_reminders) elif len(args) == 2: # minutes gci.Remind(int(args[1]), use_reminders=flags.use_reminders) elif len(args) == 1: # defaults gci.Remind(use_reminders=flags.use_reminders) else: print_err_msg('Error: invalid remind arguments\n') sys.exit(1) elif args[0] == 'import': if len(args) == 1: # stdin gci.ImportICS(flags.verbose, flags.dump, flags.reminder) elif len(args) == 2: # ics file gci.ImportICS(flags.verbose, flags.dump, flags.reminder, args[1]) else: print_err_msg('Error: invalid import arguments\n') sys.exit(1)
def test_get_time_from_str(): begin_2018_gmt = '2018-01-01T00:00:00+00:00' two_hrs_later = '2018-01-01T02:00:00+00:00' assert (begin_2018_gmt, two_hrs_later) == \ get_time_from_str(begin_2018_gmt, e_duration=120)