From 597fead93976d0ac408b30f0f96dd8e50e514294 Mon Sep 17 00:00:00 2001 From: "Adam J. Gamble" Date: Mon, 26 Nov 2012 23:32:01 +0000 Subject: [PATCH] Added some [belated] exception handling. * namedtuple mapping catch errors with headings, partial fix. * fix: pass instead of break to send options.headings for all ARGS, lazier. --- colly/commands/__init__.py | 5 +++-- colly/commands/diff.py | 5 +++++ colly/core.py | 25 +++++++++++++++++-------- colly/exceptions.py | 2 ++ 4 files changed, 27 insertions(+), 10 deletions(-) create mode 100644 colly/exceptions.py diff --git a/colly/commands/__init__.py b/colly/commands/__init__.py index a1431f2..82eeb07 100644 --- a/colly/commands/__init__.py +++ b/colly/commands/__init__.py @@ -5,6 +5,7 @@ import optparse from colly.optbase import parser from colly.core import Collate +from colly.exceptions import CommandError __all__ = ['command_dict', 'Command', 'load_command', 'load_all_commands', 'command_names'] @@ -101,12 +102,12 @@ class CsvCommand(Command): try: kwargs['headings'] = options.headings[n] except IndexError: - break + pass elif options.index: try: kwargs['headings'] = [] # leave out index for now except IndexError: - break + pass else: pass diff --git a/colly/commands/diff.py b/colly/commands/diff.py index 8440292..ad87bd7 100644 --- a/colly/commands/diff.py +++ b/colly/commands/diff.py @@ -2,6 +2,7 @@ import sys import logging from colly.commands import CsvCommand +from colly.exceptions import CommandError class DiffCommand(CsvCommand): name = 'diff' @@ -48,6 +49,10 @@ class DiffCommand(CsvCommand): 'all' : A.column.union(B.column) }[options.method] + if not ('pk' in A.headings or 'pk' in B.headings): + logging.warning("pk should be set, for csv file A and B") + raise CommandError("Please set 'pk' columns for both files") + for row in filtered: try: print A.get_row(row) diff --git a/colly/core.py b/colly/core.py index 6424f49..24d7535 100644 --- a/colly/core.py +++ b/colly/core.py @@ -4,11 +4,14 @@ import logging import csv from collections import namedtuple +from colly.exceptions import CsvImportError + import simplejson as json class Collate(object): _rows = {} + headings = "" ''' Csv parsing and formatting. ''' @@ -24,18 +27,19 @@ class Collate(object): f.seek(0) # reset iter to first row ^ else: headings = first_row - if 'pk' in options: - try: - pk = int(options['pk']) - headings[pk] = 'pk' #: denote primary key - except ValueError: - logging.warning('Ignoring pk, should be a number denoting the primary key column') + logging.info('Interpretting headings as row #1') +# if 'pk' in options: +# try: +# pk = int(options['pk']) +# headings[pk] = 'pk' #: denote primary key +# except ValueError: +# logging.warning('Ignoring pk, should be a number denoting the primary key column') cL = len(first_row) #: column length hL = len(headings) if hL != cL: if cL < hL: - raise "Given headings exceeded those in CSV" + raise CsvImportError("Given headings exceeded those in CSV") else: ''' Pad extra columns alphabeticaly * fieldnames have to be unique, & not empty, or number. @@ -43,14 +47,19 @@ class Collate(object): pad = list(string.uppercase[hL:cL]) #:FIX/ limited to 26 column CSV headings += pad + self.headings = headings logging.info("CSV columns given headings: %s" % (headings)) ''' OK, columns should be in order, ready to generate a map of the @csv_file. ''' - Head = namedtuple("row", ",".join(headings), verbose=False) index, pk = {}, 0 #: set empty vars + try: + Head = namedtuple("row", ",".join(headings), verbose=False) + except ValueError, err: + raise CsvImportError(err) + for row in map(Head._make, raw): ''' Perhaps not immediately obvious, index contains the full dataset. The "pk" (primary key) should be unique, or if diff --git a/colly/exceptions.py b/colly/exceptions.py new file mode 100644 index 0000000..269616e --- /dev/null +++ b/colly/exceptions.py @@ -0,0 +1,2 @@ +class CsvImportError(Exception): pass +class CommandError(Exception): pass -- 2.11.4.GIT