def view_capture_stream(telstate, capture_block_id, stream_name):
    """Create telstate view based on given capture block ID and stream name.

    It constructs a view on `telstate` with at least the prefixes

      - <capture_block_id>_<stream_name>
      - <capture_block_id>
      - <stream_name>

    Additionally if there is a <stream_name>_inherit key, that stream is
    added too (recursively).

    Parameters
    ----------
    telstate : :class:`katsdptelstate.TelescopeState` object
        Original telescope state
    capture_block_id : string
        Capture block ID
    stream_name : string
        Stream name

    Returns
    -------
    telstate : :class:`~katdal.sensordata.TelescopeState` object
        Telstate with a view that incorporates capture block, stream and combo
    """
    streams = [stream_name]
    while True:
        inherit = telstate.view(streams[-1], exclusive=True).get('inherit')
        if inherit is None:
            break
        streams.append(inherit)
    streams.reverse()

    for stream in streams:
        telstate = telstate.view(stream)
    telstate = telstate.view(capture_block_id)
    for stream in streams:
        capture_stream = telstate.join(capture_block_id, stream)
        telstate = telstate.view(capture_stream)
    return telstate