def go(): image_source = image_source_from_env(e) library = library_from_env(e) with timeit_wall('graph_generic', 1.0): key = (e.library_name, e.spec.url_part, e.thing_name, text_hash) if not key in self.last_processed2: logger.error('Cannot find key %s' % str(key)) logger.error('keys: %s' % list(self.last_processed2)) context = e.library._generate_context_with_hooks() thing = e.spec.load(e.library, e.thing_name, context=context) else: thing = self.last_processed2[key] if thing is None: return response_image(e.request, 'Could not parse.') with timeit_wall('graph_generic - get_png_data', 1.0): data = e.spec.get_png_data(image_source=image_source, name=e.thing_name, thing=thing, data_format=data_format, library=library) mime = get_mime_for_format(data_format) return response_data(e.request, data, mime)
def create_a_to_data(download, data_format, data): """ Returns a tag with base64 encoded data """ assert data_format in ['pdf', 'png'] from mcdp_web.images.images import (get_mime_for_format) mime = get_mime_for_format(data_format) encoded = base64.b64encode(data) href = 'data:%s;base64,%s' % (mime, encoded) attrs = dict(href=href, download=download) return Tag(name='a', attrs=attrs)
def create_a_to_data(soup, download, data_format, data): """ Returns a tag with base64 encoded data """ assert data_format in ['pdf', 'png'] mime = get_mime_for_format(data_format) encoded = base64.b64encode(data) href = 'data:%s;base64,%s' % (mime, encoded) attrs = dict(href=href, download=download) print('download: %s' % download) return soup.new_tag('a', **attrs)
def data_encoded_for_src(data, ext): """ data = ext = png, jpg, ... returns "data: ... " sttring """ from mcdp_web.images.images import get_mime_for_format # XXX: move encoded = base64.b64encode(data) mime = get_mime_for_format(ext) link = 'data:%s;base64,%s' % (mime, encoded) return link
def allformats_posets_report(id_poset, poset, libname, which): from mcdp_web.images.images import get_mime_for_format from mcdp_library_tests.tests import get_test_library r = Report(id_poset + '-' + which) library = get_test_library(libname) image_source = ImagesFromPaths(library.get_images_paths()) mf = MakeFiguresPoset(poset=poset, image_source=image_source) formats = mf.available_formats(which) res = mf.get_figure(which, formats) fig = r.figure() for f in formats: data = res[f] mime = get_mime_for_format(f) dn = DataNode(f, data=data, mime=mime) fig.add_child(dn) return r
def embed_images(html, basedir): """ Embeds png and Jpg images using data """ soup = BeautifulSoup(html, 'lxml', from_encoding='utf-8') for tag in soup.select('img'): href = tag['src'] extensions = ['png', 'jpg'] for ext in extensions: if ext in href and not 'data:' in href: resolve = os.path.join(basedir, href) with open(resolve) as ff: data = ff.read() encoded = base64.b64encode(data) from mcdp_web.images.images import get_mime_for_format mime = get_mime_for_format(ext) src = 'data:%s;base64,%s' % (mime, encoded) tag['src'] = src return str(soup)
def go(): data_format = str(request.matchdict['data_format']) # unicode library = self.get_library(request) widget_name = self.get_widget_name(request, spec) library_name = self.get_current_library_name(request) key = (library_name, spec, widget_name) if not key in self.last_processed2: l = self.get_library(request) context = l._generate_context_with_hooks() thing = spec.load(l, widget_name, context=context) else: thing = self.last_processed2[key] if thing is None: return response_image(request, 'Could not parse.') data = spec.get_png_data(library, widget_name, thing, data_format=data_format) from mcdp_web.images.images import get_mime_for_format mime = get_mime_for_format(data_format) return response_data(request, data, mime)
def allformats_report(id_ndp, ndp, libname, which): from mcdp_web.images.images import get_mime_for_format r = Report(id_ndp + '-' + which) from mcdp_library_tests.tests import get_test_library library = get_test_library(libname) mf = MakeFiguresNDP(ndp=ndp, library=library, yourname=id_ndp) formats = mf.available_formats(which) try: res = mf.get_figure(which, formats) except DPSemanticError as e: if 'Cannot abstract' in str(e): r.text('warning', 'Not connected. \n\n %s' % e) return r print('%s -> %s %s ' % (which, formats, map(len, [res[f] for f in formats]))) fig = r.figure() for f in formats: data = res[f] mime = get_mime_for_format(f) dn = DataNode(f, data=data, mime=mime) fig.add_child(dn) return r
def allformats_report(id_ndp, ndp, libname, which): from mcdp_web.images.images import get_mime_for_format from mcdp_library_tests.tests import get_test_library r = Report(id_ndp + '-' + which) library = get_test_library(libname) image_source = ImagesFromPaths(library.get_images_paths()) mf = MakeFiguresNDP(ndp=ndp, image_source=image_source, yourname=id_ndp) formats = mf.available_formats(which) try: res = mf.get_figure(which, formats) except DPSemanticError as e: if 'Cannot abstract' in str(e): r.text('warning', 'Not connected. \n\n %s' % e) return r print('%s -> %s %s ' % (which, formats, map(len, [res[f] for f in formats]))) fig = r.figure() for f in formats: data = res[f] mime = get_mime_for_format(f) dn = DataNode(f, data=data, mime=mime) fig.add_child(dn) return r
def embed_images_from_library(html, library): """ Resolves images from library """ def resolve(href): #print('resolving %r' % href) f = library._get_file_data(href) data = f['data'] # realpath = f['realpath'] return data soup = BeautifulSoup(html, 'lxml', from_encoding='utf-8') for tag in soup.select('img'): href = tag['src'] extensions = ['png', 'jpg'] for ext in extensions: if ext in href and not 'data:' in href: data = resolve(href) encoded = base64.b64encode(data) from mcdp_web.images.images import get_mime_for_format mime = get_mime_for_format(ext) src = 'data:%s;base64,%s' % (mime, encoded) tag['src'] = src return str(soup)
def gg_figure(r, name, ggraph, do_png=True, do_pdf=True, do_svg=True, do_dot=True): """ Adds a figure to the Report r that displays this graph and also its source. """ f = r.figure(name, cols=1) # save file in dot file with tmpfile(".dot") as filename_dot: with open(filename_dot, 'w') as fo: s = get_dot_string(ggraph) fo.write(s) # if False: # ff = '%s.dot' % id(r) # print('writing to %r' % ff) # with open(ff, 'w') as f2: # f2.write(s) prog = 'dot' try: if do_png: with f.data_file('graph', MIME_PNG) as filename: graphviz_run(filename_dot, filename, prog=prog) if do_pdf: with f.data_file('graph_pdf', MIME_PDF) as filename: graphviz_run(filename_dot, filename, prog=prog) if do_svg: with f.data_file('graph_svg', MIME_SVG) as filename: graphviz_run(filename_dot, filename, prog=prog) soup = BeautifulSoup(open(filename).read(), 'lxml', from_encoding='utf-8') for tag in soup.select('image'): href = tag['xlink:href'] extensions = ['png', 'jpg'] for ext in extensions: if ext in href: with open(href) as ff: png = ff.read() encoded = base64.b64encode(png) from mcdp_web.images.images import get_mime_for_format mime = get_mime_for_format(ext) src = 'data:%s;base64,%s' % (mime, encoded) tag['xlink:href'] = src with codecs.open(filename, 'w', encoding='utf-8') as ff: s = str(soup) u = unicode(s, 'utf-8') ff.write(u) except CmdException: if MCDPConstants.test_ignore_graphviz_errors: mcdp_dev_warning('suppressing errors from graphviz') logger.error('Graphivz failed, but I will ignore it ' 'because of MCDPConstants.test_ignore_graphviz_errors.') else: raise # MIME_GRAPHVIZ if do_dot: with f.data_file('dot', MIME_PLAIN) as filename: with open(filename, 'w') as f: f.write(s) return f