diff --git a/hexrd/nf_config/config.py b/hexrd/nf_config/config.py new file mode 100644 index 000000000..74c620629 --- /dev/null +++ b/hexrd/nf_config/config.py @@ -0,0 +1,80 @@ +"""Base Config class""" + +import logging +import os + +from .utils import null + +logger = logging.getLogger('hexrd.config') + +class Config(object): + + _dirty = False + + def __init__(self, cfg): + self._cfg = cfg + + @property + def dirty(self): + return self._dirty + + def get(self, key, default=null): + args = key.split(':') + args, item = args[:-1], args[-1] + temp = self._cfg + for arg in args: + temp = temp.get(arg, {}) + # intermediate block may be None: + temp = {} if temp is None else temp + try: + res = temp[item] + except KeyError: + if default is not null: + logger.info( + '%s not specified, defaulting to %s', key, default + ) + res = temp.get(item, default) + else: + raise RuntimeError( + '%s must be specified in configuration file' % key + ) + return res + + def set(self, key, val): + args = key.split(':') + args, item = args[:-1], args[-1] + temp = self._cfg + for arg in args: + temp = temp.get(arg, {}) + # intermediate block may be None: + temp = {} if temp is None else temp + if temp.get(item, null) != val: + temp[item] = val + self._dirty = True + + def dump(self, filename): + import yaml + + with open(filename, 'w') as f: + yaml.dump(self._cfg, f) + self._dirty = False + + @staticmethod + def check_filename(fname, wdir): + """Check whether filename is valid relative to working directory + + fname - the name of the file + + Returns the absolute path of the filename if the file exists + + If fname is an absolute path, use that; otherwise take it as a path + relative to the working directory. +""" + temp = fname + if not os.path.isabs(fname): + temp = os.path.join(wdir, temp) + if os.path.exists(temp): + return temp + raise IOError( + 'file: "%s" not found' % temp + ) diff --git a/hexrd/nf_config/experiment.py b/hexrd/nf_config/experiment.py index 3abfadab6..96f5f134a 100644 --- a/hexrd/nf_config/experiment.py +++ b/hexrd/nf_config/experiment.py @@ -2,13 +2,15 @@ import os import numpy as np -from hexrd import config as Config +from .config import Config logger = logging.getLogger('hexrd.config') class ExperimentConfig(Config): + def __init__(self, cfg): + self._cfg = cfg @property def beam_energy(self): diff --git a/hexrd/nf_config/images.py b/hexrd/nf_config/images.py index 433c6c6b5..e0e49bfdf 100644 --- a/hexrd/nf_config/images.py +++ b/hexrd/nf_config/images.py @@ -1,7 +1,7 @@ import logging import os -from hexrd import config as Config +from .config import Config logger = logging.getLogger('hexrd.config') @@ -12,6 +12,8 @@ class ImagesConfig(Config): + def __init__(self, cfg): + self._cfg = cfg @property def data_folder(self): diff --git a/hexrd/nf_config/input_files.py b/hexrd/nf_config/input_files.py index 4de10500b..e92fd7b9d 100644 --- a/hexrd/nf_config/input_files.py +++ b/hexrd/nf_config/input_files.py @@ -1,14 +1,16 @@ import logging import os -from hexrd import config as Config +from .config import Config logger = logging.getLogger('hexrd.config') class InputConfig(Config): - + def __init__(self, cfg): + self._cfg = cfg + @property def det_file(self): temp = self._cfg.get('input_files:det_file') diff --git a/hexrd/nf_config/multiprocessing.py b/hexrd/nf_config/multiprocessing.py index 60fc9288a..d5e206782 100644 --- a/hexrd/nf_config/multiprocessing.py +++ b/hexrd/nf_config/multiprocessing.py @@ -3,13 +3,15 @@ import psutil import multiprocessing as mp -from hexrd import config as Config +from .config import Config logger = logging.getLogger('hexrd.config') class MultiprocessingConfig(Config): + def __init__(self, cfg): + self._cfg = cfg @property def num_cpus(self): diff --git a/hexrd/nf_config/nf_root.py b/hexrd/nf_config/nf_root.py index afd005325..ab80b243f 100644 --- a/hexrd/nf_config/nf_root.py +++ b/hexrd/nf_config/nf_root.py @@ -5,7 +5,7 @@ from hexrd.constants import shared_ims_key from hexrd import imageseries -from hexrd import config as Config +from .config import Config from .multiprocessing import MultiprocessingConfig from .reconstruction import ReconstructionConfig from .images import ImagesConfig @@ -17,7 +17,9 @@ class NFRootConfig(Config): - + def __init__(self, cfg): + self._cfg = cfg + @property def main_dir(self): return self._cfg.get('main_dir') diff --git a/hexrd/nf_config/reconstruction.py b/hexrd/nf_config/reconstruction.py index 1e689bfac..ec755449c 100644 --- a/hexrd/nf_config/reconstruction.py +++ b/hexrd/nf_config/reconstruction.py @@ -1,12 +1,14 @@ import logging import os -from hexrd import config as Config +from .config import Config logger = logging.getLogger('hexrd.config') class ReconstructionConfig(Config): + def __init__(self, cfg): + self._cfg = cfg @property def tomography(self): diff --git a/hexrd/nf_config/tomography.py b/hexrd/nf_config/tomography.py index 1f0a67c22..d6629344a 100644 --- a/hexrd/nf_config/tomography.py +++ b/hexrd/nf_config/tomography.py @@ -1,13 +1,15 @@ import logging import os -from hexrd import config as Config +from .config import Config logger = logging.getLogger('hexrd.config') class TomographyConfig(Config): + def __init__(self, cfg): + self._cfg = cfg @property def data_folder(self):