def build_mock_open_side_effect(string_d, stream_d=None, add_names=True):
if stream_d is None:<EOL><INDENT>stream_d = {}<EOL><DEDENT>assert(len(set(string_d.keys()).intersection(set(stream_d.keys()))) == <NUM_LIT:0>)<EOL>def mock_open_side_effect(*args, **kwargs):<EOL><INDENT>if args[<NUM_LIT:0>] in string_d:<EOL><INDENT>r = StringIO.StringIO(string_d[args[<NUM_LIT:0>]])<EOL>if add_names:<EOL><INDENT>r.name = args[<NUM_LIT:0>]<EOL><DEDENT>return r<EOL><DEDENT>elif args[<NUM_LIT:0>] in stream_d:<EOL><INDENT>r = stream_d[args[<NUM_LIT:0>]]<EOL>if add_names:<EOL><INDENT>r.name = args[<NUM_LIT:0>]<EOL><DEDENT>return r<EOL><DEDENT>else:<EOL><INDENT>raise IOError("<STR_LIT>" + args[<NUM_LIT:0>] + "<STR_LIT>" +<EOL>"<STR_LIT:U+002CU+0020>".join(string_d.keys()) + "<STR_LIT>" +<EOL>"<STR_LIT:U+002CU+0020>".join(stream_d.keys()))<EOL><DEDENT><DEDENT>return mock_open_side_effect<EOL>
Build a mock open side effect using a dictionary of content for the files.

:param string_d:  keys are file names, values are string file contents
:param stream_d:  keys are file names, values are stream of contents
:param add_names: if True, each StringIO object will have a .name attribute
                  added to it.