# should this be combined with FAHGeneration somehow?
# or does it really server a distinct purpose? Where is it used?
from FAHBaseType import FAHBaseType
class FAHJob( FAHBaseType ):

	def __init__(self, prcg, datapath=""):
		self.prcg = prcg
		self.prcgd = {'proj': prcg[0], 'run': prcg[1], 'clone': prcg[2], 'gen': prcg[3]}
		self.name = "PROJ%d RUN%d CLONE%d GEN%d" % self.prcg

	def __getitem__(self, key):
		try:
			return self.prcg[key]
		except TypeError:
			return self.prcgd[key]
	
	def __str__(self):
		return self.name

	def __repr__( self):
		return "Job %s" % self.name

class FAHReturnedWU( FAHBaseType ):
	def __init__( self, returnline ):
		self.returnline = returnline.strip()
		self.fields=self.returnline.split()

		try:
			self.donor=self.fields[1]
			self.team=self.fields[2]
			self.credit=self.fields[4]; self.points=float( self.credit )
			self.ip=self.fields[6]
			self.prgcstring =self.fields[7]
		
			self.wustringlist=self.prgcstring.replace("(","").replace(")","").split(",")
			self.project = int( self.wustringlist[0] )
			self.run = int( self.wustringlist[1] )
			self.clone = int( self.wustringlist[3] )
			self.gen = int( self.wustringlist[2] )
			self.prcg = ( self.project, self.run, self.clone, self.gen )
			self.prcgstring = "(%d,%d,%d,%d)" % ( self.project, self.run, self.clone, self.gen )

		except IndexError:
			raise self.BadReturnLine

		except ValueError:
			raise self.BadReturnLine

		self.name = "%s %s %s %s %s" % ( self.donor, self.team, self.credit, self.ip, self.prgcstring )

	def __str__( self ):
		return self.name

	def recreditForm( self, points ):
		form = "%s %s %s %s" % ( self.donor, self.team, self.prcgstring, points )
		return form
		
	class BadReturnLine( Exception ): 
		pass 
