# coding: utf-8
import pickle 

DATA_FILES = {'b':'static/spc/spc_b.txt', 'c':'static/spc/spc_c.txt',
				'd':'static/spc/spc_d.txt'}

def new_batch(batch, bcd):
	with open(DATA_FILES[bcd], 'wb') as batchinfo:
		try:
			pickle.dump(batch, batchinfo)
			return True
		except EOFError: 
			return False
	

def add_record(item, bcd):
	"""
	add a item into pricelist
	"""
	with open(DATA_FILES[bcd], 'rb') as pricelist:
		try:
			pklist = pickle.load(pricelist)
		except EOFError:
			pklist = []		
		pklist.append(item)	
	with open(DATA_FILES[bcd], 'wb') as pricelist:
		pickle.dump(pklist, pricelist)

def load_records(bcd):
	"""
	output all pricelist in the database
	"""
	with open(DATA_FILES[bcd], 'rb') as pricelist:
		try:
			pklist = pickle.load(pricelist)
		except EOFError: 
			pass
	return pklist

def modify_item(row, item, bcd):
	"""
	the function modify a certain row of the pricelist, 
	the input item is the correct value in that row
	"""
	with open(DATA_FILES[bcd], 'rb') as pricelist:
		try:
			pklist = pickle.load(pricelist)
		except EOFError:
			pklist = []		
		pklist[row] = item	
	with open(DATA_FILES[bcd], 'wb') as pricelist:
		pickle.dump(pklist, pricelist)	

def remove_item(row, bcd):
	with open(DATA_FILES[bcd], 'rb') as pricelist:
		try:
			pklist = pickle.load(pricelist)
		except EOFError:
			pklist = []		
		pklist.pop(row)	
	with open(DATA_FILES[bcd], 'wb') as pricelist:
		pickle.dump(pklist, pricelist)
