def _find_key_in_yaml_file(yaml_file_path, search_keys, full_key_name, value_is_relative_path): """Find a key in a yaml file.""" if not os.path.isfile(yaml_file_path): return None result = _load_yaml_file(yaml_file_path) if not search_keys: # Give the entire yaml file contents. # |value_is_relative_path| is not applicable here. return result for search_key in search_keys: if not isinstance(result, dict): raise errors.InvalidConfigKey(full_key_name) if search_key not in result: return None result = result[search_key] if value_is_relative_path: yaml_directory = os.path.dirname(yaml_file_path) if isinstance(result, list): result = [os.path.join(yaml_directory, str(i)) for i in result] else: result = os.path.join(yaml_directory, str(result)) return result
def _get_key_location(search_path, full_key_name): """Get the path of the the yaml file and the key components given a full key name.""" key_parts = full_key_name.split(SEPARATOR) dir_path = search_path # Find the directory components of the key path for i, search_key in enumerate(key_parts): search_path = os.path.join(dir_path, search_key) if os.path.isdir(search_path): # Don't allow both a/b/... and a/b.yaml if os.path.isfile(search_path + YAML_FILE_EXTENSION): raise errors.InvalidConfigKey(full_key_name) dir_path = search_path else: # The remainder of the key path is a yaml_filename.key1.key2... key_parts = key_parts[i:] break else: # The entirety of the key reference a directory. key_parts = [] if key_parts: return dir_path, key_parts[0] + YAML_FILE_EXTENSION, key_parts[1:] return dir_path, '', []
def _get_helper(self, key_name='', default=None, value_is_relative_path=False): """Helper for get and get_absolute_functions.""" if self._root: key_name = self._root + SEPARATOR + key_name if key_name else self._root if not key_name: raise errors.InvalidConfigKey(key_name) cache_key_name = self._cache.get_key(self._get_helper, (key_name, value_is_relative_path), {}) value = self._cache.get(cache_key_name) if value is not None: return value value = _search_key(self._config_dir, key_name, value_is_relative_path) if value is None: return default self._cache.put(cache_key_name, value) return value