def callback(request, context):
log.debug('<STR_LIT>', request.method, request.url)<EOL>assert request.url.startswith('<STR_LIT>')<EOL>path = os.path.join(HERE, request.url.replace('<STR_LIT>', '<STR_LIT>')<EOL>.replace('<STR_LIT>', '<STR_LIT>').replace('<STR_LIT:/>', os.sep))<EOL>if os.path.isfile(path):<EOL><INDENT>context.status_code = <NUM_LIT:200><EOL>with open(path, '<STR_LIT:rb>') as resp_fp:<EOL><INDENT>data = resp_fp.read()<EOL><DEDENT>log.debug('<STR_LIT>', len(data), path)<EOL>return data<EOL><DEDENT>if os.path.isdir(path) and os.path.isfile(os.path.join(path, '<STR_LIT>')):<EOL><INDENT>path = os.path.join(path, '<STR_LIT>')<EOL>context.status_code = <NUM_LIT:200><EOL>with open(path, '<STR_LIT:rb>') as resp_fp:<EOL><INDENT>data = resp_fp.read()<EOL><DEDENT>log.debug('<STR_LIT>', len(data), path)<EOL>return data<EOL><DEDENT>if BUILD_CACHE:<EOL><INDENT>folder = os.path.dirname(path)<EOL>try:<EOL><INDENT>if not os.path.isdir(folder):<EOL><INDENT>os.makedirs(folder)<EOL><DEDENT><DEDENT>except OSError:<EOL><INDENT>orig_folder = folder<EOL>while os.path.abspath(folder) != os.path.abspath(HERE):<EOL><INDENT>if os.path.isfile(folder):<EOL><INDENT>os.rename(folder, folder + '<STR_LIT>')<EOL>os.makedirs(orig_folder)<EOL>os.rename(folder + '<STR_LIT>', os.path.join(folder, '<STR_LIT>'))<EOL>break<EOL><DEDENT>folder = os.path.dirname(folder)<EOL><DEDENT><DEDENT>urllib_request = Request(request.url, request.body if request.method == '<STR_LIT:POST>' else None, request.headers)<EOL>try:<EOL><INDENT>real_http = urlopen(urllib_request)<EOL><DEDENT>except HTTPError as exc:<EOL><INDENT>context.status_code = exc.code<EOL>return None<EOL><DEDENT>with open(path, '<STR_LIT:wb>') as resp_fp:<EOL><INDENT>data = real_http.read()<EOL>resp_fp.write(data)<EOL><DEDENT>if data[:<NUM_LIT:2>] == b'<STR_LIT>':  <EOL><INDENT>with gzip.open(path) as zipf:<EOL><INDENT>data = zipf.read()<EOL><DEDENT>with open(path, '<STR_LIT:wb>') as resp_fp:<EOL><INDENT>resp_fp.write(data)<EOL><DEDENT><DEDENT>context.status_code = real_http.getcode()<EOL>log.debug('<STR_LIT>', context.status_code, len(data))<EOL>return data<EOL><DEDENT>context.status_code = <NUM_LIT><EOL>log.debug('<STR_LIT>', path)<EOL>return None<EOL>
request handler for requests.mock