Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use pathlib in historyapp.py #13278

Merged
merged 1 commit into from
Nov 15, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 12 additions & 12 deletions IPython/core/historyapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
To be invoked as the `ipython history` subcommand.
"""

import os
import sqlite3
from pathlib import Path

from traitlets.config.application import Application
from .application import BaseIPythonApplication
Expand Down Expand Up @@ -52,8 +52,8 @@ class HistoryTrim(BaseIPythonApplication):
))

def start(self):
profile_dir = self.profile_dir.location
hist_file = os.path.join(profile_dir, 'history.sqlite')
profile_dir = Path(self.profile_dir.location)
hist_file = profile_dir / "history.sqlite"
con = sqlite3.connect(hist_file)

# Grab the recent history from the current database.
Expand All @@ -77,12 +77,12 @@ def start(self):
con.close()

# Create the new history database.
new_hist_file = os.path.join(profile_dir, 'history.sqlite.new')
new_hist_file = profile_dir / "history.sqlite.new"
i = 0
while os.path.exists(new_hist_file):
while new_hist_file.exists():
# Make sure we don't interfere with an existing file.
i += 1
new_hist_file = os.path.join(profile_dir, 'history.sqlite.new'+str(i))
new_hist_file = profile_dir / ("history.sqlite.new" + str(i))
new_db = sqlite3.connect(new_hist_file)
new_db.execute("""CREATE TABLE IF NOT EXISTS sessions (session integer
primary key autoincrement, start timestamp,
Expand All @@ -106,16 +106,16 @@ def start(self):

if self.backup:
i = 1
backup_hist_file = os.path.join(profile_dir, 'history.sqlite.old.%d' % i)
while os.path.exists(backup_hist_file):
backup_hist_file = profile_dir / ("history.sqlite.old.%d" % i)
while backup_hist_file.exists():
i += 1
backup_hist_file = os.path.join(profile_dir, 'history.sqlite.old.%d' % i)
os.rename(hist_file, backup_hist_file)
backup_hist_file = profile_dir / ("history.sqlite.old.%d" % i)
hist_file.rename(backup_hist_file)
print("Backed up longer history file to", backup_hist_file)
else:
os.remove(hist_file)
hist_file.unlink()

os.rename(new_hist_file, hist_file)
new_hist_file.rename(hist_file)

class HistoryClear(HistoryTrim):
description = clear_hist_help
Expand Down