First version of csv header to function map
authorJean-Michel Nirgal Vourgère <jmv@nirgal.com>
Fri, 21 Jan 2011 13:43:17 +0000 (13:43 +0000)
committerJean-Michel Nirgal Vourgère <jmv@nirgal.com>
Fri, 21 Jan 2011 13:43:17 +0000 (13:43 +0000)
bin/common.py

index 334b6c2d7b958627a05cafdff23330934ff6d6d9..eb67f8a85339a03c06649c7fc1931821cfbd0543 100755 (executable)
@@ -1275,6 +1275,30 @@ class Nmea(Nmea1, Nmea5):
         'eta',
         'draught',
         ]
+    
+    '''
+    Maps the csv header name to matching function to call to get the data.
+    '''
+    csv_name_to_function = {
+        'mmsi': lambda nmea: nmea.mmsi,
+        'flag': get_flag,
+        'name': Nmea5.get_name,
+        'imo': lambda nmea: str(nmea.imo),
+        'callsign': Nmea5.get_callsign,
+        'type': lambda nmea: str(nmea.type) + '-' + get_shiptype(nmea),
+        'length':lambda nmea: str(nmea.get_length(nmea)),
+        'width': lambda nmea: str(nmea.get_width(nmea)),
+        'datetime': lambda nmea: datetime.utcfromtimestamp(nmea.get_last_timestamp()).strftime('%Y-%m-%dT%H:%M:%SZ'),
+        'status': Nmea1.get_status,
+        'sog': Nmea1.get_sog_str,
+        'latitude': Nmea1.get_latitude_str,
+        'longitude': Nmea1.get_longitude_str,
+        'cog': Nmea1.get_cog_str,
+        'heading': Nmea1.get_heading_str,
+        'destination': Nmea5.get_destination,
+        'eta': Nmea5.get_eta_str,
+        'draught': Nmea5.get_draught_str,
+    }
 
     def get_dump_row(self):
         result = []
@@ -1282,11 +1306,10 @@ class Nmea(Nmea1, Nmea5):
             if txt is None:
                 return ''
             return txt.replace('\0','').replace('@', '').strip()
+        f = csv_name_to_function['mmsi']
         result.append(self.strmmsi)
-        country_mid = int(self.strmmsi[:3])
-        country_name = COUNTRIES_MID.get(country_mid, u'unknown')
-        result.append(country_name.encode('utf-8'))
-        result.append(_clean(self.name))
+        result.append(self.get_flag().encode('utf-8'))
+        result.append(self.get_name())
         result.append(str(self.imo))
         result.append(_clean(self.callsign))
         result.append(str(self.type) + '-' + SHIP_TYPES.get(self.type, 'unknown'))