1
2
3
4 """
5 Handling of command history and their saving/loading in/from files.
6
7 The history is a list of commands (one item by command) to be
8 compatible with the history represenation in wx.Py.shell.
9 """
10
11 import os
12 import time
13
14 -class HistoryManager(object):
15 """
16 Manage the history of commands.
17 """
18
19
20
21
22
23 COMMANDS_SEPARATOR = '\x00\n'
24
25
26 - def __init__(self, shell, history_dir, extension="session", max_file_nb=10):
27 """
28 Initialise the manager.
29
30 ``history_dir``
31
32 the directory where history files are supposed to be saved.
33 """
34 self.shell = shell
35 self.history_dir = history_dir
36 self.extension = extension
37 self.max_file_nb = max_file_nb
38 self.put_landmark()
39
41 """
42 Return the list of dates corresponding to the available history files.
43 """
44 histo_list = []
45 for root, dirs, files in os.walk(self.history_dir):
46 for filename in files:
47 if filename.endswith(".%s"%self.extension):
48
49
50
51 histo_list.append(filename.rstrip(".%s"%self.extension))
52
53 return histo_list
54
55 - def load_session_at_date(self, dateinfo):
56 """
57 Iterate over the list of command corresponding to the session at a
58 given date, described by a string in the following format:
59
60 YYYYMMDD-hhmmss
61 """
62 f = open(os.path.join(self.history_dir,"%s.%s"%(dateinfo,self.extension)))
63 hist = f.read()
64 commands = hist.split(self.COMMANDS_SEPARATOR)
65
66 for c in commands:
67 self.shell.history.append(c)
68
70 """
71 Load the latest session.
72 """
73 session_dates = self.get_history_files_info()
74
75 session_dates.sort()
76 if len(session_dates)>0:
77
78 self.load_session_at_date(session_dates[-1])
79
81 """
82 Check that there is not too many session file, and if there is
83 remove the oldest.
84 """
85 session_files = self.get_history_files_info()
86 session_files.sort()
87 while len(session_files)>self.max_file_nb:
88 f = session_files.pop(0)
89 os.remove(os.path.join(self.history_dir,"%s.%s"%(f,self.extension)))
90
92 """
93 Save the history of a given file into a shell.
94
95 Save the session only after the landmark.
96 """
97 lt = time.localtime()
98
99 date_stamp = "%s%#02d%#02d-%#02d%#02d%#02d" % (lt.tm_year,lt.tm_mon,lt.tm_mday,
100 lt.tm_hour,lt.tm_min,lt.tm_sec)
101
102 filename = os.path.join(self.history_dir,"%s.%s"%(date_stamp,self.extension))
103 f = open(filename,"w")
104 f.write(self.COMMANDS_SEPARATOR.join(self.get_history_from_landmark()))
105 f.close()
106
107 self.check_not_too_many_file()
108
109 - def put_landmark(self):
110 """
111 Record the current length of the shell's history.
112 """
113 self.landmark=len(self.shell.history)
114
115 - def get_landmark(self):
116 """
117 Record the length of the shell's history when the latest landmark was put.
118 """
119 return self.landmark
120
122 """
123 Return the list of commands entered since the latest landmark.
124 """
125
126
127 if self.landmark==0:
128 return self.shell.history[:]
129 else:
130 return self.shell.history[:-self.landmark]
131