"""
Contains common utility functions. Its imperative that this
module must not depend upon any other gtransport module[s]
"""
import re


def normalize_header_name(name):
    """
    Utility method to convert the given name to a proper http
    header name based on RFC2616. This function does two things:

    1. Identify all the words and title case them
    (for ex. "hello world" converts to "Hello World")
    2. All the identified word boundaries are replaced by a dash.
    (in the above ex. "Hello World" converts to "Hello-World")

    @param name: str, name of the header
    """
    if name is None:
        return None
    return '-'.join(
        [
            word.title()
            for word in re.findall(r'[a-zA-Z0-9]+', str(name))
            if word
        ]
    )


class GlobalState(object):
    """Maintains global state configuration"""

    def __init__(self, config, transport_supplier):
        """
        @param config: _config.Configuration, the global configuration object
        @param transport_supplier: TransportClientSupplier, the supplier
        of a transport client
        """
        self._config = config
        self._transport_supplier = transport_supplier

    @property
    def config(self):
        """
        Returns the global config object
        @rtype: gtransport._config.Configuration
        """
        return self._config

    @property
    def client_supplier(self):
        """
        Returns the global transport supplier
        @return: the tranport client supplier object
        @rtype: gtransport._transport_client.TransportClientSupplier
        """
        return self._transport_supplier
