config_base cant depend on utils.filenames

This commit is contained in:
Kovid Goyal 2022-03-09 08:20:30 +05:30
parent c1a06e9c37
commit 29482963b7
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -344,33 +344,36 @@ class ConfigInterface:
self.option_set.smart_update(opts1, opts2) self.option_set.smart_update(opts1, opts2)
def read_data(file_path, count=10, sleep_time=0.2): def retry_on_fail(func, *args, count=10, sleep_time=0.2):
import time import time
count = 10
for i in range(count): for i in range(count):
try: try:
with open(file_path, 'rb') as f: return func(*args)
return f.read()
except FileNotFoundError: except FileNotFoundError:
raise raise
except OSError: except OSError:
if i > count - 2: if not iswindows or i > count - 2:
raise raise
# Try the operation repeatedly in case something like a virus # Try the operation repeatedly in case something like a virus
# scanner has opened one of the files (I love windows) # scanner has opened one of the files (I love windows)
time.sleep(sleep_time) time.sleep(sleep_time)
def read_data(file_path):
def r():
with open(file_path, 'rb') as f:
return f.read()
return retry_on_fail(r)
def commit_data(file_path, data): def commit_data(file_path, data):
import tempfile import tempfile
from calibre.utils.filenames import atomic_rename
bdir = os.path.dirname(file_path) bdir = os.path.dirname(file_path)
os.makedirs(bdir, exist_ok=True, mode=CONFIG_DIR_MODE) os.makedirs(bdir, exist_ok=True, mode=CONFIG_DIR_MODE)
try: try:
with tempfile.NamedTemporaryFile(dir=bdir, delete=False) as f: with tempfile.NamedTemporaryFile(dir=bdir, delete=False) as f:
f.write(data) f.write(data)
atomic_rename(f.name, file_path) retry_on_fail(os.replace, f.name, file_path)
finally: finally:
with suppress(FileNotFoundError, NameError): with suppress(FileNotFoundError, NameError):
os.remove(f.name) os.remove(f.name)