Revision fd5db045 kamaki/cli/history.py

b/kamaki/cli/history.py
35 35

  
36 36
from os.path import exists
37 37

  
38

  
38 39
def order_free_contains(containing, contained):
39
	superset = containing.split()
40
	for term in contained.split():
41
		if term not in superset:
42
			return False
43
	return True
40
    superset = containing.split()
41
    for term in contained.split():
42
        if term not in superset:
43
            return False
44
    return True
45

  
44 46

  
45 47
class History(object):
46
	def __init__(self, filepath):
47
		self.filepath=filepath
48
    def __init__(self, filepath):
49
        self.filepath = filepath
48 50

  
49
	@classmethod
50
	def _match(self,line, match_terms):
51
		if match_terms is None:
52
			return True
53
		for term in match_terms.split():
54
			if term not in line:
55
				return False
56
		return True
51
    @classmethod
52
    def _match(self, line, match_terms):
53
        if match_terms is None:
54
            return True
55
        for term in match_terms.split():
56
            if term not in line:
57
                return False
58
        return True
57 59

  
58
	def get(self, match_terms=None, limit=0):
59
		f = open(self.filepath, 'r')
60
		result = ['%s.  \t%s'%(index+1,line) \
61
			for index,line in enumerate(f.readlines()) \
62
			if self._match(line, match_terms)]
63
		offset = len(result)-limit if limit and len(result) > limit else 0
64
		return result[offset:]
60
    def get(self, match_terms=None, limit=0):
61
        f = open(self.filepath, 'r')
62
        result = ['%s.  \t%s' % (index + 1, line)\
63
            for index, line in enumerate(f.readlines())\
64
            if self._match(line, match_terms)]
65
        offset = len(result) - limit if limit and len(result) > limit else 0
66
        return result[offset:]
65 67

  
66
	def add(self, line):
67
		f = open(self.filepath, 'a+')
68
		f.write(line+'\n')
69
		f.close()
68
    def add(self, line):
69
        f = open(self.filepath, 'a+')
70
        f.write(line + '\n')
71
        f.close()
70 72

  
71
	def clean(self):
72
		f = open(self.filepath, 'w')
73
		f.close()
73
    def clean(self):
74
        f = open(self.filepath, 'w')
75
        f.close()

Also available in: Unified diff