def get(section, option, default=None, use_cache=True, expiration_time=900, session=None): """ Get an option value for the named section. Value can be auto-coerced to string, int, float, bool, None. Caveat emptor: Strings, regardless the case, matching 'on'/off', 'true'/'false', 'yes'/'no' are converted to bool. 0/1 are converted to int, and not to bool. :param section: The name of the section. :param option: The name of the option. :param default: The default value if no value is found. :param use_cache: Boolean if the cache should be used. :param expiration_time: Time after that the cached value gets ignored. :param session: The database session in use. :returns: The auto-coerced value. """ value_key = _value_cache_key(section, option) value = NoValue() if use_cache: value = read_from_cache(value_key, expiration_time) if isinstance(value, NoValue): tmp = session.query(models.Config.value).filter_by(section=section, opt=option).first() if tmp is not None: value = __convert_type(tmp[0]) write_to_cache(value_key, tmp[0]) elif default is None: raise ConfigNotFound else: value = default write_to_cache(value_key, str(value)) # Also write default to cache else: value = __convert_type(value) return value
def has_option(section, option, use_cache=True, expiration_time=3600, session=None): """ Check if the given section exists and contains the given option. :param section: The name of the section. :param option: The name of the option. :param use_cache: Boolean if the cache should be used. :param expiration_time: Time after that the cached value gets ignored. :param session: The database session in use. :returns: True/False """ has_option_key = 'has_option_%s_%s' % (section, option) has_option = NoValue() if use_cache: has_option = read_from_cache(has_option_key, expiration_time) if isinstance(has_option, NoValue): query = session.query(models.Config).filter_by(section=section, opt=option) has_option = True if query.first() else False write_to_cache(has_option_key, has_option) return has_option
def get_config_limit(activity, rse_id, logger=logging.log): """ Get RSE transfer limits in strict mode. :param activity: The activity. :param rse_id: The RSE id. :param logger: Optional decorated logger that can be passed from the calling daemons or servers. :returns: max_transfers if exists else None. """ result = NoValue() key = 'config_limits' if using_memcache: result = REGION_SHORT.get(key) if type(result) is NoValue: try: logger(logging.DEBUG, "Refresh rse config limits") result = get_config_limits() REGION_SHORT.set(key, result) except: logger( logging.WARNING, "Failed to retrieve rse transfer limits: %s" % (traceback.format_exc())) result = None threshold = None if result: if activity in result.keys(): if rse_id in result[activity].keys(): threshold = result[activity][rse_id] elif 'all_rses' in result[activity].keys(): threshold = result[activity]['all_rses'] if not threshold and 'all_activities' in result.keys(): if rse_id in result['all_activities'].keys(): threshold = result['all_activities'][rse_id] elif 'all_rses' in result['all_activities'].keys(): threshold = result['all_activities']['all_rses'] return threshold
def get_config_limit(activity, rse_id): """ Get RSE transfer limits in strict mode. :param activity: The activity. :param rse_id: The RSE id. :returns: max_transfers if exists else None. """ result = NoValue() key = 'config_limits' if using_memcache: result = REGION_SHORT.get(key) if type(result) is NoValue: try: logging.debug("Refresh rse config limits") result = get_config_limits() REGION_SHORT.set(key, result) except: logging.warning("Failed to retrieve rse transfer limits: %s" % (traceback.format_exc())) result = None threshold = None if result: if activity in result.keys(): if rse_id in result[activity].keys(): threshold = result[activity][rse_id] elif 'all_rses' in result[activity].keys(): threshold = result[activity]['all_rses'] if not threshold and 'all_activities' in result.keys(): if rse_id in result['all_activities'].keys(): threshold = result['all_activities'][rse_id] elif 'all_rses' in result['all_activities'].keys(): threshold = result['all_activities']['all_rses'] return threshold
def items(section, use_cache=True, expiration_time=900, session=None): """ Return a list of (option, value) pairs for each option in the given section. Values are auto-coerced as in get(). :param section: The name of the section. :param use_cache: Boolean if the cache should be used. :param expiration_time: Time after that the cached value gets ignored. :param session: The database session in use. :returns: [('option', auto-coerced value), ...] """ items_key = _items_cache_key(section) items = NoValue() if use_cache: items = read_from_cache(items_key, expiration_time) if isinstance(items, NoValue): items = session.query(models.Config.opt, models.Config.value).filter_by(section=section).all() write_to_cache(items_key, items) return [(item[0], __convert_type(item[1])) for item in items]
def has_section(section, use_cache=True, expiration_time=900, session=None): """ Indicates whether the named section is present in the configuration. :param section: The name of the section. :param use_cache: Boolean if the cache should be used. :param expiration_time: Time after that the cached value gets ignored. :param session: The database session in use. :returns: True/False """ has_section_key = 'has_section_%s' % section has_section = NoValue() if use_cache: has_section = read_from_cache(has_section_key, expiration_time) if isinstance(has_section, NoValue): query = session.query(models.Config).filter_by(section=section) has_section = True if query.first() else False write_to_cache(has_section_key, has_section) return has_section
def sections(use_cache=True, expiration_time=900, session=None): """ Return a list of the sections available. :param use_cache: Boolean if the cache should be used. :param expiration_time: Time after that the cached value gets ignored. :param session: The database session in use. :returns: ['section_name', ...] """ all_sections = NoValue() if use_cache: all_sections = read_from_cache(SECTIONS_CACHE_KEY, expiration_time) if isinstance(all_sections, NoValue): query = session.query(models.Config.section).distinct().all() all_sections = [section[0] for section in query] write_to_cache(SECTIONS_CACHE_KEY, all_sections) return all_sections
def options(section, use_cache=True, expiration_time=900, session=None): """ Returns a list of options available in the specified section. :param section: The name of the section. :param use_cache: Boolean if the cache should be used. :param expiration_time: Time after that the cached value gets ignored. :param session: The database session in use. :returns: ['option', ...] """ options_key = _options_cache_key(section) options = NoValue() if use_cache: options = read_from_cache(options_key, expiration_time) if isinstance(options, NoValue): query = session.query(models.Config.opt).filter_by(section=section).distinct().all() options = [option[0] for option in query] write_to_cache(options_key, options) return options
def sections(use_cache=True, expiration_time=3600, session=None): """ Return a list of the sections available. :param use_cache: Boolean if the cache should be used. :param expiration_time: Time after that the cached value gets ignored. :param session: The database session in use. :returns: ['section_name', ...] """ sections_key = 'sections' all_sections = NoValue() if use_cache: all_sections = REGION.get(sections_key, expiration_time=expiration_time) if isinstance(all_sections, NoValue): query = session.query(models.Config.section).distinct().all() all_sections = [section[0] for section in query] REGION.set(sections_key, all_sections) return all_sections