# -*- coding: utf-8 -*-
# Copyright (C) Duncan Macleod (2014)
#
# This file is part of GWpy.
#
# GWpy is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# GWpy is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GWpy.  If not, see <http://www.gnu.org/licenses/>.

"""Read events from an Omega-format ASCII file.
"""

import re

from astropy.io.ascii import core

from ...io import registry
from .. import (Table, EventTable)


class OmegaHeader(core.BaseHeader):
    """Read a multi-line column-definition header
    """

    def get_cols(self, lines):
        """Initialize Column objects from a multi-line ASCII header

        Parameters
        ----------
        lines : `list`
            List of table lines
        """
        re_name_def = re.compile("^\s*%\s+(?P<colname>\w+)")
        self.names = []
        for line in lines:
            if not line.startswith('%'):
                break  # End of header lines
            else:
                match = re_name_def.search(line)
                if match:
                    self.names.append(match.group('colname'))

        if not self.names:
            raise core.InconsistentTableError(
                'No column names found in Omega header')

        self.cols = []
        for n in self.names:
            col = core.Column(name=n)
            self.cols.append(col)

    def write(self, lines):
        for name in self.colnames:
            lines.append('%% %s' % name)


class OmegaData(core.BaseData):
    comment = '%'


class Omega(core.BaseReader):
    """Read an Omega file
    """
    _format_name = 'omega'
    _io_registry_can_write = True
    _description = 'Omega format table'

    header_class = OmegaHeader
    data_class = OmegaData


# register ascii.omega for EventTable
registry.register_reader(
    'ascii.omega', EventTable, registry.get_reader('ascii.omega', Table))
