#coding=utf-8
import yaml
import sys
import os
base_path = os.getcwd()
sys.path.append(base_path)
file_path = base_path + '/Config/userconfig.yalm'
class WriteUserCommand:
	def read_data(self):
		'''
		加载yaml数据
		'''
		# file_path = base_path + '/Config/userconfig.yaml'
		with open(file_path) as fr:
			data = yaml.load(fr)
			yaml.warnings({'YAMLLoadWarning': False})
		return data

	def get_value(self,key,port):
		'''
		获取value
		'''
		data = self.read_data()
		value = data[key][port]
		return value

	def write_data(self,i,device,bp,port):
		'''
		写入数据
		'''
		data = self.join_data(i,device,bp,port)
		with open(file_path,"a") as fr:
			yaml.dump(data,fr)

	def join_data(self,i,device,bp,port):
		data = {
		"user_info_"+str(i):{
		"deviceName":device,
		"bp":bp,
		"port":port
		}
		}
		return data

	def clear_data(self):
		with open(file_path,"w") as fr:
			fr.truncate()
		fr.close()

	def get_file_lines(self):
		data = self.read_data()
		return len(data)


if __name__ == '__main__':
	write_file = WriteUserCommand()
	print(write_file.read_data())
	print(write_file.get_value("config",'name'))

	# print(write_file.get_value('config','name'))
