def sanitize_from_stream(stream, config):
for line in io.TextIOWrapper(stream, encoding="<STR_LIT:utf-8>"):<EOL><INDENT>line = line.rstrip("<STR_LIT:\n>")<EOL>if not config:<EOL><INDENT>yield line<EOL>continue<EOL><DEDENT>insert_into_match = INSERT_INTO_PATTERN.match(line)<EOL>if not insert_into_match:<EOL><INDENT>yield line<EOL>continue<EOL><DEDENT>table_name = insert_into_match.group("<STR_LIT>")<EOL>column_names = parse_column_names(insert_into_match.group("<STR_LIT>"))<EOL>sanitizers = {}<EOL>for index, column_name in enumerate(column_names):<EOL><INDENT>sanitizer = config.get_sanitizer_for(<EOL>table_name=table_name,<EOL>column_name=column_name,<EOL>)<EOL>if sanitizer:<EOL><INDENT>sanitizers[index] = sanitizer<EOL><DEDENT><DEDENT>if len(sanitizers) == <NUM_LIT:0>:<EOL><INDENT>yield line<EOL>continue<EOL><DEDENT>sanitized_value_tuples = []<EOL>for values in parse_values(insert_into_match.group("<STR_LIT>")):<EOL><INDENT>if len(column_names) != len(values):<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>sanitized_values = []<EOL>for index, value in enumerate(values):<EOL><INDENT>sanitizer_callback = sanitizers.get(index)<EOL>if sanitizer_callback:<EOL><INDENT>value = sanitizer_callback(value)<EOL><DEDENT>sanitized_values.append(encode_mysql_literal(value))<EOL><DEDENT>sanitized_value_tuples.append(sanitized_values)<EOL><DEDENT>yield "<STR_LIT>" % (<EOL>table_name,<EOL>"<STR_LIT:U+002CU+0020>".join("<STR_LIT>" + column_name + "<STR_LIT>" for column_name in column_names),<EOL>"<STR_LIT:U+002C>".join(<EOL>"<STR_LIT:(>" + "<STR_LIT:U+002C>".join(value_tuple) + "<STR_LIT:)>"<EOL>for value_tuple in sanitized_value_tuples<EOL>),<EOL>)<EOL><DEDENT>
Reads dump of MySQL database from given stream and sanitizes it.

:param stream: Stream where the database dump is expected to be available
               from, such as stdout of `mysqldump` process.
:type stream: file

:param config: Optional sanitizer configuration to be used for sanitation
               of the values stored in the database.
:type config: database_sanitizer.config.Configuration|None