def substr_enclosed_in_seq(data, initial_seq, terminal_seq, from_index=0,
    to_index=-1, include_trailing_newline=False):
    """
    Get first substring between initial and terminal key sequence if present in data,
    e.g. if data is '[[my_val]]', initial_seq is '[[', and terminal_seq is ']]',
    the result is {'value': 'my_val', 'start': 0, 'end': 8, 'error': ''};
    :param data: string
    :param initial_seq: string
    :param terminal_seq: string
    :param from_index: index from which to start search
    :param to_index: index where to stop search (not including it), -1 means the data end
    :param include_trailing_newline: if true, result['end'] will be advanced to include trailing new line chars;
    :return: Dict:  'value' (everything between initial and terminal sequence,
                    'start', 'end' (open range, index of next character after teminal_seq end)
                    'error': empty string if no errors, 'syntax' otherwise
    """
    result = {'value': '', 'start': -1, 'end': -1, 'error': ''}
    if to_index == -1:
        start = data.find(initial_seq, from_index)
    else:
        start = data.find(initial_seq, from_index, to_index)
    if start == -1:
        return result
    result['start'] = start
    if to_index == -1:
        end = data.find(terminal_seq, start)
    else:
        end = data.find(terminal_seq, start, to_index)
    if end == -1:
        result['error'] = 'syntax'
        return result
    result_end = end + len(terminal_seq)
    if include_trailing_newline:
        data_len = len(data)
        if data_len - result_end >= 2:
            if data[result_end] == '\r' or data[result_end] == '\n':
                result_end += 1
            if data[result_end] == '\r' or data[result_end] == '\n':
                if data[result_end] != data[result_end - 1]:
                    result_end += 1
        elif data_len - result_end == 1:
            if data[result_end] == '\r' or data[result_end] == '\n':
                result_end += 1
    result['end'] = result_end
    result['value'] = data[start + len(initial_seq):end]
    return result
