# -*- coding: utf-8 -*-
from __future__ import unicode_literals

import re

from .keylevel import _KeyLevel, LineKey, Regex, BooleanKey
from .helpers import ReaderError, SUPPRESS, Namespace
from .py23compat import py23_items, py23_values, py23_basestring


class _KeyAdder(_KeyLevel):
    """An abstract base class that knows how to add keys to itself
    and check the keys read within it."""

    def __init__(self, case=False):
        """Initiallizes the key holders in this class"""
        super(_KeyAdder, self).__init__(case=case)

        # Default the key dictionary
        self._keys  = {}

        # The mutually exclusive groups
        self._meg = []

    def _ensure_default_has_a_value(self, kwargs):
        if 'default' not in kwargs:
            kwargs['default'] = self._default

    def _check_keyname(self, keyname, strid):
        """Run the given keyname through a few checks"""
        # Check that the keyname is valid
        if not isinstance(keyname, py23_basestring):
            raise ValueError('{0}: {1} must be str'.format(repr(keyname), strid))
        # Check that the keyname is not defined twice
        if keyname in self._keys:
            raise ReaderError('The {0} "{1}" has been defined twice'.format(strid, keyname))
        # Adjust keyname if this is case sensitive
        if not self._case:
            keyname = keyname.lower()
        return keyname