'Split the input files by molecule records.'

def split(cls, input_datasets, subdir_generator_function, split_params):

    if (split_params is None):
        return None
    if (len(input_datasets) > 1):
        raise Exception('SD-file    splitting    does    not    support    multiple    files')
    input_files = [ds.file_name for ds in input_datasets]
    chunk_size = None
    if (split_params['split_mode'] == 'number_of_parts'):
        raise Exception(('Split    mode    "%s"    is    currently    not    implemented    for    SD-files.' % split_params['split_mode']))
    elif (split_params['split_mode'] == 'to_size'):
        chunk_size = int(split_params['split_size'])
    else:
        raise Exception(('Unsupported    split    mode    %s' % split_params['split_mode']))
    def _read_sdf_records(filename):
        lines = []
        with open(filename) as handle:
            for line in handle:
                lines.append(line)
                if line.startswith('$$$$'):
                    (yield lines)
                    lines = []
    def _write_part_sdf_file(accumulated_lines):
        part_dir = subdir_generator_function()
        part_path = os.path.join(part_dir, os.path.basename(input_files[0]))
        part_file = open(part_path, 'w')
        part_file.writelines(accumulated_lines)
        part_file.close()
    try:
        sdf_records = _read_sdf_records(input_files[0])
        sdf_lines_accumulated = []
        for (counter, sdf_record) in enumerate(sdf_records, start=1):
            sdf_lines_accumulated.extend(sdf_record)
            if ((counter % chunk_size) == 0):
                _write_part_sdf_file(sdf_lines_accumulated)
                sdf_lines_accumulated = []
        if sdf_lines_accumulated:
            _write_part_sdf_file(sdf_lines_accumulated)
    except Exception as e:
        log.error(('Unable    to    split    files:    %s' % str(e)))
        raise
