Commits

Robert Brewer committed c80be7b

Some import tweaks

Comments (0)

Files changed (2)

flowrate/__init__.py

         mi.load_mint_csv(csvfile.file)
 
         page = open(os.path.join(uidir, "import.html"), "rb").read()
-        errors = ["<tr><td>Unknown account name: %s</td></tr>" % a
-                  for a in mi.unknown_accounts]
+        errors = ["<tr><td>Unknown account name: %s</td>"
+                      "<td>Description: %s</td></tr>" %
+                  (e.account_name, e.tx['Description'])
+                  for e in mi.errors]
         if errors:
             body = ('<p>Errors encountered:</p>\n'
                     '<table id="errors">\n' + "\n".join(errors) + '</table>')

flowrate/csvutil.py

         reader = csv.DictReader(f, dialect=dialect)
 
         for tx in reader:
-            if tx['Transaction Type'] == 'credit':
-                credit = self.get_account_id(tx['Category'])
-                debit = self.get_account_id(tx['Account Name'])
-            else:
-                credit = self.get_account_id(tx['Account Name'])
-                debit = self.get_account_id(tx['Category'])
-            # Let subclasses override get_account_id if they
-            # want to add new accountids, otherwise pass
-            if credit is None or debit is None:
-                continue
+            self.process_tx(tx)
 
-            amount = decimal.Decimal(tx['Amount'])
-            month, day, year = map(int, tx['Date'].split('/'))
-            postdate = datetime.date(year, month, day)
-            self.add_tx(credit, debit, amount, postdate, tx['Description'])
+    def process_tx(self, tx):
+        """Process the given transaction row from a CSV DictReader."""
+        if tx['Transaction Type'] == 'credit':
+            credit = self.get_account_id(tx['Category'])
+            debit = self.get_account_id(tx['Account Name'])
+        else:
+            credit = self.get_account_id(tx['Account Name'])
+            debit = self.get_account_id(tx['Category'])
+        # Let subclasses override get_account_id if they
+        # want to add new accountids, otherwise pass
+        if credit is None or debit is None:
+            return
+
+        amount = decimal.Decimal(tx['Amount'])
+        month, day, year = map(int, tx['Date'].split('/'))
+        postdate = datetime.date(year, month, day)
+        self.add_tx(credit, debit, amount, postdate, tx['Description'])
+
+
+class ImportException(Exception):
+    pass
+
+
+class UnknownAccount(ImportException):
+
+    def __init__(self, account_name):
+        self.account_name = account_name
 
 
 class ErrorGatheringMintImporter(MintImporter):
 
     def __init__(self):
-        self.unknown_accounts = []
+        self.errors = []
+
+    def process_tx(self, tx):
+        try:
+            result = MintImporter.process_tx(self, tx)
+        except ImportException, exc:
+            exc.tx = tx
+            self.errors.append(exc)
 
     def get_account_id(self, account_name):
         acctid = Importer.get_account_id(self, account_name)
         if acctid is None:
-            self.unknown_accounts.append(account_name)
+            raise UnknownAccount(account_name)
         return acctid