#
# Copyright 2012, Couchbase, Inc.
# All Rights Reserved
#
# Licensed under the Apache License, Version 2.0 (the "License")
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

sources = [{'type':'csv', 'class':'CSVReader', 'example':'csv://<filename>'}]
destinations = []

import csv
import migrator


class CSVReader(migrator.Reader):
    def __init__(self, source):
        if source[0:2] == "//":
            source = source[2:]
        self.reader = csv.DictReader(open(source, 'rb'))
        self.reader.fieldnames[0] = 'id'

    def __iter__(self):
        return self

    def next(self):
        data = self.reader.next()
        if data:
            record = {'id': data['id']}
            record['value'] = (dict((k, v) for (k, v) in data.iteritems()
                               if k != 'id' and not k.startswith('_')))
            return record
        else:
            raise StopIteration()


class CSVWriter(migrator.Writer):
    pass
