diff options
author | Dan Williams <dcbw@localhost.localdomain> | 2006-09-25 14:56:31 (GMT) |
---|---|---|
committer | Dan Williams <dcbw@localhost.localdomain> | 2006-09-25 14:56:31 (GMT) |
commit | f6c043b2043371837e885bdb3233aaea33565e17 (patch) | |
tree | fe1dc8f4bd1cfa8aa3aebcb0a9b5b8676e846c7d /services | |
parent | ea5b59dca73596d3ceb4f2fb0e2e86522d451371 (diff) |
Add a buddy icon cache
Diffstat (limited to 'services')
-rw-r--r-- | services/presence/BuddyIconCache.py | 61 | ||||
-rw-r--r-- | services/presence/Makefile.am | 5 |
2 files changed, 64 insertions, 2 deletions
diff --git a/services/presence/BuddyIconCache.py b/services/presence/BuddyIconCache.py new file mode 100644 index 0000000..59d5e88 --- /dev/null +++ b/services/presence/BuddyIconCache.py @@ -0,0 +1,61 @@ +import os, time, md5 +from sugar import env +from sugar import util + +class BuddyIconCache(object): + """Caches icons on disk and finds them based on md5 hash.""" + def __init__(self): + ppath = env.get_profile_path() + self._cachepath = os.path.join(ppath, "cache", "buddy-icons") + if not os.path.exists(self._cachepath): + os.makedirs(self._cachepath) + + self._cache = {} + + # Read all cached icons and their sums + for fname in os.listdir(self._cachepath): + m = md5.new() + data = self._get_icon_data(fname) + if len(data) == 0: + continue + m.update(data) + printable_hash = util.printable_hash(m.digest()) + self._cache[printable_hash] = fname + del m + + def _get_icon_data(self, fname): + fd = open(os.path.join(self._cachepath, fname), "r") + data = fd.read() + fd.close() + del fd + return data + + def get_icon(self, printable_hash): + if type(printable_hash) != type(u""): + raise RuntimeError("printable_hash must be a unicode string.") + try: + fname = self._cache[printable_hash] + return self._get_icon_data(fname) + except KeyError: + pass + return None + + def add_icon(self, icon_data): + if len(icon_data) == 0: + return + + m = md5.new() + m.update(icon_data) + printable_hash = util.printable_hash(m.digest()) + if self._cache.has_key(printable_hash): + del m + return + + # Write the icon to disk and add an entry to our cache for it + m.update(time.asctime()) + fname = util.printable_hash(m.digest()) + fd = open(os.path.join(self._cachepath, fname), "w") + fd.write(icon_data) + fd.close() + self._cache[printable_hash] = fname + del m diff --git a/services/presence/Makefile.am b/services/presence/Makefile.am index c095cb6..f64bc02 100644 --- a/services/presence/Makefile.am +++ b/services/presence/Makefile.am @@ -6,11 +6,12 @@ $(service_DATA): $(service_in_files) Makefile @sed -e "s|\@bindir\@|$(bindir)|" $< > $@ sugardir = $(pkgdatadir)/services/presence -sugar_PYTHON = \ +sugar_PYTHON = \ __init__.py \ Activity.py \ Buddy.py \ - PresenceService.py \ + BuddyIconCache.py \ + PresenceService.py \ Service.py bin_SCRIPTS = sugar-presence-service |