#!/usr/bin/env python

"""
 CsvReader.py
 
 Una piccola classe Python che implementa un generatore
 (nuova caratteristica di Python 2.1)  che permette di
 leggere i dati di un file CSV con delimitatori di riga
 e di colonna personalizzabili. 
	
 Copyright (C) 2000-2003 Michele Ferretti
 black.bird@tiscali.it
 http://www.blackbirdblog.it

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

"""

from __future__ import generators

class CsvReader:
	
	def	__init__(self, filein, colsdel=';', rowsdel='\n'):
		" Costruttore della classe "
		self.__colsdel 	=	colsdel
		self.__rowsdel 	=	rowsdel
		self.__fin = open(filein,'r')
		self.__contenuto = self.__fin.read().split(self.__rowsdel)
		
	def __len__(self):
		return len(self.__contenuto)
	
	def	__iter__(self):
		for	record in self.__contenuto:
			yield record.split(self.__colsdel)
	
	def __del__(self):			
		self.__fin.close()
