[chirp_devel] [PATCH 1 of 3] [csv] Refactor line parsing to allow dialects. #533

Tom Hayward
Sat Feb 9 12:44:55 PST 2013


# HG changeset patch
# User Tom Hayward <tom at tomh.us>
# Date 1360439120 28800
# Node ID f501ac229f9b1be1c24dab889b75ac40f27efd16
# Parent  e917718c875d0b7931991811eb65ec9c627334ca
[csv] Refactor line parsing to allow dialects. #533

diff -r e917718c875d -r f501ac229f9b chirp/generic_csv.py
--- a/chirp/generic_csv.py	Tue Feb 05 21:32:18 2013 -0800
+++ b/chirp/generic_csv.py	Sat Feb 09 11:45:20 2013 -0800
@@ -106,6 +106,19 @@
 
         return rf
 
+    def _clean(self, headers, line, mem):
+        """Runs post-processing functions on new mem objects.
+
+        This is useful for parsing other CSV dialects when multiple columns
+        convert to a single Chirp column."""
+
+        for attr in dir(mem):
+            fname = "_clean_%s" % attr
+            if hasattr(self, fname):
+                mem = getattr(self, fname)(headers, line, mem)
+        
+        return mem
+
     def _parse_csv_data_line(self, headers, line):
         mem = chirp_common.Memory()
         try:
@@ -114,7 +127,11 @@
         except OmittedHeaderError:
             pass
 
-        for header, (typ, attr) in self.ATTR_MAP.items():
+        for header in headers:
+            try:
+                typ, attr = self.ATTR_MAP[header]
+            except KeyError:
+                continue
             try:
                 val = get_datum_by_header(headers, line, header)
                 if not val and typ == int:
@@ -128,7 +145,7 @@
             except Exception, e:
                 raise Exception("[%s] %s" % (attr, e))
 
-        return mem
+        return self._clean(headers, line, mem)
 
     def load(self, filename=None):
         if filename is None and self._filename is None:



More information about the chirp_devel mailing list