def test_put_cache_complex(self): """Test if put_cache() works for a more complex, practical scenario: As 'source' we just use some random integer as count value. The 'rendered representation' of it is just the word "spam" repeated count times, which we cache. The cache key calculation (for the 'non-guessable' keys) is also rather simple. In real world, source would be likely some big image, rendered representation of it a thumbnail / preview of it. Or some LaTeX source and its rendered representation as png image. Key calculation could be some MAC or some other hard to guess and unique string. """ import random request = self.request render = lambda data: "spam" * data secret = 4223 keycalc = lambda data: str(data * secret) source = random.randint(1, 100) rendered1 = render(source) key1 = keycalc(source) cache.put(request, key1, rendered1) url1 = cache.url(request, key1) assert 'key=%s' % key1 in url1 data_cache = caching.CacheEntry(request, arena=cache.cache_arena, scope=cache.cache_scope, key=key1+'.data') cached1 = data_cache.content() assert render(source) == cached1 # if that succeeds, we have stored the rendered representation of source in the cache under key1 # now we use some different source, render it and store it in the cache source = source * 2 rendered2 = render(source) key2 = keycalc(source) cache.put(request, key2, rendered2) url2 = cache.url(request, key2) assert 'key=%s' % key2 in url2 data_cache = caching.CacheEntry(request, arena=cache.cache_arena, scope=cache.cache_scope, key=key2+'.data') cached2 = data_cache.content() assert render(source) == cached2 # if that succeeds, we have stored the rendered representation of updated source in the cache under key2 assert url2 != url1 # URLs must be different for different source (implies different keys)
def test_put_cache_complex(self): """Test if put_cache() works for a more complex, practical scenario: As 'source' we just use some random integer as count value. The 'rendered representation' of it is just the word "spam" repeated count times, which we cache. The cache key calculation (for the 'non-guessable' keys) is also rather simple. In real world, source would be likely some big image, rendered representation of it a thumbnail / preview of it. Or some LaTeX source and its rendered representation as png image. Key calculation could be some MAC or some other hard to guess and unique string. """ import random request = self.request render = lambda data: "spam" * data secret = 4223 keycalc = lambda data: str(data * secret) source = random.randint(1, 100) rendered1 = render(source) key1 = keycalc(source) cache.put(request, key1, rendered1) url1 = cache.url(request, key1) assert 'key=%s' % key1 in url1 data_cache = caching.CacheEntry(request, arena=cache.cache_arena, scope=cache.cache_scope, key=key1 + '.data') cached1 = data_cache.content() assert render(source) == cached1 # if that succeeds, we have stored the rendered representation of source in the cache under key1 # now we use some different source, render it and store it in the cache source = source * 2 rendered2 = render(source) key2 = keycalc(source) cache.put(request, key2, rendered2) url2 = cache.url(request, key2) assert 'key=%s' % key2 in url2 data_cache = caching.CacheEntry(request, arena=cache.cache_arena, scope=cache.cache_scope, key=key2 + '.data') cached2 = data_cache.content() assert render(source) == cached2 # if that succeeds, we have stored the rendered representation of updated source in the cache under key2 assert url2 != url1 # URLs must be different for different source (implies different keys)
def test_put_cache_file_like_data(self): """Test if put_cache() works when we give it a file like object for the content""" request = self.request key = 'nooneknowsit' filename = "test.png" data = "dontcareatall" data_file = StringIO.StringIO(data) cache.put(request, key, data_file) url = cache.url(request, key) assert key in url meta_cache = caching.CacheEntry(request, arena=cache.cache_arena, scope=cache.cache_scope, key=key + '.meta', use_pickle=True) meta = meta_cache.content() assert meta['httpdate_last_modified'].endswith( ' GMT' ) # only a very rough check, it has used cache mtime as last_modified assert ("Content-Type", "application/octet-stream") in meta['headers'] assert ("Content-Length", len(data)) in meta['headers'] data_cache = caching.CacheEntry(request, arena=cache.cache_arena, scope=cache.cache_scope, key=key + '.data') cached = data_cache.content() assert data == cached
def render(self, formatter): """ renders formular """ _ = self._ # checks if initializing of all attributes in __init__ was done if not self.init_settings: return text = """%(mathtran_server)s?;D=%(scale_factor)s;tex=%(formular)s""" % { "mathtran_server": self.mathtran_server, "formular": wikiutil.url_quote(self.raw), "scale_factor": self.scale_factor, } key = cache.key(self.request, itemname=self.pagename, content=text) if not cache.exists(self.request, key) and urllib: # ToDo add a timeout image = urllib.urlopen(text) cache.put(self.request, key, image.read(), content_type="image/png") if cache.exists(self.request, key): credits = """<a href="http://www.mathtran.org/" title="MoinMoin uses the Mathtran Online translation of mathematical content software.">MathTran Powered</a>""" if not credits in self.request.cfg.page_credits: self.request.cfg.page_credits.append(credits) # ToDo show the credits only on pages using the parser extension return formatter.image(src=cache.url(self.request, key), alt=self.raw)
def test_put_cache_file_like_data(self): """Test if put_cache() works when we give it a file like object for the content""" request = self.request key = 'nooneknowsit' filename = "test.png" data = "dontcareatall" data_file = StringIO.StringIO(data) cache.put(request, key, data_file) url = cache.url(request, key) assert key in url meta_cache = caching.CacheEntry(request, arena=cache.cache_arena, scope=cache.cache_scope, key=key+'.meta', use_pickle=True) meta = meta_cache.content() assert meta['httpdate_last_modified'].endswith(' GMT') # only a very rough check, it has used cache mtime as last_modified assert ("Content-Type", "application/octet-stream") in meta['headers'] assert ("Content-Length", len(data)) in meta['headers'] data_cache = caching.CacheEntry(request, arena=cache.cache_arena, scope=cache.cache_scope, key=key+'.data') cached = data_cache.content() assert data == cached
def execute(macro, args): formatter = macro.formatter macro.request.page.formatter = formatter request = macro.request _ = request.getText if not args: args = request.page.page_name key = cache_key(request, (macro.name, args, latest_edit(request))) map_text = 'usemap="#%s" ' % (key) if not cache_exists(request, key): succ, data, mappi = draw_topology(request, args, key) if not succ: return mappi mappi = encode_page(mappi) cache.put(request, key, data, content_type='image/png') cache.put(request, key + '-map', mappi, content_type='text/html') else: mappifile = cache._get_datafile(request, key + '-map') mappi = mappifile.read() mappifile.close() div = u'<div class="ClarifiedTopology">\n' + \ u'<img %ssrc="%s" alt="%s">\n</div>\n' % \ (map_text, cache.url(request, key), _('topology')) map = u'<map id="%s" name="%s">\n' % (key, key) map += decode_page(mappi) map += u'</map>\n' return div + map
def render(self, formatter): """text to image conversion""" key = 'aafigure_%s' % (cache.key(self.request, itemname=self.pagename, content="%s%s" % (self.raw, self.args)), ) if not cache.exists(self.request, key) or not cache.exists( self.request, key + '_size'): # not in cache, regenerate image options = dict(format='svg') for arg in self.args.split(): try: k, v = arg.split('=', 1) except ValueError: # when splitting fails k = arg v = None if k == 'aspect': options['aspect'] = float(v) elif k == 'scale': options['scale'] = float(v) elif k == 'textual': options['textual'] = True elif k == 'proportional': options['proportional'] = True elif k == 'linewidth': options['linewidth'] = float(v) elif k == 'foreground': options['foreground'] = sanitizte_color(v) elif k == 'fill': options['fill'] = sanitizte_color(v) # no 'background' as SVG backend ignores that # no generic options # XXX unknown options are ignored with no message visitor, output = aafigure.render(self.raw, None, options) cache.put(self.request, key, output.getvalue(), content_type="image/svg+xml") # need to store the size attributes too cache.put(self.request, key + '_size', visitor.get_size_attrs(), content_type="text/plain") # get the information from the cache #~ return formatter.image(src=cache.url(self.request, key), alt=xxx) # XXX this currently only works for HTML, obviously... return formatter.rawHTML( '<object type="image/svg+xml" data="%s" %s></object>' % ( cache.url(self.request, key), cache._get_datafile( self.request, key + '_size').read() # XXX no way to directly read cache? ))
def render(self, formatter): from MoinMoin.action import cache # checks if initializing of all attributes in __init__ was done if not self.init_settings: return # check if diagram on this page has been rendered before key = cache.key(self.request, itemname=self.pagename, content=self.raw) if not cache.exists(self.request, key): image = urllib.urlopen(createYumlUrl(self.raw), proxies=proxies) cache.put(self.request, key, image.read(), content_type="image/png") return formatter.image(src=cache.url(self.request, key), alt=self.raw)
def test_put_cache_minimal(self): """Test if put_cache() works""" request = self.request key = 'nooneknowsit' data = "dontcare" cache.put(request, key, data) url = cache.url(request, key) assert key in url meta_cache = caching.CacheEntry(request, arena=cache.cache_arena, scope=cache.cache_scope, key=key+'.meta', use_pickle=True) meta = meta_cache.content() assert meta['httpdate_last_modified'].endswith(' GMT') # only a very rough check, it has used cache mtime as last_modified assert ("Content-Type", "application/octet-stream") in meta['headers'] assert ("Content-Length", len(data)) in meta['headers']
def test_put_cache_guess_ct_give_lm(self): """Test if put_cache() works, when we give filename (so it guesses content_type) and last_modified""" request = self.request key = 'nooneknowsit' filename = "test.png" data = "dontcare" cache.put(request, key, data, filename=filename, last_modified=1) url = cache.url(request, key) assert key in url meta_cache = caching.CacheEntry(request, arena=cache.cache_arena, scope=cache.cache_scope, key=key+'.meta', use_pickle=True) meta = meta_cache.content() assert meta['httpdate_last_modified'] == 'Thu, 01 Jan 1970 00:00:01 GMT' assert ("Content-Type", "image/png") in meta['headers'] assert ("Content-Length", len(data)) in meta['headers']
def render(self, formatter): """text to image conversion""" key = 'aafigure_%s' % (cache.key(self.request, itemname=self.pagename, content="%s%s" % (self.raw, self.args)),) if not cache.exists(self.request, key) or not cache.exists(self.request, key+'_size'): # not in cache, regenerate image options = dict(format='svg') for arg in self.args.split(): try: k, v = arg.split('=', 1) except ValueError: # when splitting fails k = arg v = None if k == 'aspect': options['aspect'] = float(v) elif k == 'scale': options['scale'] = float(v) elif k == 'textual': options['textual'] = True elif k == 'proportional': options['proportional'] = True elif k == 'linewidth': options['linewidth'] = float(v) elif k == 'foreground': options['foreground'] = sanitizte_color(v) elif k == 'fill': options['fill'] = sanitizte_color(v) # no 'background' as SVG backend ignores that # no generic options # XXX unknown options are ignored with no message visitor, output = aafigure.render(self.raw, None, options) cache.put(self.request, key, output.getvalue(), content_type="image/svg+xml") # need to store the size attributes too cache.put(self.request, key+'_size', visitor.get_size_attrs(), content_type="text/plain") # get the information from the cache #~ return formatter.image(src=cache.url(self.request, key), alt=xxx) # XXX this currently only works for HTML, obviously... return formatter.rawHTML('<object type="image/svg+xml" data="%s" %s></object>' % ( cache.url(self.request, key), cache._get_datafile(self.request, key+'_size').read() # XXX no way to directly read cache? ))
def test_put_cache_guess_ct_give_lm(self): """Test if put_cache() works, when we give filename (so it guesses content_type) and last_modified""" request = self.request key = 'nooneknowsit' filename = "test.png" data = "dontcare" cache.put(request, key, data, filename=filename, last_modified=1) url = cache.url(request, key) assert key in url meta_cache = caching.CacheEntry(request, arena=cache.cache_arena, scope=cache.cache_scope, key=key + '.meta', use_pickle=True) meta = meta_cache.content() assert meta[ 'httpdate_last_modified'] == 'Thu, 01 Jan 1970 00:00:01 GMT' assert ("Content-Type", "image/png") in meta['headers'] assert ("Content-Length", len(data)) in meta['headers']
def test_put_cache_minimal(self): """Test if put_cache() works""" request = self.request key = 'nooneknowsit' data = "dontcare" cache.put(request, key, data) url = cache.url(request, key) assert key in url meta_cache = caching.CacheEntry(request, arena=cache.cache_arena, scope=cache.cache_scope, key=key + '.meta', use_pickle=True) meta = meta_cache.content() assert meta['httpdate_last_modified'].endswith( ' GMT' ) # only a very rough check, it has used cache mtime as last_modified assert ("Content-Type", "application/octet-stream") in meta['headers'] assert ("Content-Length", len(data)) in meta['headers']
def execute(macro, args): request = macro.request # Handle GET arguments level = '4' level_text = '7' if not args: args = '' arglist = [x.strip() for x in args.split(',') if x] if not have_cairo(): return "Cairo not found." key = cache_key(request, (macro.name, arglist)) if not cache_exists(request, key): law = None if len(arglist) == 1: level = args[0] elif len(arglist): level = args[0] if arglist[1].isdigit(): level_text = ','.join(arglist[1:]) else: level_text = '' law = ','.join(arglist[1:]) data = plot_tll(level, level_text, law) cache.put(request, key, data, content_type='image/png') f = macro.formatter divfmt = {"class": "ST"} result = f.div(1, **divfmt) result += f.image(src=cache.url(request, key), alt=LAW.format(level_text)) result += f.div(0) return result
def execute(macro, args): request = macro.request # Handle GET arguments level = '4' level_text = '7' if not args: args = '' arglist = [x.strip() for x in args.split(',') if x] if not cairo_found: return "Cairo not found." key = cache_key(request, (macro.name, arglist)) if not cache_exists(request, key): law = None if len(arglist) == 1: level = args[0] elif len(arglist): level = args[0] if arglist[1].isdigit(): level_text = ','.join(arglist[1:]) else: level_text = '' law = ','.join(arglist[1:]) data = plot_tll(level, level_text, law) cache.put(request, key, data, content_type='image/png') f = macro.formatter divfmt = {"class": "ST"} result = f.div(1, **divfmt) result += f.image(src=cache.url(request, key), alt=LAW.format(level_text)) result += f.div(0) return result
def execute(macro, args): request = macro.request if not cairo_found: return "Cairo not found." if not args: key = DEFAULT elif not hasattr(request.cfg, 'gwiki_markings'): return "No gwiki_markings in configuration." else: try: val = request.cfg['gwiki_markings'][args] key = list() for line in val: if not isinstance(line, unicode): return ("Marking misconfiguration " + "(not a tuple of unicode strings)") key.append((line, CAIRO_BOLD)) except KeyError: return "Marking not in gwiki_markings." level_text = ' '.join(x[0] for x in key) ckey = cache_key(request, (macro.name, key)) if not cache_exists(request, ckey): data = plot_box(key) cache.put(request, ckey, data, content_type='image/png') f = macro.formatter divfmt = {"class": "CM"} result = f.div(1, **divfmt) result += f.image(src=cache.url(request, ckey), alt=level_text) result += f.div(0) return result
def execute(self): request = self.request _ = request.getText pagename = request.page.page_name if not request.user.may.read(pagename): fault = _(u'Can not read page') + u'\n' if self.inline: request.write(request.formatter.text(fault)) return request.content_type = 'text/plain' request.write(fault) return form = values_to_form(request.values) self.formargs(form) if self.help or not self.attachment: enter_page(request, pagename, 'View .gv attachment') self.sendForm() if self.help: # This is the URL addition to the nodes that have graph data self.urladd = url_parameters(form) self.urladd = self.urladd.replace('&help=Inline', '') request.write('<<ViewDot(' + self.urladd + ')>>') exit_page(request, pagename) return if not self.attachment[:10].lower() == 'attachment': fault = _(u'No attachment defined') + u'\n' if self.inline: request.write(request.formatter.text(fault)) return request.content_type = 'text/plain' request.write(fault) return self.attachment = self.attachment[11:] pagename, filename = AttachFile.absoluteName(self.attachment, self.pagename) if not request.user.may.read(pagename): fault = _(u'Can not read attachment page') + u'\n' if self.inline: request.write(request.formatter.text(fault)) return request.content_type = 'text/plain' request.write(fault) return fname = wikiutil.taintfilename(filename) fpath = AttachFile.getFilename(request, pagename, fname) try: data = file(fpath, 'r').read() except IOError: fault = _(u'Can not read attachment') + u'\n' if self.inline: request.write(request.formatter.text(fault)) return request.content_type = 'text/plain' request.write(fault) return if not gv_found: fault = _(u"ERROR: Graphviz Python extensions not installed. " +\ u"Not performing layout.") if self.inline: request.write(request.formatter.text(fault)) return request.content_type = 'text/plain' request.write(fault) return self.cache_key = cache_key(self.request, [data, self.graphengine, self.format]) key = "%s-%s" % (self.cache_key, self.format) if self.format in ['zgr', 'svg']: formatcontent = 'svg+xml' else: formatcontent = self.format if not cache_exists(request, key): graphviz = Graphviz(engine=self.graphengine, string=data) data = self.getLayoutInFormat(graphviz, self.format) cache.put(self.request, key, data, content_type=formatcontent) if self.format in ['zgr', 'svg']: # Display zgr graphs as applets if self.format == 'zgr': image_p = lambda url, text: \ '<applet code="net.claribole.zgrviewer.ZGRApplet.class"'+ \ ' archive="%s/gwikicommon/zgrviewer/zvtm.jar,' % \ (self.request.cfg.url_prefix_static) + \ '%s/gwikicommon/zgrviewer/zgrviewer.jar" ' % \ (self.request.cfg.url_prefix_static) + \ 'width="%s" height="%s">' % (form_escape(self.width), form_escape(self.height))+\ '<param name="type" ' + \ 'value="application/x-java-applet;version=1.4" />' + \ '<param name="scriptable" value="false" />' + \ '<param name="svgURL" value="%s" />' % (url) + \ '<param name="title" value="ZGRViewer - Applet" />'+ \ '<param name="appletBackgroundColor" value="#DDD" />' + \ '<param name="graphBackgroundColor" value="#DDD" />' + \ '<param name="highlightColor" value="red" />' + \ ' </applet><br>\n' else: image_p = lambda url, text: \ '<object data="%s" alt="%s" ' % (url, text) + \ 'type="image/svg+xml">\n' + \ '<embed src="%s" alt="%s" ' % (url, text) + \ 'type="image/svg+xml"/>\n</object>' else: image_p = lambda url, text: \ '<img src="%s" alt="%s">\n' % (url, text) image_uri = cache.url(self.request, key) if not self.inline: if self.format == 'zgr': request.write('<html><body>') request.write(image_p(image_uri, _('visualisation'))) if not self.inline and self.format == 'zgr': request.write('</html></body>') else: pass # No footer
def execute(self): request = self.request _ = request.getText pagename = request.page.page_name if not request.user.may.read(pagename): fault = _(u'Can not read page') + u'\n' if self.inline: request.write(request.formatter.text(fault)) return request.content_type = 'text/plain' request.write(fault) return form = values_to_form(request.values) self.formargs(form) if self.help or not self.attachment: enter_page(request, pagename, 'View .gv attachment') self.sendForm() if self.help: # This is the URL addition to the nodes that have graph data self.urladd = url_parameters(form) self.urladd = self.urladd.replace('&help=Inline', '') request.write('<<ViewDot(' + self.urladd + ')>>') exit_page(request, pagename) return if not self.attachment[:10].lower() == 'attachment': fault = _(u'No attachment defined') + u'\n' if self.inline: request.write(request.formatter.text(fault)) return request.content_type = 'text/plain' request.write(fault) return self.attachment = self.attachment[11:] pagename, filename = AttachFile.absoluteName(self.attachment, self.pagename) if not request.user.may.read(pagename): fault = _(u'Can not read attachment page') + u'\n' if self.inline: request.write(request.formatter.text(fault)) return request.content_type = 'text/plain' request.write(fault) return fname = wikiutil.taintfilename(filename) fpath = AttachFile.getFilename(request, pagename, fname) try: data = file(fpath, 'r').read() except IOError: fault = _(u'Can not read attachment') + u'\n' if self.inline: request.write(request.formatter.text(fault)) return request.content_type = 'text/plain' request.write(fault) return if not have_gv(): fault = _(u"ERROR: Graphviz Python extensions not installed. " +\ u"Not performing layout.") if self.inline: request.write(request.formatter.text(fault)) return request.content_type = 'text/plain' request.write(fault) return self.cache_key = cache_key(self.request, [data, self.graphengine, self.format]) key = "%s-%s" % (self.cache_key, self.format) if self.format in ['zgr', 'svg']: formatcontent = 'svg+xml' else: formatcontent = self.format if not cache_exists(request, key): graphviz = Graphviz(engine=self.graphengine, string=data) data = self.getLayoutInFormat(graphviz, self.format) cache.put(self.request, key, data, content_type=formatcontent) if self.format in ['zgr', 'svg']: # Display zgr graphs as applets if self.format == 'zgr': image_p = lambda url, text: \ '<applet code="net.claribole.zgrviewer.ZGRApplet.class"'+ \ ' archive="%s/gwikicommon/zgrviewer/zvtm.jar,' % \ (self.request.cfg.url_prefix_static) + \ '%s/gwikicommon/zgrviewer/zgrviewer.jar" ' % \ (self.request.cfg.url_prefix_static) + \ 'width="%s" height="%s">' % (form_escape(self.width), form_escape(self.height))+\ '<param name="type" ' + \ 'value="application/x-java-applet;version=1.4" />' + \ '<param name="scriptable" value="false" />' + \ '<param name="svgURL" value="%s" />' % (url) + \ '<param name="title" value="ZGRViewer - Applet" />'+ \ '<param name="appletBackgroundColor" value="#DDD" />' + \ '<param name="graphBackgroundColor" value="#DDD" />' + \ '<param name="highlightColor" value="red" />' + \ ' </applet><br>\n' else: image_p = lambda url, text: \ '<object data="%s" alt="%s" ' % (url, text) + \ 'type="image/svg+xml">\n' + \ '<embed src="%s" alt="%s" ' % (url, text) + \ 'type="image/svg+xml"/>\n</object>' else: image_p = lambda url, text: \ '<img src="%s" alt="%s">\n' % (url, text) image_uri = cache.url(self.request, key) if not self.inline: if self.format == 'zgr': request.write('<html><body>') request.write(image_p(image_uri, _('visualisation'))) if not self.inline and self.format == 'zgr': request.write('</html></body>') else: pass # No footer