# -*- coding: iso-8859-1 -*-
"""
    Cachetest

    this macro is used to find a problem of fuid

    @copyright: 2009 MoinMoin:ReimarBauer

    @license: GNU GPL, see COPYING for details.
"""
from MoinMoin import caching, wikiutil
from MoinMoin.action import AttachFile, cache

def macro_Cachetest(macro, attachment=unicode):
    request = macro.request
    pagename = request.page.page_name

    Image = wikiutil.importWikiPlugin(request.cfg, "macro", "Image", function="Image")
    size = (128, 128)
    cache_arena = 'sendcache'
    cache_scope = 'wiki'
    do_locking = False
    if attachment and AttachFile.exists(request, pagename, attachment):
        itemname = pagename + '/' + attachment
        img = Image(request, itemname)
        content_type = img.content_type
        size_str = '%d_%d' % size
        key = '%s_%s_%s' % (content_type.replace('/', '_'), size_str, img._cache_key)
        meta_cache = caching.CacheEntry(request, cache_arena, key+'.meta', cache_scope, do_locking=do_locking, use_pickle=True)
        result = """
IMG Cache key: %s <BR>     
Key: %s <BR>   
Cache exists: %s <BR>     
Meta cache exists: %s <BR>
Needs Update: %s <BR> 
""" % (img._cache_key, key, cache.exists(request, key), meta_cache.exists(), meta_cache.needsUpdate(img._filename))
        return result
    
    return "Attachment not found"