# A very small Fortran file reader class
# $Jeannot: FortranLineReader.py,v 1.3 2004/11/02 07:32:45 js Exp $

# Copyright (c) 2001-2004, Jean-Sebastien Roy (js@jeannot.org)

# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:

# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

"""
A very simple python class to read Fortran files

FortranLineReader is a minimalist class to read some kind of Fortran files.

Basic use is as follow:
	# Define a format
	# 2 floats on 4 places, a blank, a char, an int on 2 places
	format = "2F4,X,A,I2"

	# Create a Fortran line reader object
	reader = FortranLineReader(format)
	
	# Apply reader to each line of the file
	for line in myFile:
		values = reader(line)
		# values is a list of 2 floats, a char, and an int.
"""

import re

class FortranLineReader:
	def __init__(self,format,translation = {"X":None,"I":int,"A":str,"F":float,"E":float}):
		elements=re.compile("(\d*["+"".join(translation.keys())+"]\d*)").findall(format)
		formats=[f.groups() for f in map(re.compile("(\d*)(.)(\d*)").match,elements)]
		self.format=[]
		cur=0
		for repeat,code,nb in formats:
			if repeat!="": repeat=int(repeat)
			else: repeat=1
			if nb!="": nb=int(nb)
			else: nb=1
			for null in range(repeat):
				if translation[code]!=None: self.format.append((translation[code],cur,cur+nb))
				cur+=nb

	def __call__(self,line):
		return [ f(line[debut:fin]) for f,debut,fin in self.format ]

# Example
if __name__ == '__main__':
	# A set of lines, as read from a file
	data = ["12.525.5 A21","2.259.50 C33","154.54.0 Z18"]

	# Format specification:
	# 2 float on 4 places, a blank, a char, an int on 2 places
	format = "2F4,X,A,I2"
	# Creation of the Fortran line reader object
	reader = FortranLineReader(format)

	# Apply the reader to each line
	for l in data:
		print reader(l)

	# Output:
	#[ 12.5, 25.5, 'A', 21]
	#[ 2.25,  9.5, 'C', 33]
	#[154.0, 54.0, 'Z', 18]
