Commit 4259852e authored by Laurent Bachelier's avatar Laurent Bachelier 🐧 Committed by ntome

config: Cross-platform file replace

parent 8347aa24
......@@ -30,6 +30,7 @@ from weboob.tools.compat import unicode
from .extra import time_buffer
from .iconfig import ConfigError, IConfig
from .util import replace
from .yamlconfig import WeboobDumper
try:
......@@ -146,7 +147,7 @@ class SQLiteConfig(IConfig):
for line in self.storage.iterdump():
f.write(unicode(line).encode('utf-8'))
f.write(b'\n')
os.rename(f.name, target)
replace(f.name, target)
def ensure_table(self, name):
if name not in self._tables:
......
# -*- coding: utf-8 -*-
# Copyright(C) 2019 Laurent Bachelier
#
# This file is part of weboob.
#
# weboob is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# weboob is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with weboob. If not, see <http://www.gnu.org/licenses/>.
import os
__all__ = ['replace']
try:
from os import replace
except ImportError:
def replace(src, dst, *args, **kwargs):
if os.name != 'posix':
try:
os.remove(dst)
except OSError:
pass
return os.rename(src, dst, *args, **kwargs)
......@@ -27,6 +27,7 @@ import weboob.tools.date
import yaml
from .iconfig import ConfigError, IConfig
from .util import replace
try:
from yaml import CLoader as Loader
......@@ -86,7 +87,7 @@ class YamlConfig(IConfig):
f = tempfile.NamedTemporaryFile(mode='w', dir=os.path.dirname(self.path), delete=False, encoding='utf-8')
with f:
yaml.dump(self.values, f, Dumper=self.DUMPER, default_flow_style=False)
os.rename(f.name, self.path)
replace(f.name, self.path)
def get(self, *args, **kwargs):
v = self.values
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment