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 @@ ...@@ -3,6 +3,7 @@
from dataclasses import dataclass from dataclasses import dataclass
import copy import copy
from enum import Enum from enum import Enum
import glob
from itertools import combinations from itertools import combinations
import logging import logging
import os import os
...@@ -12,6 +13,7 @@ from typing import Any, List, Mapping, Optional, Union ...@@ -12,6 +13,7 @@ from typing import Any, List, Mapping, Optional, Union
import iris import iris
import pkg_resources import pkg_resources
import xdg.BaseDirectory
import yaml import yaml
from .index import Index from .index import Index
...@@ -315,12 +317,28 @@ def build_index_function(spec): ...@@ -315,12 +317,28 @@ def build_index_function(spec):
return index_function return index_function
def load_metadata(): def find_metadata_files_in_dir(directory):
metadata_filename = os.path.join(os.path.dirname(__file__), if os.path.isdir(directory):
'etc', 'metadata.yml') return glob.glob(os.path.join(directory, '*.yml'))
with open(metadata_filename) as md_file: return []
metadata = yaml.safe_load(md_file)
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
indices = {name: build_index(idx_meta)
for name, idx_meta in metadata['indices'].items()} def load_metadata():
indices = {}
for metadata_path in find_metadata_files():
with open(metadata_path) as md_file:
metadata = yaml.safe_load(md_file)
index_metadata = metadata.get('indices', {})
for name, idx_meta in index_metadata.items():
indices[name] = build_index(idx_meta, metadata_path)
return IndexCatalog(indices) return IndexCatalog(indices)
...@@ -37,6 +37,7 @@ setuptools.setup( ...@@ -37,6 +37,7 @@ setuptools.setup(
'dask-jobqueue', 'dask-jobqueue',
'distributed>=2.4.0', 'distributed>=2.4.0',
'numpy', 'numpy',
'sentry-sdk', 'sentry-sdk',
'scitools-iris>=2.2.0', 'scitools-iris>=2.2.0',
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