[open-ils-commits] r1039 - servres/trunk/conifer/integration (gfawcett)

svn at svn.open-ils.org svn at svn.open-ils.org
Wed Oct 13 22:28:54 EDT 2010


Author: gfawcett
Date: 2010-10-13 22:28:52 -0400 (Wed, 13 Oct 2010)
New Revision: 1039

Added:
   servres/trunk/conifer/integration/memoization.py
Modified:
   servres/trunk/conifer/integration/uwindsor.py
Log:
uwindsor integration: proper copy counts.

Added: servres/trunk/conifer/integration/memoization.py
===================================================================
--- servres/trunk/conifer/integration/memoization.py	                        (rev 0)
+++ servres/trunk/conifer/integration/memoization.py	2010-10-14 02:28:52 UTC (rev 1039)
@@ -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

Modified: servres/trunk/conifer/integration/uwindsor.py
===================================================================
--- servres/trunk/conifer/integration/uwindsor.py	2010-10-14 02:28:47 UTC (rev 1038)
+++ servres/trunk/conifer/integration/uwindsor.py	2010-10-14 02:28:52 UTC (rev 1039)
@@ -10,6 +10,7 @@
 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 @@
 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,12 +74,28 @@
     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
 
+ at 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):
     if query.startswith(EG_BASE):
         # query is an Evergreen URL



More information about the open-ils-commits mailing list