def test_parse_invalid_charset(self): with self.assertRaises(exceptions.InvalidCharset): DataURI.make( mimetype='text/plain', charset='*garbled*', base64=True, data= 'VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wZWQgb3ZlciB0aGUgbGF6eSBkb2cu')
def selenium_process_html(html_content, output): chrome_options = webdriver.ChromeOptions() chrome_options.add_argument('headless') capabilities = { 'browserName': 'chrome', 'javascriptEnabled': True, } capabilities.update(chrome_options.to_capabilities()) driver = webdriver.Remote(settings.SELENIUM_URL, desired_capabilities=capabilities) calculated_print_options = { 'landscape': False, 'displayHeaderFooter': False, 'printBackground': True, 'preferCSSPageSize': True, } made = DataURI.make('text/html', charset='utf-8', base64=True, data=html_content) driver.get(made) result = send_devtools(driver, "Page.printToPDF", calculated_print_options) with open(output, 'wb') as file: file.write(base64.b64decode(result['data'])) driver.quit() return True
def svg_marker(text, color='white', background_color='black', direction='se'): text_style = f'font-size:13px;font-family:Roboto;fill:{color};' text_width = int(round(_text_width(text, text_style))) rect_width = text_width + 24 rect_height = 26 translate_x, translate_y, arrow_width, origin_x, origin_y, arrow, = directions[direction](rect_width / 2, rect_width + 8) img_width = rect_width + 2 + arrow_width img_height = rect_height + 2 + 8 view_box = f'-{translate_x} -{translate_y} {img_width} {img_height}' image = ( f'<?xml version="1.0" ?>' f'<svg xmlns="http://www.w3.org/2000/svg" viewBox="{view_box}" width="{img_width}px" height="{img_height}px" >' f'<rect width="{rect_width}" height="{rect_height}" rx="3" ry="3" fill="{background_color}"></rect>' f'<path d="{arrow}" fill="{background_color}"></path>' f'<text y="18" x="12" style="{text_style}">{text}</text>' f'</svg>' ) # NOQA E131 return { 'icon': { 'url': str(DataURI.make('image/svg+xml', charset='utf8', base64=True, data=image)), 'anchor': {'x': origin_x + 1, 'y': origin_y + 1}, }, 'shape': { 'type': 'rect', 'coords': (translate_x, translate_y, rect_width + translate_x, rect_height + translate_y), } }
def check_add(self, item): if not 'file' in item: return if ';' in item['file']['mime']: item['file']['mime'] = item['file']['mime'].split(";")[0] story = { 'name' : item['title'], 'auth' : item['author'], 'tags' : item['tags'], 'desc' : item['desc'], 'ul_date' : item['uldate'], 'fname' : item['file']['name'], 'file' : DataURI.make(mimetype=item['file']['mime'], charset=None, base64=True, data=item['file']['file']), } # print(item['tags']) # print(item['file']) assert 'name' in story assert 'auth' in story assert 'fname' in story assert 'file' in story assert 'desc' in story assert 'tags' in story with app.app.test_request_context(): app.api_handlers.addStory({'story' : story})
def get_vitessce_conf(self, entity): if ('files' not in entity or 'data_types' not in entity): # Would a default no-viz config be better? return {} if self.is_mock: cellsData = json.dumps( {'cell-id-1': { 'mappings': { 't-SNE': [1, 1] } }}) cellsUri = DataURI.make('text/plain', charset='us-ascii', base64=True, data=cellsData) token = 'fake-token' return { 'description': 'DEMO', 'layers': [ { 'name': 'cells', 'type': 'CELLS', 'url': cellsUri, 'requestInit': { 'headers': { 'Authorization': 'Bearer ' + token } } }, ], 'name': 'Linnarsson', 'staticLayout': [ { 'component': 'scatterplot', 'props': { 'mapping': 'UMAP', 'view': { 'zoom': 4, 'target': [0, 0, 0] } }, 'x': 0, 'y': 0, 'w': 12, 'h': 2 }, ] } else: vitessce = Vitessce(entity=entity, nexus_token=self.nexus_token) return vitessce.conf
def _build_image_layer_datauri(self, rel_path): """ Specifically for the RASTERS schema, we need to build a DataURI of the schema because it contains a URL for a file whose location is unknown until pipelines have been run. returns e.g: DataURI.make( 'text/plain', charset='us-ascii', base64=True, data=json.dumps( { 'schema_version': '0.0.1' 'images': [ { 'name': 'example.ome.tiff' 'type': 'ome-tiff' 'url': 'https://assets.dev.hubmapconsortium.org/uuid/example.ome.tiff', } ] } ) ) """ image_layer = {} # For CODEX replace the tiles. Otherwise this next line does nothing. image_paths = [ str(rel_path).replace( "#CODEX_TILE#", f"R{str(r).zfill(3)}_X{str(x).zfill(3)}_Y{str(y).zfill(3)}", ) for (r, x, y) in itertools.product(*[R_VALS, X_VALS, Y_VALS]) ] image_layer["images"] = [ self._build_image_schema(image_path) for image_path in image_paths ] image_layer["schema_version"] = "0.0.1" return DataURI.make("text/plain", charset="us-ascii", base64=True, data=json.dumps(image_layer))
def post(self, photo): req_json = request.get_json() photo.roundtable = db_session.query(Roundtable).filter( Roundtable.roundtable_num == req_json.get( 'roundtable_num')).first() photo.album_name = req_json.get('album_name') photo.album_link = req_json.get('album_link') photo.album_id = req_json.get('album_id') tmp_req = requests.get(req_json.get('represent_img')) represent_img_uri = DataURI.make(tmp_req.headers['Content-Type'], None, base64=True, data=tmp_req.content) photo.representation_image_link = str(represent_img_uri) photo.album_description = req_json.get('description') return jsonify(success=True)
def _get_mock_vitessce_conf(): cellsData = json.dumps({'cell-id-1': {'mappings': {'t-SNE': [1, 1]}}}) cellsUri = DataURI.make('text/plain', charset='us-ascii', base64=True, data=cellsData) token = 'fake-token' return { 'description': 'DEMO', 'layers': [ { 'name': 'cells', 'type': 'CELLS', 'url': cellsUri, 'requestInit': { 'headers': { 'Authorization': 'Bearer ' + token } } }, ], 'name': 'Linnarsson', 'staticLayout': [ { 'component': 'scatterplot', 'props': { 'mapping': 'UMAP', 'view': { 'zoom': 4, 'target': [0, 0, 0] } }, 'x': 0, 'y': 0, 'w': 12, 'h': 2 }, ] }
def save_readable_titles(interactive=False): """Saves a mapping from readable chat titles to chat folders in the dump.""" titles_string = "" for (readable, original) in _get_readable_titles(): readable = readable.strip() if len(readable) == 0: readable = "<Deactivated User>" titles_string += readable + "\n" titles_string += f"Folder: {original}" + "\n\n" titles_string = titles_string[:-1] if interactive: uri = DataURI.make("text/plain", charset="utf-8", base64=True, data=titles_string) display( HTML( f"Generated a <a href='{uri}' target='_blank' style='text-decoration: none;'>" + "mapping" + "</a> from chat titles to folders.")) else: titles_path = os.path.join(_repo_root, "titles.txt") with open(titles_path, "w") as title_file: title_file.write(titles_string)
def test_make_no_charset(self): made = DataURI.make('text/plain', charset=None, base64=True, data='This is a message.') self.assertEqual(made.data, b'This is a message.')
def test_make_base64(self): made = DataURI.make('text/plain', charset='us-ascii', base64=True, data='This is a message.') self.assertEqual(made.text, u'This is a message.')
def test_make(self): made = DataURI.make('text/plain', charset='us-ascii', base64=False, data='This is a message.') self.assertEqual(made.data, 'This is a message.')