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: image_rev = None if is_binary_image(path): is_text = False contents = '' image_rev = revision else: is_text, contents = decode_data(contents, tree_config.source_encoding) if not is_text: contents = '' elif is_textual_image(path): image_rev = revision # We do some wrapping to mimic the JSON returned by an ES lines query. return _browse_file(tree, path, [{'content': line} for line in split_content_lines(contents)], {}, config, not is_text, contents=contents, image_rev=image_rev) 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. with open(path, 'rb') as source_file: contents = source_file.read() # always str _, contents = decode_data(contents, encoding_guess) return contents # unicode if we were able to decode, str if not
def unicode_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, or else None. :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. with open(path, 'rb') as source_file: initial_portion = source_file.read(4096) if not is_binary_string(initial_portion): # Move the cursor back to the start of the file. source_file.seek(0) decoded, contents = decode_data(source_file.read(), encoding_guess, can_be_binary=False) if decoded: return contents