added Line class as container for stuff like station of line, departure
authorFlorian Schweikert <kelvan@logic.at>
Fri, 14 Oct 2011 19:03:53 +0000 (21:03 +0200)
committerFlorian Schweikert <kelvan@logic.at>
Fri, 14 Oct 2011 19:03:53 +0000 (21:03 +0200)
of station on line

gotovienna/realtime.py

index be36453..e8ecb47 100644 (file)
@@ -5,6 +5,7 @@ from urllib2 import urlopen
 from datetime import time
 import re
 import collections
+from errors import LineNotFoundError, StationNotFoundError
 
 from gotovienna import defaults
 
@@ -178,3 +179,35 @@ def categorize_lines(lines):
     return [(LINE_TYPE_NAMES[key], categorized_lines[key])
             for key in sorted(categorized_lines)]
 
+
+class Line:
+    def __init__(self, name):
+        self._stations = None
+        self.parser = ITipParser()
+        if name.strip() in self.parser.lines():
+            self.name = name.strip()
+        else:
+            raise LineNotFoundError('There is no line "%s"' % name.strip())
+        
+    @property
+    def stations(self):
+        if not self._stations:
+            self._stations = parser.get_stations(self.name)
+        return self._stations
+    
+    def get_departures(self, stationname):
+        stationname = stationname.strip().lower()
+        stations = self.stations
+        
+        found = false
+        
+        for direction in stations.keys():
+            # filter stations starting with stationname
+            stations[direction] = filter(lambda station: station[0].lower().starts_with(stationname), stations)
+            found = found or bool(stations[direction])
+        
+        if found:
+            # TODO return departures
+            raise NotImplementedError()
+        else:
+            raise StationNotFoundError('There is no stationname called "%s" at route of line "%s"' % (stationname, self.name))
\ No newline at end of file