33
33
import textwrap
34
34
import time
35
35
import traceback
36
+ from enum import Enum
36
37
from itertools import takewhile
38
+ from pathlib import Path
37
39
from types import ModuleType
38
- from enum import Enum
39
40
40
41
from pygments .token import Token
41
42
from pygments .lexers import Python3Lexer
@@ -435,11 +436,10 @@ def __init__(self, interp, config):
435
436
self .closed = False
436
437
self .clipboard = get_clipboard ()
437
438
438
- pythonhist = os .path .expanduser (self .config .hist_file )
439
- if os .path .exists (pythonhist ):
439
+ if self .config .hist_file .exists ():
440
440
try :
441
441
self .rl_history .load (
442
- pythonhist , getpreferredencoding () or "ascii"
442
+ self . config . hist_file , getpreferredencoding () or "ascii"
443
443
)
444
444
except OSError :
445
445
pass
@@ -829,13 +829,13 @@ def write2file(self):
829
829
self .interact .notify (_ ("Save cancelled." ))
830
830
return
831
831
832
- if fn . startswith ( "~" ):
833
- fn = os . path . expanduser ( fn )
834
- if not fn .endswith (".py" ) and self . config . save_append_py :
835
- fn = fn + " .py"
832
+ fn = Path ( fn ). expanduser ()
833
+ if fn . suffix != ".py" and self . config . save_append_py :
834
+ # fn.with_suffix (".py") does not append if fn has a non-empty suffix
835
+ fn = Path ( f" { fn } .py")
836
836
837
837
mode = "w"
838
- if os . path . exists (fn ):
838
+ if fn . exists ():
839
839
mode = self .interact .file_prompt (
840
840
_ (
841
841
"%s already exists. Do you "
@@ -941,10 +941,9 @@ def push(self, s, insert_into_history=True):
941
941
return more
942
942
943
943
def insert_into_history (self , s ):
944
- pythonhist = os .path .expanduser (self .config .hist_file )
945
944
try :
946
945
self .rl_history .append_reload_and_write (
947
- s , pythonhist , getpreferredencoding ()
946
+ s , self . config . hist_file , getpreferredencoding ()
948
947
)
949
948
except RuntimeError as e :
950
949
self .interact .notify (f"{ e } " )
@@ -1147,7 +1146,7 @@ def open_in_external_editor(self, filename):
1147
1146
return subprocess .call (args ) == 0
1148
1147
1149
1148
def edit_config (self ):
1150
- if not os . path . isfile ( self .config .config_path ):
1149
+ if not self .config .config_path . is_file ( ):
1151
1150
if self .interact .confirm (
1152
1151
_ (
1153
1152
"Config file does not exist - create "
@@ -1160,17 +1159,15 @@ def edit_config(self):
1160
1159
)
1161
1160
# Py3 files need unicode
1162
1161
default_config = default_config .decode ("ascii" )
1163
- containing_dir = os .path .dirname (
1164
- os .path .abspath (self .config .config_path )
1165
- )
1166
- if not os .path .exists (containing_dir ):
1167
- os .makedirs (containing_dir )
1162
+ containing_dir = self .config .config_path .parent
1163
+ if not containing_dir .exists ():
1164
+ containing_dir .mkdir (parents = True )
1168
1165
with open (self .config .config_path , "w" ) as f :
1169
1166
f .write (default_config )
1170
1167
except OSError as e :
1171
1168
self .interact .notify (
1172
1169
_ ("Error writing file '%s': %s" )
1173
- % (self .config .config . path , e )
1170
+ % (self .config .config_path , e )
1174
1171
)
1175
1172
return False
1176
1173
else :
0 commit comments