"""
Python-PDI - Library for Personal Data Interchange.
Copyright (C) 2002-2003 Peter Gebauer

Provides with nice wrapper functions for parsing from files, strings and list of strings.

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

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 pdi.core import *

def fromFile(fileName,inObject,bufferSize = 4096):
	fromFileObject(open(fileName),inObject,bufferSize)

def fromFileObject(fObj, inObject, bufferSize = 4096):
    """
    Opens a file and parses it line by line.
    If nothing unexpected happens the inObject will be populated and returned.

    @param  fObj: File Object to read from.
    @param  inObject: An instance of a L{pdi.core.Component} to populate.
    @param  bufferSize: The read buffer and maximum size per line. Default is 4k.
    @rtype: L{pdi.core.Component}
    @return:    The same instance of pdi.core.Component passed in as the second argument, only populated.
    """
    currentObject = inObject
    
    line = fObj.readline(bufferSize)
    lineNum = 0
    while line and currentObject:   
        lineNum += 1
        currentObject = currentObject.parseLine(line, lineNum)
        line = fObj.readline(bufferSize)
    fObj.close()    
    return inObject

def fromStrings(list, inObject):
    """
    Itterates over a list of strings and parses them.
    If nothing unexpected happens the inObject will be populated and returned.

    @param  list: A list with strings.
    @param  inObject: An instance of a L{pdi.core.Component} to populate.
    @rtype: L{pdi.core.Component}
    @return:    The same instance of pdi.core.Component passed in as the second argument, only populated.
    """
    currentObject = inObject
    lineNum = 0
    for line in list:    
        lineNum += 1
        currentObject = currentObject.parseLine(line, lineNum)
    return inObject

def fromString(data, inObject, crlf = "\n"):
    """
    Splits a string on CRLF's and parses them.
    If nothing unexpected happens the inObject will be populated and returned.

    @type   data: string
    @param  data: A string containing the data.
    @type   inObject: L{pdi.core.Component}
    @param  inObject: An instance of a pdi.core.Component.
    @type   crlf: string
    @param  crlf: Split on this substring.
    @rtype: L{pdi.core.Component}
    @return:    The same instance of pdi.core.Component passed in as the second argument, only populated.
    """
    unpack = data.split(crlf)
    return fromStrings(unpack,inObject)
