def test_cache_key_attachment(self):
        request = self.request
        pagename = self.pagename
        attachname = 'foo.txt'

        become_trusted(request)
        create_page(request, pagename, u"Foo!")

        AttachFile.add_attachment(request, pagename, attachname, "Test content1", True)

        result1 = cache.key(request, itemname=pagename, attachname=attachname, secret='bar')
        result2 = cache.key(request, itemname=pagename, attachname=attachname, secret='baz')
        assert result1  # not empty
        assert result1 != result2  # different for different secret

        # test below does not work, because mtime is often same, inode can be same due to how add_attachment
        # works, file size is same, attachment name is same, wikiname/pagename is same.
        # In practice, this should rather rarely cause problems:
        #AttachFile.add_attachment(request, pagename, attachname, "Test content2", True)
        #result3 = cache.key(request, itemname=pagename, attachname=attachname, secret='baz')
        #assert result3 != result2  # different for different content

        AttachFile.add_attachment(request, pagename, attachname, "Test content33333", True)
        result4 = cache.key(request, itemname=pagename, attachname=attachname, secret='baz')
        assert len(result4) == len(result2)  # same length of key for different input lengths
        nuke_page(request, pagename)
 def test_cache_key_content(self):
     request = self.request
     result1 = cache.key(request, content='foo', secret='bar')
     result2 = cache.key(request, content='foo', secret='baz')
     assert result1  # not empty
     assert result1 != result2  # different for different secret
     result3 = cache.key(request, content='foofoo', secret='baz')
     assert result3 != result2  # different for different content
     result4 = cache.key(request, content='foo'*1000, secret='baz')
     assert len(result4) == len(result3)  # same length of key for different input lengths
Exemplo n.º 3
0
 def test_cache_key_content(self):
     request = self.request
     result1 = cache.key(request, content='foo', secret='bar')
     result2 = cache.key(request, content='foo', secret='baz')
     assert result1  # not empty
     assert result1 != result2  # different for different secret
     result3 = cache.key(request, content='foofoo', secret='baz')
     assert result3 != result2  # different for different content
     result4 = cache.key(request, content='foo' * 1000, secret='baz')
     assert len(result4) == len(
         result3)  # same length of key for different input lengths
Exemplo n.º 4
0
    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)
Exemplo n.º 5
0
def cache_key(request, parts):
    data = StringIO.StringIO()

    # Make sure that repr of the object is unique
    for part in parts:
        data.write(repr(part))

    data = data.getvalue()

    return cache.key(request, content=data)
Exemplo n.º 6
0
def cache_key(request, parts):
    data = StringIO.StringIO()

    # Make sure that repr of the object is unique
    for part in parts:
        data.write(repr(part))

    data = data.getvalue()

    return cache.key(request, content=data)
Exemplo n.º 7
0
    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?
            ))
Exemplo n.º 8
0
    def test_cache_key_attachment(self):
        request = self.request
        pagename = self.pagename
        attachname = 'foo.txt'

        become_trusted(request)
        create_page(request, pagename, u"Foo!")

        AttachFile.add_attachment(request, pagename, attachname,
                                  "Test content1", True)

        result1 = cache.key(request,
                            itemname=pagename,
                            attachname=attachname,
                            secret='bar')
        result2 = cache.key(request,
                            itemname=pagename,
                            attachname=attachname,
                            secret='baz')
        assert result1  # not empty
        assert result1 != result2  # different for different secret

        # test below does not work, because mtime is often same, inode can be same due to how add_attachment
        # works, file size is same, attachment name is same, wikiname/pagename is same.
        # In practice, this should rather rarely cause problems:
        #AttachFile.add_attachment(request, pagename, attachname, "Test content2", True)
        #result3 = cache.key(request, itemname=pagename, attachname=attachname, secret='baz')
        #assert result3 != result2  # different for different content

        AttachFile.add_attachment(request, pagename, attachname,
                                  "Test content33333", True)
        result4 = cache.key(request,
                            itemname=pagename,
                            attachname=attachname,
                            secret='baz')
        assert len(result4) == len(
            result2)  # same length of key for different input lengths
        nuke_page(request, pagename)
Exemplo n.º 9
0
    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)
Exemplo n.º 10
0
    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?
        ))