def get_config(config_path=None): """reads the config file, validates it and return a config dict :param config_path: path to a custom config file, if none is given the default locations will be searched :type config_path: str :returns: configuration :rtype: dict """ if config_path is None: config_path = _find_configuration_file() logger.debug('using the config file at {}'.format(config_path)) config = ConfigObj(DEFAULTSPATH, interpolation=False) try: user_config = ConfigObj(config_path, configspec=SPECPATH, interpolation=False, file_error=True, ) except ConfigObjError as error: logger.fatal('parsing the config file file with the following error: ' '{}'.format(error)) logger.fatal('if you recently updated khal, the config file format ' 'might have changed, in that case please consult the ' 'CHANGELOG or other documentation') sys.exit(1) fdict = {'timezone': is_timezone, 'expand_path': expand_path, } validator = Validator(fdict) results = user_config.validate(validator, preserve_errors=True) if not results: for entry in flatten_errors(config, results): # each entry is a tuple section_list, key, error = entry if key is not None: section_list.append(key) else: section_list.append('[missing section]') section_string = ', '.join(section_list) if error is False: error = 'Missing value or section.' print(section_string, ' = ', error) raise ValueError # TODO own error class config.merge(user_config) config_checks(config) extras = get_extra_values(user_config) for section, value in extras: if section == (): logger.warn('unknown section "{}" in config file'.format(value)) else: section = sectionize(section) logger.warn('unknown key or subsection "{}" in ' 'section "{}"'.format(value, section)) return config
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 warn_leftovers(self): for section in self._conf_parser.sections(): for option in self._conf_parser.options(section): logger.warn("Ignoring unknow option '{}' in section " "'{}'".format(option, section))