root / cachers / cached_image.py

Revision 165:6811f8d6cb25, 1.1 kB (checked in by mercurial@…, 14 months ago)

merge

Line 
1import os
2import logging
3import cached_url, thumbnail
4from urllib2 import urlopen
5from StringIO import StringIO
6
7#logging.basicConfig(level=logging.DEBUG,
8#                    format='%(asctime)s %(levelname)s %(message)s',
9#                    filename='cached.log',
10#                    filemode='w')
11
12console = logging.StreamHandler()
13console.setLevel(logging.DEBUG)
14formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
15console.setFormatter(formatter)
16logging.getLogger('').addHandler(console)
17
18def get_content(url, width=None, height=None, scale=None, control=False):
19    """get cached content"""
20    key = '%s::%s::%s::%s' % (url, str(height), str(width), str(scale))
21    content = cached_url.mc.get(key)
22    if content is not None:
23        logging.debug('%s cached' % key)
24        return content
25    infos, content = cached_url.get_content(url, control)
26    try:
27        content = thumbnail.generate(StringIO(content), width, height, scale)
28    except IOError:
29        content = None
30    cached_url.mc.set(key, (infos, content))
31    logging.debug('%s caching' % key)
32    return infos, content
Note: See TracBrowser for help on using the browser.