def testNonSourceFile(self): prettify_data = prettify.BuildPrettifyData(0, '/dev/null') self.assertDictEqual( dict(should_prettify=ezt.boolean(False), prettify_class=None), prettify_data) prettify_data = prettify.BuildPrettifyData(10, 'readme.txt') self.assertDictEqual( dict(should_prettify=ezt.boolean(False), prettify_class=None), prettify_data)
def testThirdPartyExtensionLanguages(self): for ext in [ 'apollo', 'agc', 'aea', 'el', 'scm', 'cl', 'lisp', 'go', 'hs', 'lua', 'fs', 'ml', 'proto', 'scala', 'sql', 'vb', 'vbs', 'vhdl', 'vhd', 'wiki', 'yaml', 'yml', 'clj' ]: prettify_data = prettify.BuildPrettifyData( 123, '/trunk/src/hello.' + ext) self.assertDictEqual( dict(should_prettify=ezt.boolean(True), prettify_class='lang-' + ext), prettify_data)
def testExactFilename(self): prettify_data = prettify.BuildPrettifyData(123, 'trunk/src/Makefile') self.assertDictEqual( dict(should_prettify=ezt.boolean(True), prettify_class='lang-sh'), prettify_data)
def testSpecificLanguage(self): prettify_data = prettify.BuildPrettifyData(123, 'trunk/src/hello.java') self.assertDictEqual( dict(should_prettify=ezt.boolean(True), prettify_class='lang-java'), prettify_data)
def testGenericLanguage(self): prettify_data = prettify.BuildPrettifyData(123, 'trunk/src/hello.php') self.assertDictEqual( dict(should_prettify=ezt.boolean(True), prettify_class=''), prettify_data)
def GatherPageData(self, mr): """Parse the attachment ID from the request and serve its content. Args: mr: commonly used info parsed from the request. Returns: Dict of values used by EZT for rendering almost the page. """ with mr.profiler.Phase('get issue, comment, and attachment'): try: attachment, issue = tracker_helpers.GetAttachmentIfAllowed( mr, self.services) except exceptions.NoSuchIssueException: webapp2.abort(404, 'issue not found') except exceptions.NoSuchAttachmentException: webapp2.abort(404, 'attachment not found') except exceptions.NoSuchCommentException: webapp2.abort(404, 'comment not found') content = [] if attachment.gcs_object_id: bucket_name = app_identity.get_default_gcs_bucket_name() full_path = '/' + bucket_name + attachment.gcs_object_id logging.info("reading gcs: %s" % full_path) with cloudstorage.open(full_path, 'r') as f: content = f.read() filesize = len(content) # This servlet only displays safe textual attachments. The user should # not have been given a link to this servlet for any other kind. if not attachment_helpers.IsViewableText(attachment.mimetype, filesize): self.abort(400, 'not a text file') u_text, is_binary, too_large = filecontent.DecodeFileContents(content) lines = prettify.PrepareSourceLinesForHighlighting( u_text.encode('utf8')) config = self.services.config.GetProjectConfig(mr.cnxn, mr.project_id) granted_perms = tracker_bizobj.GetGrantedPerms(issue, mr.auth.effective_ids, config) page_perms = self.MakePagePerms(mr, issue, permissions.DELETE_ISSUE, permissions.CREATE_ISSUE, granted_perms=granted_perms) page_data = { 'issue_tab_mode': 'issueDetail', 'local_id': issue.local_id, 'filename': attachment.filename, 'filesize': template_helpers.BytesKbOrMb(filesize), 'file_lines': lines, 'is_binary': ezt.boolean(is_binary), 'too_large': ezt.boolean(too_large), 'code_reviews': None, 'page_perms': page_perms, } if is_binary or too_large: page_data['should_prettify'] = ezt.boolean(False) else: page_data.update( prettify.BuildPrettifyData(len(lines), attachment.filename)) return page_data