Ignore:
Timestamp:
8 Jun 2009, 16:54:29 (16 years ago)
Author:
uli
Message:

Use new CSV file framework.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • waeup/branches/ulif-rewrite/src/waeup/utils/importexport.py

    r4158 r4220  
    77from xml.dom.minidom import Document, getDOMImplementation
    88from zope.interface import Interface
     9from waeup.csvfile.interfaces import ICSVFile
    910from waeup.interfaces import (IWAeUPObject, IWAeUPExporter, IWAeUPXMLExporter,
    1011                              IWAeUPXMLImporter, IWAeUPCSVImporter)
     
    8687        return obj
    8788
    88 class CSVImporter(grok.Adapter):
    89     grok.context(IWAeUPObject)
     89
     90class CSVImporter(grok.MultiAdapter):
     91    grok.adapts(ICSVFile, IWAeUPObject)
    9092    grok.provides(IWAeUPCSVImporter)
    91     grok.baseclass() # We don't want this base to be registered really.
    92     column_terms = [] # The column terms supported by this importer
    93    
    94     def __init__(self, context):
    95         self.context = context
    96    
    97     def doImport(self, filepath):
     93    grok.baseclass()  # We don't want this base to be registered really.
     94
     95    def __init__(self, csvfile, receiver):
     96        self.csvfile = csvfile
     97        self.receiver = receiver
     98
     99    def doImport(self):
    98100        pass
    99    
    100     def getData(self, filepath):
    101         result = readFile(open(filepath, 'rb'))
    102         return result
    103 
    104     def validate(self, filepath):
    105         self.checkHeader(filepath)
    106         self.getData(filepath)
    107         return True
    108 
    109     def checkHeader(self, filepath):
    110         missing = []
    111         headerline = ''
    112 
    113         # Extract the header fields...
    114         for line in open(filepath, 'rb'):
    115             headerline = line
    116             if len(headerline.strip()) == 0:
    117                 continue # Ignore leading empty lines...
    118             break # We only want the first line...
    119         tmpfile = StringIO(headerline)
    120         headerfields = readFile(tmpfile)[0]
    121 
    122         # Find missing header fields...
    123         for key in self.column_terms:
    124             if key not in headerfields:
    125                 missing.append(key)
    126         if len(missing):
    127             raise KeyError(
    128                 'Validation Error: the follwing '
    129                 'columns could not be found: %s' % (', '.join(missing))
    130                 )
    131         return
Note: See TracChangeset for help on using the changeset viewer.