Ejemplo n.º 1
0
	def __init__(self, filename, ConfigFiles):
		#self.conf = Conf()
		history_max_size = ConfigFiles.readVar("history", "max_size")
		
		self.confDir = ConfDir()
		#self.confDir.checkForAndCreateHistDir(filename)
		conf_dir_path, self.path = self.confDir.checkForAndCreateConfDir(None, filename) 
		
		self.fileCrop(history_max_size)
Ejemplo n.º 2
0
class History(object):
	def __init__(self, filename, ConfigFiles):
		#self.conf = Conf()
		history_max_size = ConfigFiles.readVar("history", "max_size")
		
		self.confDir = ConfDir()
		#self.confDir.checkForAndCreateHistDir(filename)
		conf_dir_path, self.path = self.confDir.checkForAndCreateConfDir(None, filename) 
		
		self.fileCrop(history_max_size)
		
	# number, self.path -> file
	def fileCrop(self, history_max_size):
		# open file, create if not exists
		try:
			history = open(self.path, "r")#r+
		except IOError:
			history = open(self.path, "w+")
		
		history_list = []
		for line in history:
			history_list.append(line)
		
		history.close()
		
		length = len(history_list)
		
		if length > history_max_size:
			print "cropping history"
			
			value = (length - history_max_size) * -1
			history_list = history_list[::-1][:value][::-1]
			
			history = open(self.path, "w")
			
			for line in history_list:
				if not line == "\n":
					history.write(line)
			
			history.close()
	
	def history(self, index):
		return self.history(self, index, None)
	
	# index, self.path -> string
	def history(self, index, string):
		if type(string) == types.NoneType:
			string = None
		elif string == "":
			string = None
			
		#print "string: ", string
		
		history = open(self.path, "r")
		
		history_list = []
		for line in history:
			history_list.append(line)
		
		history_list.append("")
		
		history.close()
		
		try:
			return history_list[::-1][index][:-1]
		except IndexError:
			return ""
		#return history_list[index * -1]
		#####################################################
		if len(history_list) > 0:
			if string != None:
				print "if"
				for item in history_list[::-1]:
					if item.startswith(string):
						return item
			else:
				print "else"
				return history_list[index * -1]
		else:
			return "History is empty"
		
#		try:
#			result = history_list[::-1][index][:-1]
#			return result
#		except IndexError:
#			print "history.py -> indexerror"
#			return ""
		
	# number, self.path -> list, list
	def history_inverted(self, count):
		history = open(self.path, "r")
		
		history_list = []
		for line in history:
			line_utf8 = line.decode("utf8")
			# to delete the "\n":
			history_list.append(line_utf8[:-1])
			
		history.close()
		
		if count:
			return history_list[::-1][:int(count)]
		else:
			return history_list[::-1]
		
	# self.path, string -> file
	def historyWrite(self, text):
		
		if not self.history(1, None) == text:
			history = open(self.path, "a")
			
			text = text.encode("utf8")
			history.write(text)
			history.write("\n")
			history.close()