# ================================================================================================
# ONE-SHOT OBJECTS
# Follows is a set of objects for interpreting configuration keywords. They fall into the following
# categories:
# 	class KeyWithValue	eg, PROJECTID
#	class KeyBoolean	eg, USE_XTC
# 	class KeyWithFile	eg, GRO_NAME_DEFAULT
#	class MultiGroFiles	list of dictionaries with keys 'gro', 'top', etc.

# special treatment: MULTI_GRO, TPR_SCHEME

BooleanKeywords = (
"ACCEPT_COMPRESS",
"ASSIGN_COMPRESS",
"DELETE_OLD_TPR",
"DELETE_OLD_TRR",
"MULTI_GRO",
"USE_XTC"
)

FileKeywords = (
"GRO_NAME_DEFAULT",
"MDP_NAME_DEFAULT",
"TOP_NAME_DEFAULT",
"XVG_NAME"
)

ValueKeywords = (
"CONTACT",
"DBSERVER",
"DEADLINE_TIME",
"DESCRIPTION",
"GET_LOG",
"NUM_CLONES",
"NUM_DUMPS",
"NUM_RUNS",
"PROJECT_TYPE",
"PROJECTID",
"RETRY_MAX",
"STATSCREDIT",
"TEMPERATURE" , 
"TIMEOUT_OPR",
"TPR_SCHEME"  # ***
)

class FileList( object ):
	# a dictionary of files
	# keys are 'gro', 'top', 'ndx', 'xvg'
	# values are the path relative to 'path' locating the file

	# a word about paths: we assume files here are relative to 
	# server2/. so the path given should be the server2/ path.

	def __init__( self, listOfFiles, path="" ):

		d = {
			'gro' : '' ,
			'top' : '' ,
			'ndx' : '' ,
			'xvg' : '' ,
			'mdp' : '' 
		    }

		self.path = ( path + "/" ).replace( "//", "/" )

		for file in listOfFiles :
			if path not in file:
				file = self.path + file

			if file[-3:] == "gro" :
				d['gro'] = file
			if file[-3:] == "top" :
				d['top'] = file
			if file[-3:] == "ndx" :
				d['ndx'] = file
			if file[-3:] == "xvg" :
				d['xvg'] = file
			if file[-3:] == "mdp" :
				d['mdp'] = file

		self.files = d 	 

class KeyBoolean( object ):

	def __init__( self, confLine ):
		self.keyword = confLine.split()[0]
		self.value = True

		assert self.value

		return

class KeyWithFile( object ):
	
	def __init__ ( self, confLine, path="" ):
		parts = confLine.split()
		self.keyword = parts[0]

		rest = parts[ 1: ]

		# if more than one, it's a FileList dictionary; if one, 
		# just return the string with the file 

		self.value = FileList( parts[1:], path ).files

		if len( rest ) == 1:
			newval = ""
			for key in self.value.keys() :
				if self.value[ key ] :
					self.value = self.value[ key ]
					break

		return

class KeyWithValue( object ):
	
	def __init__( self, confLine ):

		parts = confLine.split()
		self.keyword = parts[0]
		self.value = self.restOfLine( parts[1:] )

		assert self.keyword != ""
		assert self.value != ""

		return

	def restOfLine( self, list ):
		mystr = ""
		for elem in list:
			mystr += elem 
			if elem != list[-1]:
				mystr += " "
		return mystr

class MultiGroFiles( object ):
	def __init__( self, multigro, path = "" ):
		filelist = []

		for line in multigro:
			# We shouldn't get these, but check just in case
			if "MULTI_GRO" not in line : # covers both MULTI_GRO and END_MULTI_GRO
				listOfFiles = line.split()
				filelist.append( FileList( listOfFiles, path ).files )	
			
		self.filelist = list( filelist )
