def import_folder(epath, mpoint, secret,  automount, autodelete):
	if _get_items(epath=epath):
		return True
	configuration = Configuration()
	configuration.create_folder(epath,mpoint,automount,autodelete)
	configuration.save()
	cfipm = CFIPasswordManager()
	ans = cfipm.createEncriptedFolder(epath,secret)
	return True
def add_folder(epath, mpoint, secret, automount, autodelete):
	print(is_folder_encrypted(epath))
	if is_folder_encrypted(epath):
		raise AlreadyEncrypted()
		return False
	"""Add new EncFS item to keyring."""
	print(_get_items(epath=epath))
	if _get_items(epath=epath):
		return True
	configuration = Configuration()
	configuration.create_folder(epath,mpoint,automount,autodelete)
	configuration.save()
	cfipm = CFIPasswordManager()
	cfipm.createEncriptedFolder(epath,secret)
	#
	return create_crypted_folder(epath, mpoint, secret)
def edit_folder(epath, new_mpoint = None, new_secret = None, new_automount = False, new_autodelete = False):	
	print('Edit folders')
	folder = _get_items(epath=epath)
	if folder != None:
		mpoint = folder["mount-point"]
		automount = (folder["auto-mount"] == 'yes')
		autodelete = (folder['auto-delete'] == 'yes')
		secret = get_secret(epath)	
		if new_mpoint == None and new_secret == None and new_automount == automount and new_autodelete == autodelete:
			return True
		if new_secret != None and secret != new_secret:
			p1 = subprocess.Popen([ECHO, "-e", secret + "\n" + new_secret + "\n"],\
				stdout=subprocess.PIPE)
			p2 = subprocess.Popen([ENCFSCTL, "autopasswd", epath],\
				stdin=p1.stdout, stdout=subprocess.PIPE)
			p2.communicate()[0]
			if p2.poll() is not 0:
				raise BadPassword()
			cfipm = CFIPasswordManager()
			ans = cfipm.createEncriptedFolder(epath,new_secret)
			
		if new_mpoint != None and new_mpoint != mpoint:
			if is_folder_mounted(epath) == True:
				if unmount_folder(epath) == False:
					raise NotUnmount()
			try:
				delete_it(mpoint)
			except:
				pass
			delete_it(new_mpoint)
			if not os.path.exists(new_mpoint):
				os.mkdir(new_mpoint)
			mpoint = new_mpoint
		# update item data
		configuration = Configuration()
		configuration.set_folder_mount_point(epath,mpoint)
		if new_automount == True:
			configuration.set_folder_auto_mount(epath,'yes')
		else:
			configuration.set_folder_auto_mount(epath,'no')
		if new_autodelete == True:
			configuration.set_folder_auto_delete(epath,'yes')
		else:
			configuration.set_folder_auto_delete(epath,'no')
		configuration.save()
	return True
def remove_folder(epath):
	"""Remove EncFS item from keyring."""
	if is_folder_mounted(epath) == True:
		print('Is folder mounted')
		if unmount_folder(epath) == False:
			raise NotUnmount()
	'''
	if delete_it(epath) == False:
		raise NotUnmount()
	'''
	folder = _get_items(epath)
	if folder:
		mpoint = folder['mount-point']
		if delete_it(mpoint) == False:
			raise NotUnmount()
	configuration = Configuration()
	configuration.delete_folder(epath)
	configuration.save()
	cfipm = CFIPasswordManager()
	cfipm.removeEncriptedFolder(epath)
	return True
def get_secret(epath):
	cfipm = CFIPasswordManager()
	ans = cfipm.find_folder(epath)
	if ans:
		return ans[1]
	return NoEncrypted()