import unicodedata
import re


def _key_from_url(url: str) -> str:
    """
    Convert a URL str to one valid for use as a file name or dict key.  URL Protocols are
    removed entirely.  The returned string will have characters in the set [a-zA-Z.-_].

    Parameters
    ----------
    url : str
        A URL string

    Returns
    -------
    str
        A filename-safe string

    Example
    -------
    >>> url = _key_from_url('http://test.alyx.internationalbrainlab.org/')
   'test.alyx.internationalbrainlab.org'
    """
    url = unicodedata.normalize('NFKC', url)  # Ensure ASCII
    url = re.sub('^https?://', '', url).strip('/')  # Remove protocol and trialing slashes
    url = re.sub(r'[^.\w\s-]', '_', url.lower())  # Convert non word chars to underscore
    return re.sub(r'[-\s]+', '-', url)