utils.py
def prioritizeMedia(acceptVal,extension,path):
	"""
	takes in accept-media header value(str) and returns 
	which media type to use according to q priority
	"""
	if(acceptVal == ""):
		return "application/example"
	with open('media-types/content-qvals.json','r') as jf:
		priority = json.load(jf)
	tmp = acceptVal.split(',')
	for i in tmp:
		i = i.strip()
		pair = i.split(';')
		broadtype = pair[0].strip()
		mtype = broadtype.split("/")[0]
		subtype = broadtype.split("/")[1]
		if len(pair) == 1:
			pair.append("q=1.0")
		try:
			q = float(pair[1].split("=")[1].strip())
		except ValueError:
			q = float(pair[2].split("=")[1].strip())
		tag = pair[1].split("=")[0].strip()
		if broadtype == "*/*" :
			q -= 0.000001
		if tag == "level":
			q += 0.01
		if(subtype != "*"):
			if mtype + "/" + subtype not in priority.keys():
				q = 0
				
			priority[mtype + "/" + subtype] = q
		else:
			for key in priority.keys():
				if key.split("/")[0] == mtype:
					if key not in priority.keys():
						q = 0.00000001
					priority[key] = q - 0.00000001
	sortedpriority = sorted(priority.items(), key=lambda x: x[1],reverse = True)
	for key in sortedpriority:
		if key[1] != 0 and (os.path.isfile(path.rstrip(pathlib.Path(path).suffix) + "." + key[0].split("/")[1]) or key[0].split("/")[1] == "*"):
			return key[0].split("/")[1]
	return -1

	takes in accept-media header value(str) and returns 
	which media type to use according to q priority
	
takes in accept-media header value(str) and returns
which media type to use according to q priority
takes in accept - media header value ( str ) and returns which media type to use according to q priority
def prioritizeMedia(acceptVal,extension,path):
	if(acceptVal == ""):
		return "application/example"
	with open('media-types/content-qvals.json','r') as jf:
		priority = json.load(jf)
	tmp = acceptVal.split(',')
	for i in tmp:
		i = i.strip()
		pair = i.split(';')
		broadtype = pair[0].strip()
		mtype = broadtype.split("/")[0]
		subtype = broadtype.split("/")[1]
		if len(pair) == 1:
			pair.append("q=1.0")
		try:
			q = float(pair[1].split("=")[1].strip())
		except ValueError:
			q = float(pair[2].split("=")[1].strip())
		tag = pair[1].split("=")[0].strip()
		if broadtype == "*/*" :
			q -= 0.000001
		if tag == "level":
			q += 0.01
		if(subtype != "*"):
			if mtype + "/" + subtype not in priority.keys():
				q = 0
			priority[mtype + "/" + subtype] = q
		else:
			for key in priority.keys():
				if key.split("/")[0] == mtype:
					if key not in priority.keys():
						q = 0.00000001
					priority[key] = q - 0.00000001
	sortedpriority = sorted(priority.items(), key=lambda x: x[1],reverse = True)
	for key in sortedpriority:
		if key[1] != 0 and (os.path.isfile(path.rstrip(pathlib.Path(path).suffix) + "." + key[0].split("/")[1]) or key[0].split("/")[1] == "*"):
			return key[0].split("/")[1]
	return -1
def prioritizeMedia ( acceptVal , extension , path ) : if ( acceptVal == "" ) : return "application/example" with open ( 'media-types/content-qvals.json' , 'r' ) as jf : priority = json . load ( jf ) tmp = acceptVal . split ( ',' ) for i in tmp : i = i . strip ( ) pair = i . split ( ';' ) broadtype = pair [ 0 ] . strip ( ) mtype = broadtype . split ( "/" ) [ 0 ] subtype = broadtype . split ( "/" ) [ 1 ] if len ( pair ) == 1 : pair . append ( "q=1.0" ) try : q = float ( pair [ 1 ] . split ( "=" ) [ 1 ] . strip ( ) ) except ValueError : q = float ( pair [ 2 ] . split ( "=" ) [ 1 ] . strip ( ) ) tag = pair [ 1 ] . split ( "=" ) [ 0 ] . strip ( ) if broadtype == "*/*" : q -= 0.000001 if tag == "level" : q += 0.01 if ( subtype != "*" ) : if mtype + "/" + subtype not in priority . keys ( ) : q = 0 priority [ mtype + "/" + subtype ] = q else : for key in priority . keys ( ) : if key . split ( "/" ) [ 0 ] == mtype : if key not in priority . keys ( ) : q = 0.00000001 priority [ key ] = q - 0.00000001 sortedpriority = sorted ( priority . items ( ) , key = lambda x : x [ 1 ] , reverse = True ) for key in sortedpriority : if key [ 1 ] != 0 and ( os . path . isfile ( path . rstrip ( pathlib . Path ( path ) . suffix ) + "." + key [ 0 ] . split ( "/" ) [ 1 ] ) or key [ 0 ] . split ( "/" ) [ 1 ] == "*" ) : return key [ 0 ] . split ( "/" ) [ 1 ] return - 1
takes in accept-media header value(str) and returns
which media type to use according to q priority
takes in accept - media header value ( str ) and returns which media type to use according to q priority
"""
	takes in accept-media header value(str) and returns 
	which media type to use according to q priority
	"""
acceptVal
extension
path