convert to H5config and bump to v0.7.
[calibcant.git] / calibcant / config.py
index dc549bb5a9d07f2f06a9523215fd67ec3e4c9abd..0a20c8f4fd131f43ec95b699bc6bacea4424919b 100644 (file)
 """Define some variables to configure the package for a particular lab
 and workflow."""
 
 """Define some variables to configure the package for a particular lab
 and workflow."""
 
-import logging as _logging
-import os.path as _os_path
 import sys as _sys
 
 from FFT_tools import window_hann as _window_hann
 import sys as _sys
 
 from FFT_tools import window_hann as _window_hann
-from pypiezo.config import (
-    _ChoiceSetting, _BooleanSetting, _IntegerSetting, _FloatSetting,
-    _FloatListSetting, _Config, _BackedConfig, _HDF5Config, _YAMLConfig)
+import h5config.config as _config
+import h5config.tools as _h5config_tools
 
 
-from . import LOG as _LOG
 
 
-
-class _BaseConfig (_Config):
+class PackageConfig (_h5config_tools.PackageConfig):
     "Configure `calibcant` module operation"
     "Configure `calibcant` module operation"
-    settings = [
-        _ChoiceSetting(
-            name='log-level',
-            help='Module logging level.',
-            default=_logging.WARN,
-            choices=[
-                ('critical', _logging.CRITICAL),
-                ('error', _logging.ERROR),
-                ('warn', _logging.WARN),
-                ('info', _logging.INFO),
-                ('debug', _logging.DEBUG),
-                ]),
-        _BooleanSetting(
-            name='syslog',
-            help='Log to syslog (otherwise log to stderr).',
-            default=False),
-        _BooleanSetting(
+    settings = _h5config_tools.PackageConfig.settings + [
+        _config.BooleanSetting(
             name='matplotlib',
             help='Plot piezo motion using `matplotlib`.',
             default=False),
             name='matplotlib',
             help='Plot piezo motion using `matplotlib`.',
             default=False),
-        _FloatSetting(
+        _config.FloatSetting(
             name='temperature',
             help=('Default temperature for thermal calibration in degrees '
                   'Celsius.'),
             name='temperature',
             help=('Default temperature for thermal calibration in degrees '
                   'Celsius.'),
@@ -69,10 +49,10 @@ class Celsius (_TemperatureUnit):
 class Kelvin (_TemperatureUnit):
     pass
 
 class Kelvin (_TemperatureUnit):
     pass
 
-class _TemperatureConfig (_Config):
+class TemperatureConfig (_config.Config):
     "Configure `calibcant` temperature operation"
     settings = [
     "Configure `calibcant` temperature operation"
     settings = [
-        _ChoiceSetting(
+        _config.ChoiceSetting(
             name='units',
             help='Units of raw temperature measurements.',
             default=Celsius,
             name='units',
             help='Units of raw temperature measurements.',
             default=Celsius,
@@ -80,7 +60,7 @@ class _TemperatureConfig (_Config):
                 ('Celsius', Celsius),
                 ('Kelvin', Kelvin),
                 ]),
                 ('Celsius', Celsius),
                 ('Kelvin', Kelvin),
                 ]),
-        _BooleanSetting(
+        _config.BooleanSetting(
             name='default',
             help=('The temperature values are defaults (vs. real '
                   'measurements).'),
             name='default',
             help=('The temperature values are defaults (vs. real '
                   'measurements).'),
@@ -94,39 +74,39 @@ class Linear (_BumpModel):
 class Quadratic (_BumpModel):
     pass
 
 class Quadratic (_BumpModel):
     pass
 
-class _BumpConfig (_Config):
+class BumpConfig (_config.Config):
     "Configure `calibcant` bump operation"
     settings = [
     "Configure `calibcant` bump operation"
     settings = [
-        _FloatSetting(
+        _config.FloatSetting(
             name='initial-position',
             help=('Position relative to surface for start of bump in meters.  '
                   'Should be less than zero to ensure non-contact region '
                   'before you hit the surface.'),
             default=-50e-9),
             name='initial-position',
             help=('Position relative to surface for start of bump in meters.  '
                   'Should be less than zero to ensure non-contact region '
                   'before you hit the surface.'),
             default=-50e-9),
-        _FloatSetting(
+        _config.FloatSetting(
             name='setpoint',
             help=('Maximum deflection in volts in case of stepper positioning '
                   'to achieve the initial position.'),
             default=2.0),
             name='setpoint',
             help=('Maximum deflection in volts in case of stepper positioning '
                   'to achieve the initial position.'),
             default=2.0),
-        _IntegerSetting(
+        _config.IntegerSetting(
             name='far-steps',
             help=('Number of stepper steps to move "far" away from the '
                   'surface.  For possible stepper adjustments while initially '
                   'locating the surface.'),
             default=200),
             name='far-steps',
             help=('Number of stepper steps to move "far" away from the '
                   'surface.  For possible stepper adjustments while initially '
                   'locating the surface.'),
             default=200),
-        _FloatSetting(
+        _config.FloatSetting(
             name='push-depth',
             help='Distance to approach in meters.',
             default=200e-9),
             name='push-depth',
             help='Distance to approach in meters.',
             default=200e-9),
-        _FloatSetting(
+        _config.FloatSetting(
             name='push-speed',
             help='Approach/retract speed in meters/second.',
             default=1e-6),
             name='push-speed',
             help='Approach/retract speed in meters/second.',
             default=1e-6),
-        _FloatSetting(
+        _config.FloatSetting(
             name='samples',
             help='Number of samples during approach and during retreat.',
             default=1024),
             name='samples',
             help='Number of samples during approach and during retreat.',
             default=1024),
-        _ChoiceSetting(
+        _config.ChoiceSetting(
             name='model',
             help='Bump deflection model.',
             default=Quadratic,
             name='model',
             help='Bump deflection model.',
             default=Quadratic,
@@ -145,19 +125,19 @@ class BreitWigner (_VibrationModel):
 class OffsetBreitWigner (_VibrationModel):
     pass
 
 class OffsetBreitWigner (_VibrationModel):
     pass
 
-class _VibrationConfig (_Config):
+class VibrationConfig (_config.Config):
     "Configure `calibcant` vibration operation"
     settings = [
     "Configure `calibcant` vibration operation"
     settings = [
-        _FloatSetting(
+        _config.FloatSetting(
             name='frequency',
             help='Sampling frequency in Hz.',
             default=50e3),
             name='frequency',
             help='Sampling frequency in Hz.',
             default=50e3),
-        _FloatSetting(
+        _config.FloatSetting(
             name='sample-time',
             help=('Aquisition time in seconds.  This is rounded up as required '
                   'so the number of samples will be an integer power of two.'),
             default=1),
             name='sample-time',
             help=('Aquisition time in seconds.  This is rounded up as required '
                   'so the number of samples will be an integer power of two.'),
             default=1),
-        _ChoiceSetting(
+        _config.ChoiceSetting(
             name='model',
             help='Vibration model.',
             default=BreitWigner,
             name='model',
             help='Vibration model.',
             default=BreitWigner,
@@ -166,95 +146,55 @@ class _VibrationConfig (_Config):
                 ('Breit-Wigner', BreitWigner),
                 ('offset Breit-Wigner', OffsetBreitWigner),
                 ]),
                 ('Breit-Wigner', BreitWigner),
                 ('offset Breit-Wigner', OffsetBreitWigner),
                 ]),
-        _IntegerSetting(
+        _config.IntegerSetting(
             name='chunk-size',
             help='FFT chunk size (for PSD fits).',
             default=2048),
             name='chunk-size',
             help='FFT chunk size (for PSD fits).',
             default=2048),
-        _BooleanSetting(
+        _config.BooleanSetting(
             name='overlap',
             help='Overlap FFT chunks (for PSD fits).'),
             name='overlap',
             help='Overlap FFT chunks (for PSD fits).'),
-        _ChoiceSetting(
+        _config.ChoiceSetting(
             name='window',
             help='FFT chunk window (for PSD fits).',
             default=_window_hann,
             choices=[
                 ('Hann', _window_hann),
                 ]),
             name='window',
             help='FFT chunk window (for PSD fits).',
             default=_window_hann,
             choices=[
                 ('Hann', _window_hann),
                 ]),
-        _FloatSetting(
+        _config.FloatSetting(
             name='minimum-fit-frequency',
             help='Lower bound of Lorentzian fitting region.',
             default=500.),
             name='minimum-fit-frequency',
             help='Lower bound of Lorentzian fitting region.',
             default=500.),
-        _FloatSetting(
+        _config.FloatSetting(
             name='maximum-fit-frequency',
             help='Upper bound of Lorentzian fitting region.',
             default=25e3),
         ]
 
 
             name='maximum-fit-frequency',
             help='Upper bound of Lorentzian fitting region.',
             default=25e3),
         ]
 
 
-class _CalibrationConfig (_Config):
+class CalibrationConfig (_config.Config):
     "Configure a full `calibcant` calibration run"
     settings = [
     "Configure a full `calibcant` calibration run"
     settings = [
-        _IntegerSetting(
+        _config.IntegerSetting(
             name='num-bumps',
             help='Number of surface bumps.',
             default=10),
             name='num-bumps',
             help='Number of surface bumps.',
             default=10),
-        _IntegerSetting(
+        _config.IntegerSetting(
             name='num-temperatures',
             help='Number of temperature measurements.',
             default=10),
             name='num-temperatures',
             help='Number of temperature measurements.',
             default=10),
-        _IntegerSetting(
+        _config.IntegerSetting(
             name='num-vibrations',
             help='Number of thermal vibration measurements.',
             default=20),
             name='num-vibrations',
             help='Number of thermal vibration measurements.',
             default=20),
-        _FloatSetting(
+        _config.FloatSetting(
             name='temperature-sleep',
             help=('Time between temperature measurements (in seconds) to get '
                   'independent measurements when reading from slow sensors.'),
             default=1),
             name='temperature-sleep',
             help=('Time between temperature measurements (in seconds) to get '
                   'independent measurements when reading from slow sensors.'),
             default=1),
-        _FloatSetting(
+        _config.FloatSetting(
             name='vibration-spacing',
             help=('Approximate distance from the surface in meters for the '
                   'vibration measurements.  This should be large enough that '
                   'surface effects are negligable.'),
             default=50e-6),
         ]
             name='vibration-spacing',
             help=('Approximate distance from the surface in meters for the '
                   'vibration measurements.  This should be large enough that '
                   'surface effects are negligable.'),
             default=50e-6),
         ]
-
-
-# Define HDF5- and YAML-backed subclasses of the basic _Config types.
-for name,obj in locals().items():
-    if (obj != _Config and
-        type(obj) == type and
-        issubclass(obj, _Config) and
-        not issubclass(obj, _BackedConfig)):
-        for prefix,base in [('HDF5', _HDF5Config), ('YAML', _YAMLConfig)]:
-            _name = '%s%s' % (prefix, name)
-            _bases = (base, obj)
-            _dict = {}
-            _class = type(_name, _bases, _dict)
-            setattr(_sys.modules[__name__], _name, _class)
-
-del name, obj, prefix, base, _name, _bases, _dict, _class
-
-
-def find_base_config():
-    "Return the best `_BaseConfig` match after scanning the filesystem"
-    _LOG.info('looking for base_config file')
-    user_basepath = _os_path.join(_os_path.expanduser('~'), '.calibcantrc')
-    system_basepath = _os_path.join('/etc', 'calibcant', 'config')
-    distributed_basepath =  _os_path.join(
-        '/usr', 'share', 'calibcant', 'config')
-    for basepath in [user_basepath, system_basepath, distributed_basepath]:
-        for (extension, config) in [('.h5', HDF5_BaseConfig),
-                                    ('.yaml', YAML_BaseConfig)]:
-            filename = basepath + extension
-            if _os_path.exists(filename):
-                _LOG.info('base_config file found at %s' % filename)
-                base_config = config(filename)
-                base_config.load()
-                return base_config
-            else:
-                _LOG.debug('no base_config file at %s' % filename)
-    _LOG.info('new base_config file at %s' % filename)
-    basepath = user_basepath
-    filename = basepath + extension
-    return config(filename)