Beispiel #1
0
def test_load_config_custom_file(tmp_path):
    """Test with a custom file"""
    temp_conf_text = dict(name='Temporary Name',
                          location=dict(elevation='1234.56 m'))

    temp_conf_file = tmp_path / 'temp_conf.yaml'
    temp_conf_local_file = tmp_path / 'temp_conf_local.yaml'

    temp_conf_file.write_text(to_yaml(temp_conf_text))

    temp_conf_text['name'] = 'Local Name'
    temp_conf_local_file.write_text(to_yaml(temp_conf_text))

    # Ignore the local name
    temp_config = load_config(str(temp_conf_file.absolute()), load_local=False)
    assert len(temp_config) == 2
    assert temp_config['name'] == 'Temporary Name'
    assert temp_config['location']['elevation'] == 1234.56 * u.m
    assert isinstance(temp_config['location'], dict)

    # Load the local
    temp_config = load_config(str(temp_conf_local_file.absolute()),
                              load_local=True)
    assert len(temp_config) == 2
    assert temp_config['name'] == 'Local Name'
    assert temp_config['location']['elevation'] == 1234.56 * u.m
    assert isinstance(temp_config['location'], dict)

    # Reload the local but don't parse, load_local is default.
    temp_config = load_config(str(temp_conf_file.absolute()), parse=False)
    assert len(temp_config) == 2
    assert temp_config['name'] == 'Local Name'
    assert temp_config['location']['elevation'] == '1234.56 m'
    assert isinstance(temp_config['location'], dict)
Beispiel #2
0
def test_state_machine_absolute(temp_file):
    state_table = POCS.load_state_table()
    assert isinstance(state_table, dict)

    with open(temp_file, 'w') as f:
        f.write(to_yaml(state_table))

    file_path = os.path.abspath(temp_file)
    assert POCS.load_state_table(state_table_name=file_path)
Beispiel #3
0
def save_config(path, config, overwrite=True):
    """Save config to local yaml file.

    Args:
        path (str): Path to save, can be relative or absolute. See Notes in ``load_config``.
        config (dict): Config to save.
        overwrite (bool, optional): True if file should be updated, False
            to generate a warning for existing config. Defaults to True
            for updates.

    Returns:
        bool: If the save was successful.

    Raises:
         FileExistsError: If the local path already exists and ``overwrite=False``.
    """
    # Make sure ends with '_local.yaml'
    base, ext = os.path.splitext(path)

    # Always want .yaml (although not actually used).
    ext = '.yaml'

    # Check for _local name.
    if not base.endswith('_local'):
        base = f'{base}_local'

    full_path = f'{base}{ext}'

    if os.path.exists(full_path) and overwrite is False:
        raise FileExistsError(f"Path exists and overwrite=False: {full_path}")
    else:
        # Create directory if does not exist
        os.makedirs(os.path.dirname(full_path), exist_ok=True)
        logger.info(f'Saving config to {full_path}')
        with open(full_path, 'w') as f:
            to_yaml(config, stream=f)
        logger.success(f'Config info saved to {full_path}')

    return True
Beispiel #4
0
def save_config(path, config, overwrite=True):
    """Save config to local yaml file.

    This will save any entries into the `$PANDIR/conf_files/<path>_local.yaml` file to avoid
    clobbering what comes from the version control.

    Args:
        path (str): Path to save, can be relative or absolute. See Notes
            in `load_config`.
        config (dict): Config to save.
        overwrite (bool, optional): True if file should be updated, False
            to generate a warning for existing config. Defaults to True
            for updates.
    """
    # Make sure ends with '_local.yaml'
    base, ext = os.path.splitext(path)

    # Always want .yaml (although not actually used).
    ext = '.yaml'

    # Check for _local name.
    if not base.endswith('_local'):
        base = f'{base}_local'

    # Check full path location
    if not base.startswith('/'):
        config_dir = os.path.join(os.environ['PANDIR'], 'conf_files')
        base = os.path.join(config_dir, base)

    full_path = f'{base}{ext}'

    if os.path.exists(full_path) and overwrite is False:
        warn("Path exists and overwrite=False: {}".format(full_path))
    else:
        # Create directory if does not exist
        os.makedirs(os.path.dirname(full_path), exist_ok=True)
        with open(full_path, 'w') as f:
            print(config)
            serializers.to_yaml(config, stream=f)
Beispiel #5
0
def test_custom_state_file(observatory, temp_file, config_host, config_port):
    state_table = POCS.load_state_table()
    assert isinstance(state_table, dict)

    with open(temp_file, 'w') as f:
        f.write(to_yaml(state_table))

    file_path = os.path.abspath(temp_file)

    pocs = POCS(observatory,
                state_machine_file=file_path,
                run_once=True,
                simulators=['power'])
    pocs.initialize()
    pocs.power_down()
    reset_conf(config_host, config_port)
def test_roundtrip_yaml(obj):
    config_str = serializers.to_yaml(obj)
    config = serializers.from_yaml(config_str)
    assert config['name'] == obj['name']
    assert config['location']['latitude'] == obj['location']['latitude']