From 17be337122582a354cca9c97c2b9168e56d140fe Mon Sep 17 00:00:00 2001 From: gfawcett Date: Thu, 14 Oct 2010 02:28:52 +0000 Subject: [PATCH] uwindsor integration: proper copy counts. git-svn-id: svn://svn.open-ils.org/ILS-Contrib/servres/trunk@1039 6d9bc8c9-1ec2-4278-b937-99fde70a366f --- conifer/integration/memoization.py | 39 ++++++++++++++++++++++++++++++++++++++ conifer/integration/uwindsor.py | 33 ++++++++++++++++++++++++++++---- 2 files changed, 68 insertions(+), 4 deletions(-) create mode 100644 conifer/integration/memoization.py diff --git a/conifer/integration/memoization.py b/conifer/integration/memoization.py new file mode 100644 index 0000000..51cabd6 --- /dev/null +++ b/conifer/integration/memoization.py @@ -0,0 +1,39 @@ +# http://code.activestate.com/recipes/325905/ (r5) +# Recipe 325905: Memoize Decorator with Timeout (Python), by "S W" +# +import time + +class memoize(object): + """Memoize With Timeout""" + _caches = {} + _timeouts = {} + + def __init__(self,timeout=2): + self.timeout = timeout + + def collect(self): + """Clear cache of results which have timed out""" + for func in self._caches: + cache = {} + for key in self._caches[func]: + if (time.time() - self._caches[func][key][1]) < self._timeouts[func]: + cache[key] = self._caches[func][key] + self._caches[func] = cache + + def __call__(self, f): + self.cache = self._caches[f] = {} + self._timeouts[f] = self.timeout + + def func(*args, **kwargs): + kw = sorted(kwargs.items()) + key = (args, tuple(kw)) + try: + v = self.cache[key] + if (time.time() - v[1]) > self.timeout: + raise KeyError + except KeyError: + v = self.cache[key] = f(*args,**kwargs),time.time() + return v[0] + func.func_name = f.func_name + + return func diff --git a/conifer/integration/uwindsor.py b/conifer/integration/uwindsor.py index a459f33..1cb84a4 100644 --- a/conifer/integration/uwindsor.py +++ b/conifer/integration/uwindsor.py @@ -10,6 +10,7 @@ from conifer.libsystems.z3950 import pyz3950_search as PZ from xml.etree import ElementTree as ET import re import uwindsor_campus_info +from memoization import memoize def department_course_catalogue(): """ @@ -48,6 +49,14 @@ EG_BASE = 'http://%s/' % settings.EVERGREEN_GATEWAY_SERVER initialize(EG_BASE) +# Item status stuff + +STATUS_DECODE = [(str(x['id']), x['name']) + for x in E1('open-ils.search.config.copy_status.retrieve.all')] +AVAILABLE = [id for id, name in STATUS_DECODE if name == 'Available'][0] + +RESERVES_DESK_NAME = 'Leddy: Course Reserves - Main Bldng - 1st Flr - Reserve Counter at Circulation Desk' + def item_status(item): """ Given an Item object, return three numbers: (library, desk, @@ -65,10 +74,26 @@ def item_status(item): the item. The ServiceDesk object has an 'external_id' attribute which should represent the desk in the ILS. """ - if item.bib_id and item.bib_id[-1] in '02468': - return (8, 4, 2) - else: - return (2, 0, 0) + return _item_status(item.bib_id) + +CACHE_TIME = 300 + +@memoize(timeout=CACHE_TIME) +def _item_status(bib_id): + if bib_id: + counts = E1('open-ils.search.biblio.copy_counts.location.summary.retrieve', + bib_id, 1, 0) + lib = desk = avail = 0 + for org, callnum, loc, stats in counts: + print (org, callnum, loc, stats) + avail_here = stats.get(AVAILABLE, 0) + anystatus_here = sum(stats.values()) + if loc == RESERVES_DESK_NAME: + desk += anystatus_here + avail += avail_here + lib += anystatus_here + print (lib, desk, avail) + return (lib, desk, avail) def cat_search(query, start=1, limit=10): -- 2.11.0