]> code.delx.au - monosys/commitdiff
opal-card-tool: refactoring
authorJames Bunton <jamesbunton@delx.net.au>
Mon, 23 Feb 2015 22:01:36 +0000 (09:01 +1100)
committerJames Bunton <jamesbunton@delx.net.au>
Mon, 23 Feb 2015 22:01:36 +0000 (09:01 +1100)
scripts/opal-card-tool

index bbe6fdeff6f31f43fb31699c5b1e0e52bca6eda3..8f20b8840c41ef08adad918180baff574e5830b5 100755 (executable)
@@ -201,34 +201,44 @@ class CommuterGraph(object):
         out = csv.writer(f, dialect=self.gnuplot_dialect)
         return out, f
 
-    def write_points(self, transaction_list):
+    def get_oldest_date(self):
         oldest = datetime.datetime.now() - datetime.timedelta(days=12)
         oldest = oldest.replace(hour=0, minute=0, second=0, microsecond=0)
-        for transaction in transaction_list:
-            ts = transaction.timestamp
-            if ts < oldest:
-                return
-            if not is_weekday(ts):
-                continue
+        return oldest
+
+    def update_xrange(self, ts):
+        if self.xrange_start is None or ts < self.xrange_start:
+            self.xrange_start = ts
+        if self.xrange_end is None or ts > self.xrange_end:
+            self.xrange_end = ts
+
+    def generate_point(self, transaction):
+        ts = transaction.timestamp
+        x_date = ts.strftime("%Y-%m-%dT00:00:00")
+        y_time = ts.strftime("2000-01-01T%H:%M:00")
+        y_label = ts.strftime("%H:%M")
+        return [x_date, y_time, y_label]
+
+    def write_point(self, ts, point):
+        if ts.time() < datetime.time(12):
+            out_csv = self.data_am_csv
+        else:
+            out_csv = self.data_pm_csv
 
-            if self.xrange_start is None or ts < self.xrange_start:
-                self.xrange_start = ts
-            if self.xrange_end is None or ts > self.xrange_end:
-                self.xrange_end = ts
+        out_csv.writerow(point)
 
-            x_date = ts.strftime("%Y-%m-%dT00:00:00")
-            y_time = ts.strftime("2000-01-01T%H:%M:00")
-            y_label = ts.strftime("%H:%M")
-            row = [x_date, y_time, y_label]
+    def write_points(self, transaction_list):
+        oldest_date = self.get_oldest_date()
 
-            if ts.time() < datetime.time(12):
-                out_csv = self.data_am_csv
-            else:
-                out_csv = self.data_pm_csv
-            out_csv.writerow(row)
+        for transaction in transaction_list:
+            if transaction.timestamp < oldest_date:
+                return
+            if not is_weekday(transaction.timestamp):
+                continue
 
-        self.data_am_file.flush()
-        self.data_pm_file.flush()
+            self.update_xrange(transaction.timestamp)
+            point = self.generate_point(transaction)
+            self.write_point(transaction.timestamp, point)
 
     def write_plot_command(self):
         d = {