def rev(tree, revision, path): """Display a page showing the file at path at specified revision by obtaining the contents from version control. """ config = current_app.dxr_config tree_config = config.trees[tree] abs_path = join(tree_config.source_folder, path) contents = file_contents_at_rev(abs_path, revision) if contents is not None and is_text(contents): contents = contents.decode(tree_config.source_encoding) # We do some wrapping to mimic the JSON returned by an ES lines query. return _browse_file(tree, path, [{'content': line} for line in contents.splitlines(True)], {}, config, contents=contents) else: raise NotFound
def rev(tree, revision, path): """Display a page showing the file at path at specified revision by obtaining the contents from version control. """ config = current_app.dxr_config tree_config = config.trees[tree] abs_path = join(tree_config.source_folder, path) contents = file_contents_at_rev(abs_path, revision) if contents is not None and is_text(contents): contents = contents.decode(tree_config.source_encoding) # We do some wrapping to mimic the JSON returned by an ES lines query. return _browse_file(tree, path, [{ 'content': line } for line in contents.splitlines(True)], {}, config, contents=contents) else: raise NotFound
def file_contents(path, encoding_guess): # TODO: Make accessible to TreeToIndex.post_build. """Return the unicode contents of a file if we can figure out a decoding. Otherwise, return the contents as a string. :arg path: A sufficient path to the file :arg encoding_guess: A guess at the encoding of the file, to be applied if it seems to be text """ # Read the binary contents of the file. # If mime.is_text() says it's text, try to decode it using encoding_guess. # If that works, return the resulting unicode. # Otherwise, return the binary string. with open(path, 'rb') as source_file: contents = source_file.read() # always str if is_text(contents): try: contents = contents.decode(encoding_guess) except UnicodeDecodeError: pass # Leave contents as str. return contents