def set(section, option, value, session=None): """ Set the given option to the specified value. If the option doesn't exist, it is created. :param section: The name of the section. :param option: The name of the option. :param value: The content of the value. :param session: The database session in use. """ if not has_option(section=section, option=option, use_cache=False, session=session): section_existed = has_section(section=section) new_option = models.Config(section=section, opt=option, value=value) new_option.save(session=session) delete_from_cache(key=_value_cache_key(section, option)) delete_from_cache(key=_has_option_cache_key(section, option)) delete_from_cache(key=_items_cache_key(section)) if not section_existed: delete_from_cache(key=SECTIONS_CACHE_KEY) delete_from_cache(key=_has_section_cache_key(section)) else: old_value = session.query(models.Config.value).filter_by(section=section, opt=option).first()[0] if old_value != str(value): old_option = models.ConfigHistory(section=section, opt=option, value=old_value) old_option.save(session=session) session.query(models.Config).filter_by(section=section, opt=option).update({'value': str(value)}) delete_from_cache(key=_value_cache_key(section, option)) delete_from_cache(key=_items_cache_key(section))
def remove_option(section, option, session=None): """ Remove the specified option from the configuration. :param section: The name of the section. :param option: The name of the option. :param session: The database session in use. :returns: True/False """ if not has_option(section=section, option=option, session=session, use_cache=False): return False else: old_option = models.ConfigHistory(section=section, opt=option, value=session.query(models.Config.value).filter_by(section=section, opt=option).first()[0]) old_option.save(session=session) session.query(models.Config).filter_by(section=section, opt=option).delete() if not session.query(func.count('*')).select_from(models.Config).filter_by(section=section).scalar(): # we deleted the last config entry in the section. Invalidate the section cache delete_from_cache(key=SECTIONS_CACHE_KEY) delete_from_cache(key=_has_section_cache_key(section)) delete_from_cache(key=_items_cache_key(section)) delete_from_cache(key=_has_option_cache_key(section, option)) delete_from_cache(key=_value_cache_key(section, option)) return True
def remove_section(section, session=None): """ Remove the specified section from the specified section. :param section: The name of the section. :param session: The database session in use. :returns: True/False. """ if not has_section(section=section, session=session): return False else: for old in session.query( models.Config.value).filter_by(section=section).all(): old_option = models.ConfigHistory(section=old[0], opt=old[1], value=old[2]) old_option.save(session=session) delete_from_cache(key=_has_option_cache_key(old[0], old[1])) delete_from_cache(key=_value_cache_key(old[0], old[1])) session.query(models.Config).filter_by(section=section).delete() delete_from_cache(key=SECTIONS_CACHE_KEY) delete_from_cache(key=_items_cache_key(section)) return True