caching for lines and stations (saved as json)
authorFlorian Schweikert <kelvan@logic.at>
Sun, 23 Oct 2011 20:46:22 +0000 (22:46 +0200)
committerFlorian Schweikert <kelvan@logic.at>
Sun, 23 Oct 2011 20:46:22 +0000 (22:46 +0200)
gotovienna/cache.py
gotovienna/realtime.py

index edb6677..eae95b1 100644 (file)
@@ -29,10 +29,10 @@ class Lines(dict):
     def __init__(self):
         self.load()
 
-    def update(self, *args, **kwargs):
-        a = dict.update(self, *args, **kwargs)
+    def __setitem__(self, *args, **kwargs):
+        s = dict.__setitem__(self, *args, **kwargs)
         self.save()
-        return a
+        return s
 
     def save(self):
         with open(defaults.cache_lines, 'w') as fp:
@@ -45,7 +45,6 @@ class Lines(dict):
 
 lines = Lines()
 
-
 class Stations(dict):
     stations = None
 
@@ -63,10 +62,9 @@ class Stations(dict):
         # FIXME maybe cause problems in the future, race conditions
         Stations.stations[line] = self
 
-    def update(self, *args, **kwargs):
-        u = dict.update(self, *args, **kwargs)
-        if args[0]:
-            Stations.save()
+    def __setitem__(self, *args, **kwargs):
+        u = dict.__setitem__(self, *args, **kwargs)
+        Stations.save()
         return u
 
     @classmethod
index d0f276a..73f927c 100644 (file)
@@ -6,6 +6,8 @@ from datetime import time
 import re
 import collections
 from errors import LineNotFoundError, StationNotFoundError
+import cache
+from cache import Stations
 
 from gotovienna import defaults
 
@@ -40,19 +42,18 @@ class Departure:
 
 class ITipParser:
     def __init__(self):
-        self._stations = {}
-        self._lines = {}
+        self._lines = cache.lines
 
     def get_stations(self, name):
         """ Get station by direction
         {'Directionname': [('Station name', 'url')]}
         """
-        if not self._stations.has_key(name):
-            st = {}
+        if not name in self.lines:
+            return {}
 
-            if not self.lines.has_key(name):
-                return None
+        st = Stations(name)
 
+        if not st:
             bs = BeautifulSoup(urlopen(self.lines[name]))
             tables = bs.findAll('table', {'class': 'text_10pix'})
             for i in range(2):
@@ -66,9 +67,8 @@ class ITipParser:
                         sta.append((tr.text.strip('&nbsp;'), None))
 
                 st[dir] = sta
-            self._stations[name] = st
 
-        return self._stations[name]
+        return st
 
     @property
     def lines(self):