# Path: gammacat/utils.py
# def load_yaml(path):
#     """Helper function to load data from a YAML file."""
#     path = Path(path)
#     log.debug('Reading {}'.format(path))
#     with path.open() as fh:
#         data = ruamel.yaml.round_trip_load(fh)
#     return data
# 
# def write_yaml(data, path):
#     """Helper function to write data to a YAML file."""
#     path = Path(path)
#     log.info('Writing {}'.format(path))
#     with path.open('w') as fh:
#         ruamel.yaml.round_trip_dump(data, fh)


import logging
from .utils import load_yaml, write_yaml
from gammapy.catalog.gammacat import GammaCatResource

log = logging.getLogger(__name__)


class DataSet:
    """Process a dataset file."""
    resource_type = 'ds'

    def __init__(self, data, resource):
        self.resource = resource
        self.data = data

    @classmethod
    def read(cls, filename):
        data = load_yaml(filename)
        resource = cls._read_resource_info(data, filename)
        return cls(data=data, resource=resource)

    @classmethod
    def _read_resource_info(cls, data, location):
        file_id = data.get('file_id', GammaCatResource._NA_FILL['file_id'])
        return GammaCatResource(
            source_id=data['source_id'],
            reference_id=data['reference_id'],
            file_id=file_id,
            type=cls.resource_type,
            location=location
        )

    def write(self, filename):

# Path: gammacat/utils.py
# def load_yaml(path):
#     """Helper function to load data from a YAML file."""
#     path = Path(path)
#     log.debug('Reading {}'.format(path))
#     with path.open() as fh:
#         data = ruamel.yaml.round_trip_load(fh)
#     return data
# 
# def write_yaml(data, path):
#     """Helper function to write data to a YAML file."""
#     path = Path(path)
#     log.info('Writing {}'.format(path))
#     with path.open('w') as fh:
#         ruamel.yaml.round_trip_dump(data, fh)


# Path: gammacat/dataset.py
import logging
from .utils import load_yaml, write_yaml
from gammapy.catalog.gammacat import GammaCatResource


log = logging.getLogger(__name__)


class DataSet:
    """Process a dataset file."""
    resource_type = 'ds'

    def __init__(self, data, resource):
        self.resource = resource
        self.data = data

    @classmethod
    def read(cls, filename):
        data = load_yaml(filename)
        resource = cls._read_resource_info(data, filename)
        return cls(data=data, resource=resource)

    @classmethod
    def _read_resource_info(cls, data, location):
        file_id = data.get('file_id', GammaCatResource._NA_FILL['file_id'])
        return GammaCatResource(
            source_id=data['source_id'],
            reference_id=data['reference_id'],
            file_id=file_id,
            type=cls.resource_type,
            location=location
        )

    def write(self, filename):

        write_yaml(self.data, filename)