def read_chunk(filename, offset=-<NUM_LIT:1>, length=-<NUM_LIT:1>, escape_data=False):
try:<EOL><INDENT>length = int(length)<EOL>offset = int(offset)<EOL><DEDENT>except ValueError:<EOL><INDENT>return {}<EOL><DEDENT>if not os.path.isfile(filename):<EOL><INDENT>return {}<EOL><DEDENT>try:<EOL><INDENT>fstat = os.stat(filename)<EOL><DEDENT>except Exception:<EOL><INDENT>return {}<EOL><DEDENT>if offset == -<NUM_LIT:1>:<EOL><INDENT>offset = fstat.st_size<EOL><DEDENT>if length == -<NUM_LIT:1>:<EOL><INDENT>length = fstat.st_size - offset<EOL><DEDENT>with open(filename, "<STR_LIT:r>") as fp:<EOL><INDENT>fp.seek(offset)<EOL>try:<EOL><INDENT>data = fp.read(length)<EOL><DEDENT>except IOError:<EOL><INDENT>return {}<EOL><DEDENT><DEDENT>if data:<EOL><INDENT>data = _escape_data(data) if escape_data else data<EOL>return dict(offset=offset, length=len(data), data=data)<EOL><DEDENT>return dict(offset=offset, length=<NUM_LIT:0>)<EOL>
Read a chunk of a file from an offset upto the length.