-
Notifications
You must be signed in to change notification settings - Fork 8
/
cache.py
50 lines (46 loc) · 1.63 KB
/
cache.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import wx
import urllib2
import os
from StringIO import StringIO
class Cache(object):
def __init__(self):
self.cache = {}
def get_bitmap(self, key):
if key is None or isinstance(key, wx.Bitmap):
return key
if key not in self.cache:
if os.path.exists(key):
self.cache[key] = wx.Bitmap(key)
else:
fp = urllib2.urlopen(key)
data = fp.read()
fp.close()
img = wx.ImageFromStream(StringIO(data))
self.cache[key] = img.ConvertToBitmap()
return self.cache[key]
def get_color(self, key):
if key is None or isinstance(key, wx.Colour):
return key
if key not in self.cache:
self.cache[key] = wx.Colour(*key)
return self.cache[key]
def get_font(self, key):
if key is None or isinstance(key, wx.Font):
return key
if key not in self.cache:
self.cache[key] = self.make_font(key)
return self.cache[key]
def make_font(self, key):
face, size, bold, italic, underline = key
family = wx.FONTFAMILY_DEFAULT
style = wx.FONTSTYLE_ITALIC if italic else wx.FONTSTYLE_NORMAL
weight = wx.FONTWEIGHT_BOLD if bold else wx.FONTWEIGHT_NORMAL
font = wx.Font(size, family, style, weight, underline, face)
return font
DEFAULT_CACHE = Cache()
def get_bitmap(key):
return DEFAULT_CACHE.get_bitmap(key)
def get_color(key):
return DEFAULT_CACHE.get_color(key)
def get_font(key):
return DEFAULT_CACHE.get_font(key)