##// END OF EJS Templates
Use pathlib in historyapp.py
Gal B -
Show More
@@ -5,8 +5,8 b' An application for managing IPython history.'
5 To be invoked as the `ipython history` subcommand.
5 To be invoked as the `ipython history` subcommand.
6 """
6 """
7
7
8 import os
9 import sqlite3
8 import sqlite3
9 from pathlib import Path
10
10
11 from traitlets.config.application import Application
11 from traitlets.config.application import Application
12 from .application import BaseIPythonApplication
12 from .application import BaseIPythonApplication
@@ -52,8 +52,8 b' class HistoryTrim(BaseIPythonApplication):'
52 ))
52 ))
53
53
54 def start(self):
54 def start(self):
55 profile_dir = self.profile_dir.location
55 profile_dir = Path(self.profile_dir.location)
56 hist_file = os.path.join(profile_dir, 'history.sqlite')
56 hist_file = profile_dir / "history.sqlite"
57 con = sqlite3.connect(hist_file)
57 con = sqlite3.connect(hist_file)
58
58
59 # Grab the recent history from the current database.
59 # Grab the recent history from the current database.
@@ -77,12 +77,12 b' class HistoryTrim(BaseIPythonApplication):'
77 con.close()
77 con.close()
78
78
79 # Create the new history database.
79 # Create the new history database.
80 new_hist_file = os.path.join(profile_dir, 'history.sqlite.new')
80 new_hist_file = profile_dir / "history.sqlite.new"
81 i = 0
81 i = 0
82 while os.path.exists(new_hist_file):
82 while new_hist_file.exists():
83 # Make sure we don't interfere with an existing file.
83 # Make sure we don't interfere with an existing file.
84 i += 1
84 i += 1
85 new_hist_file = os.path.join(profile_dir, 'history.sqlite.new'+str(i))
85 new_hist_file = profile_dir / ("history.sqlite.new" + str(i))
86 new_db = sqlite3.connect(new_hist_file)
86 new_db = sqlite3.connect(new_hist_file)
87 new_db.execute("""CREATE TABLE IF NOT EXISTS sessions (session integer
87 new_db.execute("""CREATE TABLE IF NOT EXISTS sessions (session integer
88 primary key autoincrement, start timestamp,
88 primary key autoincrement, start timestamp,
@@ -106,16 +106,16 b' class HistoryTrim(BaseIPythonApplication):'
106
106
107 if self.backup:
107 if self.backup:
108 i = 1
108 i = 1
109 backup_hist_file = os.path.join(profile_dir, 'history.sqlite.old.%d' % i)
109 backup_hist_file = profile_dir / ("history.sqlite.old.%d" % i)
110 while os.path.exists(backup_hist_file):
110 while backup_hist_file.exists():
111 i += 1
111 i += 1
112 backup_hist_file = os.path.join(profile_dir, 'history.sqlite.old.%d' % i)
112 backup_hist_file = profile_dir / ("history.sqlite.old.%d" % i)
113 os.rename(hist_file, backup_hist_file)
113 hist_file.rename(backup_hist_file)
114 print("Backed up longer history file to", backup_hist_file)
114 print("Backed up longer history file to", backup_hist_file)
115 else:
115 else:
116 os.remove(hist_file)
116 hist_file.unlink()
117
117
118 os.rename(new_hist_file, hist_file)
118 new_hist_file.rename(hist_file)
119
119
120 class HistoryClear(HistoryTrim):
120 class HistoryClear(HistoryTrim):
121 description = clear_hist_help
121 description = clear_hist_help
General Comments 0
You need to be logged in to leave comments. Login now