MCPcopy Create free account
hub / github.com/ipython/ipython / start

Method start

IPython/core/historyapp.py:54–118  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

52 ))
53
54 def start(self):
55 profile_dir = self.profile_dir.location
56 hist_file = os.path.join(profile_dir, 'history.sqlite')
57 con = sqlite3.connect(hist_file)
58
59 # Grab the recent history from the current database.
60 inputs = list(con.execute('SELECT session, line, source, source_raw FROM '
61 'history ORDER BY session DESC, line DESC LIMIT ?', (self.keep+1,)))
62 if len(inputs) <= self.keep:
63 print("There are already at most %d entries in the history database." % self.keep)
64 print("Not doing anything. Use --keep= argument to keep fewer entries")
65 return
66
67 print("Trimming history to the most recent %d entries." % self.keep)
68
69 inputs.pop() # Remove the extra element we got to check the length.
70 inputs.reverse()
71 if inputs:
72 first_session = inputs[0][0]
73 outputs = list(con.execute('SELECT session, line, output FROM '
74 'output_history WHERE session >= ?', (first_session,)))
75 sessions = list(con.execute('SELECT session, start, end, num_cmds, remark FROM '
76 'sessions WHERE session >= ?', (first_session,)))
77 con.close()
78
79 # Create the new history database.
80 new_hist_file = os.path.join(profile_dir, 'history.sqlite.new')
81 i = 0
82 while os.path.exists(new_hist_file):
83 # Make sure we don't interfere with an existing file.
84 i += 1
85 new_hist_file = os.path.join(profile_dir, 'history.sqlite.new'+str(i))
86 new_db = sqlite3.connect(new_hist_file)
87 new_db.execute("""CREATE TABLE IF NOT EXISTS sessions (session integer
88 primary key autoincrement, start timestamp,
89 end timestamp, num_cmds integer, remark text)""")
90 new_db.execute("""CREATE TABLE IF NOT EXISTS history
91 (session integer, line integer, source text, source_raw text,
92 PRIMARY KEY (session, line))""")
93 new_db.execute("""CREATE TABLE IF NOT EXISTS output_history
94 (session integer, line integer, output text,
95 PRIMARY KEY (session, line))""")
96 new_db.commit()
97
98
99 if inputs:
100 with new_db:
101 # Add the recent history into the new database.
102 new_db.executemany('insert into sessions values (?,?,?,?,?)', sessions)
103 new_db.executemany('insert into history values (?,?,?,?)', inputs)
104 new_db.executemany('insert into output_history values (?,?,?)', outputs)
105 new_db.close()
106
107 if self.backup:
108 i = 1
109 backup_hist_file = os.path.join(profile_dir, 'history.sqlite.old.%d' % i)
110 while os.path.exists(backup_hist_file):
111 i += 1

Callers

nothing calls this directly

Calls 5

executeMethod · 0.80
popMethod · 0.80
commitMethod · 0.80
closeMethod · 0.45
removeMethod · 0.45

Tested by

no test coverage detected