#! /usr/bin/env python
__author__ = "Ben Raanan"
__copyright__ = "Copyright 2021, MBARI"
__credits__ = ["MBARI"]
__license__ = "GPL-3.0"
__maintainer__ = "Ben Raanan"
__email__ = "byraanan at mbari.org"
__doc__ = '''

Defines a YMAL configuration reader for the LRAUV backseat helper service .

@author: __author__
@license: __license__
'''
import yaml
import logging
from pathlib import Path


logger = logging.getLogger("backseat")

local_config = (Path(__file__).parent / 'backseat_config.yml').absolute()


def read_config(file_name=local_config):
    """
    Reads a yaml configuration file.

    :param file_name: configuration file path (if non provided, defaults to local config file ./backseat_config.yml)
    :return: dictionary containing config values, or None if read/validation fails.
    """
    cfg = None

    try:
        with open(file_name, 'r') as f:
            logger.info("Reading configuration file: {0}".format(file_name))
            cfg = yaml.safe_load(f)

    except FileNotFoundError:
        logger.error("Failed to read configuration from: {0}. File not found.".format(file_name))

    return cfg
