--- /dev/null
+# 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
from xml.etree import ElementTree as ET
import re
import uwindsor_campus_info
+from memoization import memoize
def department_course_catalogue():
"""
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,
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):