Package mathbench :: Package basement :: Module history_manager
[hide private]

Source Code for Module mathbench.basement.history_manager

  1  #!/usr/bin/python 
  2  # -*- coding: utf-8 -*- 
  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 # a separator that is a bit more that a line separator to be able 20 # to locate when the line ends because the commands ends or 21 # because we are writting a multiple line commands (eg class 22 # definition) (idea taken from wx.py itself...) 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
40 - def get_history_files_info(self):
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 # then it should be a command history and the rest 49 # of the file should be in the following format: 50 # YYYYMMDD-hhmmss 51 histo_list.append(filename.rstrip(".%s"%self.extension)) 52 # don't go into the hierachy of folders 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 # load all the commands in the shell's history 66 for c in commands: 67 self.shell.history.append(c)
68
69 - def load_latest_session(self):
70 """ 71 Load the latest session. 72 """ 73 session_dates = self.get_history_files_info() 74 # sort all 75 session_dates.sort() 76 if len(session_dates)>0: 77 # take the latest 78 self.load_session_at_date(session_dates[-1])
79
80 - def check_not_too_many_file(self):
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
91 - def save_current_session(self):
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 # create an appropriate date stamp to put in the file name 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 # open the file and write all the commands 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 # check that we have not exceeded the limit of such files 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 # print self.landmark 126 # print self.shell.history 127 if self.landmark==0: 128 return self.shell.history[:] 129 else: 130 return self.shell.history[:-self.landmark]
131