import os
from pathlib import Path
import yaml

class CalibrationDataFiler:
    ''' Handles saving and loading calibration data'''

    # Calibration data filenames
    PAN_MOTOR_TIMEBASE_ALIGNMENT = 'pan_motor_timebase_alignment.yml'
    FOV_TIMEBASE_ALIGNMENT = 'fov_timebase_alignment.yml'
    FRAME_RATE_CALCULATION = 'frame_rate_calculation.yml'
    TAG_GPS_TIMEBASE_ALIGNMENT = 'tag_gps_timebase_alignment.yml'

    def __init__(self, top_level_dir: Path):
        self._top_level_dir = top_level_dir

    def save_as_yml(self, obj, data_dir_name: str, calibration_file: str):
        '''
        :param Path data_dir_name: by convention this should be the same as session_dir \
                              containg the input data
        '''
        self._ensure_data_dir(data_dir_name)
        yaml_file = self._get_file_path(data_dir_name, calibration_file)
        with open(yaml_file, 'w') as yaml_file:
            yaml.dump(obj, yaml_file, default_flow_style=False)

    def load(self, data_dir_name: str, calibration_filename: str):
        filepath = self._get_file_path(data_dir_name, calibration_filename)
        if not filepath.is_file():
            return None
        with open(self._get_file_path(data_dir_name, calibration_filename), 'r') as stream:
            return yaml.load(stream)

    def _get_file_path(self, data_dir_name: str, filename: str) -> Path:
        return (self._get_data_dir_path(data_dir_name) / filename).resolve()

    def _ensure_data_dir(self, data_dir_name: str):
        '''
        :param Path data_dir: by convention this should be the same as session_dir \
                              containg the input data
        '''
        path = Path('/')
        for part in self._get_data_dir_path(data_dir_name).resolve().parts:
            path = path / part
            if not os.path.isdir(path):
                os.mkdir(path)

    def _get_data_dir_path(self, data_dir_name):
        return self._top_level_dir / data_dir_name
0d0f4cb10a963874c8be709db86c3b11be56b0e9
<mask token>


class CalibrationDataFiler:
    <mask token>
    <mask token>
    <mask token>
    <mask token>
    <mask token>

    def __init__(self, top_level_dir: Path):
        self._top_level_dir = top_level_dir

    def save_as_yml(self, obj, data_dir_name: str, calibration_file: str):
        """
        :param Path data_dir_name: by convention this should be the same as session_dir                               containg the input data
        """
        self._ensure_data_dir(data_dir_name)
        yaml_file = self._get_file_path(data_dir_name, calibration_file)
        with open(yaml_file, 'w') as yaml_file:
            yaml.dump(obj, yaml_file, default_flow_style=False)

    def load(self, data_dir_name: str, calibration_filename: str):
        filepath = self._get_file_path(data_dir_name, calibration_filename)
        if not filepath.is_file():
            return None
        with open(self._get_file_path(data_dir_name, calibration_filename), 'r'
            ) as stream:
            return yaml.load(stream)
    <mask token>

    def _ensure_data_dir(self, data_dir_name: str):
        """
        :param Path data_dir: by convention this should be the same as session_dir                               containg the input data
        """
        path = Path('/')
        for part in self._get_data_dir_path(data_dir_name).resolve().parts:
            path = path / part
            if not os.path.isdir(path):
                os.mkdir(path)

    def _get_data_dir_path(self, data_dir_name):
        return self._top_level_dir / data_dir_name

<mask token>


class CalibrationDataFiler:
    <mask token>
    <mask token>
    <mask token>
    <mask token>
    <mask token>

    def __init__(self, top_level_dir: Path):
        self._top_level_dir = top_level_dir

    def save_as_yml(self, obj, data_dir_name: str, calibration_file: str):
        """
        :param Path data_dir_name: by convention this should be the same as session_dir                               containg the input data
        """
        self._ensure_data_dir(data_dir_name)
        yaml_file = self._get_file_path(data_dir_name, calibration_file)
        with open(yaml_file, 'w') as yaml_file:
            yaml.dump(obj, yaml_file, default_flow_style=False)

    def load(self, data_dir_name: str, calibration_filename: str):
        filepath = self._get_file_path(data_dir_name, calibration_filename)
        if not filepath.is_file():
            return None
        with open(self._get_file_path(data_dir_name, calibration_filename), 'r'
            ) as stream:
            return yaml.load(stream)

    def _get_file_path(self, data_dir_name: str, filename: str) ->Path:
        return (self._get_data_dir_path(data_dir_name) / filename).resolve()

    def _ensure_data_dir(self, data_dir_name: str):
        """
        :param Path data_dir: by convention this should be the same as session_dir                               containg the input data
        """
        path = Path('/')
        for part in self._get_data_dir_path(data_dir_name).resolve().parts:
            path = path / part
            if not os.path.isdir(path):
                os.mkdir(path)

    def _get_data_dir_path(self, data_dir_name):
        return self._top_level_dir / data_dir_name

<mask token>


class CalibrationDataFiler:
    <mask token>
    PAN_MOTOR_TIMEBASE_ALIGNMENT = 'pan_motor_timebase_alignment.yml'
    FOV_TIMEBASE_ALIGNMENT = 'fov_timebase_alignment.yml'
    FRAME_RATE_CALCULATION = 'frame_rate_calculation.yml'
    TAG_GPS_TIMEBASE_ALIGNMENT = 'tag_gps_timebase_alignment.yml'

    def __init__(self, top_level_dir: Path):
        self._top_level_dir = top_level_dir

    def save_as_yml(self, obj, data_dir_name: str, calibration_file: str):
        """
        :param Path data_dir_name: by convention this should be the same as session_dir                               containg the input data
        """
        self._ensure_data_dir(data_dir_name)
        yaml_file = self._get_file_path(data_dir_name, calibration_file)
        with open(yaml_file, 'w') as yaml_file:
            yaml.dump(obj, yaml_file, default_flow_style=False)

    def load(self, data_dir_name: str, calibration_filename: str):
        filepath = self._get_file_path(data_dir_name, calibration_filename)
        if not filepath.is_file():
            return None
        with open(self._get_file_path(data_dir_name, calibration_filename), 'r'
            ) as stream:
            return yaml.load(stream)

    def _get_file_path(self, data_dir_name: str, filename: str) ->Path:
        return (self._get_data_dir_path(data_dir_name) / filename).resolve()

    def _ensure_data_dir(self, data_dir_name: str):
        """
        :param Path data_dir: by convention this should be the same as session_dir                               containg the input data
        """
        path = Path('/')
        for part in self._get_data_dir_path(data_dir_name).resolve().parts:
            path = path / part
            if not os.path.isdir(path):
                os.mkdir(path)

    def _get_data_dir_path(self, data_dir_name):
        return self._top_level_dir / data_dir_name

<mask token>


class CalibrationDataFiler:
    """ Handles saving and loading calibration data"""
    PAN_MOTOR_TIMEBASE_ALIGNMENT = 'pan_motor_timebase_alignment.yml'
    FOV_TIMEBASE_ALIGNMENT = 'fov_timebase_alignment.yml'
    FRAME_RATE_CALCULATION = 'frame_rate_calculation.yml'
    TAG_GPS_TIMEBASE_ALIGNMENT = 'tag_gps_timebase_alignment.yml'

    def __init__(self, top_level_dir: Path):
        self._top_level_dir = top_level_dir

    def save_as_yml(self, obj, data_dir_name: str, calibration_file: str):
        """
        :param Path data_dir_name: by convention this should be the same as session_dir                               containg the input data
        """
        self._ensure_data_dir(data_dir_name)
        yaml_file = self._get_file_path(data_dir_name, calibration_file)
        with open(yaml_file, 'w') as yaml_file:
            yaml.dump(obj, yaml_file, default_flow_style=False)

    def load(self, data_dir_name: str, calibration_filename: str):
        filepath = self._get_file_path(data_dir_name, calibration_filename)
        if not filepath.is_file():
            return None
        with open(self._get_file_path(data_dir_name, calibration_filename), 'r'
            ) as stream:
            return yaml.load(stream)

    def _get_file_path(self, data_dir_name: str, filename: str) ->Path:
        return (self._get_data_dir_path(data_dir_name) / filename).resolve()

    def _ensure_data_dir(self, data_dir_name: str):
        """
        :param Path data_dir: by convention this should be the same as session_dir                               containg the input data
        """
        path = Path('/')
        for part in self._get_data_dir_path(data_dir_name).resolve().parts:
            path = path / part
            if not os.path.isdir(path):
                os.mkdir(path)

    def _get_data_dir_path(self, data_dir_name):
        return self._top_level_dir / data_dir_name

import os
from pathlib import Path
import yaml

class CalibrationDataFiler:
    ''' Handles saving and loading calibration data'''

    # Calibration data filenames
    PAN_MOTOR_TIMEBASE_ALIGNMENT = 'pan_motor_timebase_alignment.yml'
    FOV_TIMEBASE_ALIGNMENT = 'fov_timebase_alignment.yml'
    FRAME_RATE_CALCULATION = 'frame_rate_calculation.yml'
    TAG_GPS_TIMEBASE_ALIGNMENT = 'tag_gps_timebase_alignment.yml'

    def __init__(self, top_level_dir: Path):
        self._top_level_dir = top_level_dir

    def save_as_yml(self, obj, data_dir_name: str, calibration_file: str):
        '''
        :param Path data_dir_name: by convention this should be the same as session_dir \
                              containg the input data
        '''
        self._ensure_data_dir(data_dir_name)
        yaml_file = self._get_file_path(data_dir_name, calibration_file)
        with open(yaml_file, 'w') as yaml_file:
            yaml.dump(obj, yaml_file, default_flow_style=False)

    def load(self, data_dir_name: str, calibration_filename: str):
        filepath = self._get_file_path(data_dir_name, calibration_filename)
        if not filepath.is_file():
            return None
        with open(self._get_file_path(data_dir_name, calibration_filename), 'r') as stream:
            return yaml.load(stream)

    def _get_file_path(self, data_dir_name: str, filename: str) -> Path:
        return (self._get_data_dir_path(data_dir_name) / filename).resolve()

    def _ensure_data_dir(self, data_dir_name: str):
        '''
        :param Path data_dir: by convention this should be the same as session_dir \
                              containg the input data
        '''
        path = Path('/')
        for part in self._get_data_dir_path(data_dir_name).resolve().parts:
            path = path / part
            if not os.path.isdir(path):
                os.mkdir(path)

    def _get_data_dir_path(self, data_dir_name):
        return self._top_level_dir / data_dir_name