Commit 469d9824 authored by Klaus Zimmermann's avatar Klaus Zimmermann
Browse files

Add unified configuration handling (closes #19)

This adds loading of multiple yml files from various locations.
Namely, in order, with later files taking precendence over earlier ones
the following directories are scanned for any .yml file.
The order of the files within each directory is standard globbing order.

- $PACKAGE/etc/
- /etc/climix/
- <XDG directories>/climix, including
- ~/.config/climix/
parent e750d1a6
......@@ -3,6 +3,7 @@
from dataclasses import dataclass
import copy
from enum import Enum
import glob
from itertools import combinations
import logging
import os
......@@ -12,6 +13,7 @@ from typing import Any, List, Mapping, Optional, Union
import iris
import pkg_resources
import xdg.BaseDirectory
import yaml
from .index import Index
......@@ -315,12 +317,28 @@ def build_index_function(spec):
return index_function
def find_metadata_files_in_dir(directory):
if os.path.isdir(directory):
return glob.glob(os.path.join(directory, '*.yml'))
return []
def find_metadata_files():
directories = [
os.path.join(os.path.dirname(__file__), 'etc'),
] + list(xdg.BaseDirectory.load_config_paths('climix'))[::-1]
files = sum([find_metadata_files_in_dir(directory)
for directory in directories], [])
return files
def load_metadata():
metadata_filename = os.path.join(os.path.dirname(__file__),
'etc', 'metadata.yml')
with open(metadata_filename) as md_file:
indices = {}
for metadata_path in find_metadata_files():
with open(metadata_path) as md_file:
metadata = yaml.safe_load(md_file)
indices = {name: build_index(idx_meta)
for name, idx_meta in metadata['indices'].items()}
index_metadata = metadata.get('indices', {})
for name, idx_meta in index_metadata.items():
indices[name] = build_index(idx_meta, metadata_path)
return IndexCatalog(indices)
......@@ -37,6 +37,7 @@ setuptools.setup(
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment