from pathlib import Path


def _transport_data(t):
    """Return data from "transport = data".

    Transport 'file' returns a transfer directory (defaulting to
    '$HOME/.CONSTELLATION/REST'.
    Transport 'sftp' returns a hostname, directory tuple; the directory
    defaults to '.CONSTELLATION/REST'."""

    ix = t.find('=')
    if t.startswith('file'):
        if ix==-1:
            return str(Path.home() / '.CONSTELLATION/REST')
        else:
            return t[ix+1:].strip()
    elif t.startswith('sftp'):
        if ix==-1:
            raise ValueError(f'Separator "=" not found in "{t}"')

        host = t[ix+1:].strip()
        if ':' in host:
            host, directory = host.split(':')
            host = host.strip()
            directory = directory.strip()
        else:
            directory =  '.CONSTELLATION/REST'

        if not host:
            raise ValueError('A non-empty host must be specified')
        if not directory:
            raise ValueError('A non-empty directory must be specified')

        return host, directory
    else:
        raise ValueError('Only "file" and "sftp" transports accepted')