22
22
# THE SOFTWARE.
23
23
24
24
import os
25
+ from pathlib import Path
25
26
import stat
26
27
from itertools import islice , chain
27
- from typing import Iterable , Optional , List , TextIO
28
+ from typing import Iterable , Optional , List , TextIO , Union
28
29
29
30
from .translations import _
30
31
from .filelock import FileLock
@@ -190,9 +191,9 @@ def reset(self) -> None:
190
191
self .index = 0
191
192
self .saved_line = ""
192
193
193
- def load (self , filename : str , encoding : str ) -> None :
194
+ def load (self , filename : Path , encoding : str ) -> None :
194
195
with open (filename , encoding = encoding , errors = "ignore" ) as hfile :
195
- with FileLock (hfile , filename = filename ):
196
+ with FileLock (hfile , filename = str ( filename ) ):
196
197
self .entries = self .load_from (hfile )
197
198
198
199
def load_from (self , fd : TextIO ) -> List [str ]:
@@ -201,14 +202,14 @@ def load_from(self, fd: TextIO) -> List[str]:
201
202
self .append_to (entries , line )
202
203
return entries if len (entries ) else ["" ]
203
204
204
- def save (self , filename : str , encoding : str , lines : int = 0 ) -> None :
205
+ def save (self , filename : Path , encoding : str , lines : int = 0 ) -> None :
205
206
fd = os .open (
206
207
filename ,
207
208
os .O_WRONLY | os .O_CREAT | os .O_TRUNC ,
208
209
stat .S_IRUSR | stat .S_IWUSR ,
209
210
)
210
211
with open (fd , "w" , encoding = encoding , errors = "ignore" ) as hfile :
211
- with FileLock (hfile , filename = filename ):
212
+ with FileLock (hfile , filename = str ( filename ) ):
212
213
self .save_to (hfile , self .entries , lines )
213
214
214
215
def save_to (
@@ -221,7 +222,7 @@ def save_to(
221
222
fd .write ("\n " )
222
223
223
224
def append_reload_and_write (
224
- self , s : str , filename : str , encoding : str
225
+ self , s : str , filename : Path , encoding : str
225
226
) -> None :
226
227
if not self .hist_size :
227
228
return self .append (s )
@@ -233,7 +234,7 @@ def append_reload_and_write(
233
234
stat .S_IRUSR | stat .S_IWUSR ,
234
235
)
235
236
with open (fd , "a+" , encoding = encoding , errors = "ignore" ) as hfile :
236
- with FileLock (hfile , filename = filename ):
237
+ with FileLock (hfile , filename = str ( filename ) ):
237
238
# read entries
238
239
hfile .seek (0 , os .SEEK_SET )
239
240
entries = self .load_from (hfile )
0 commit comments