mirror of
https://github.com/esphome/esphome.git
synced 2025-09-02 19:32:19 +01:00
Make file generation saving atomic (#792)
* Make file generation saving atomic * Lint * Python 2 Compat * Fix * Handle file not found error
This commit is contained in:
@@ -7,7 +7,7 @@ import os
|
||||
|
||||
from esphome import const
|
||||
from esphome.core import CORE
|
||||
from esphome.helpers import mkdir_p
|
||||
from esphome.helpers import mkdir_p, write_file_if_changed
|
||||
|
||||
# pylint: disable=unused-import, wrong-import-order
|
||||
from esphome.core import CoreType # noqa
|
||||
@@ -89,8 +89,7 @@ class StorageJSON(object):
|
||||
|
||||
def save(self, path):
|
||||
mkdir_p(os.path.dirname(path))
|
||||
with codecs.open(path, 'w', encoding='utf-8') as f_handle:
|
||||
f_handle.write(self.to_json())
|
||||
write_file_if_changed(path, self.to_json())
|
||||
|
||||
@staticmethod
|
||||
def from_esphome_core(esph, old): # type: (CoreType, Optional[StorageJSON]) -> StorageJSON
|
||||
@@ -130,8 +129,7 @@ class StorageJSON(object):
|
||||
@staticmethod
|
||||
def _load_impl(path): # type: (str) -> Optional[StorageJSON]
|
||||
with codecs.open(path, 'r', encoding='utf-8') as f_handle:
|
||||
text = f_handle.read()
|
||||
storage = json.loads(text, encoding='utf-8')
|
||||
storage = json.load(f_handle)
|
||||
storage_version = storage['storage_version']
|
||||
name = storage.get('name')
|
||||
comment = storage.get('comment')
|
||||
@@ -195,15 +193,12 @@ class EsphomeStorageJSON(object):
|
||||
return json.dumps(self.as_dict(), indent=2) + u'\n'
|
||||
|
||||
def save(self, path): # type: (str) -> None
|
||||
mkdir_p(os.path.dirname(path))
|
||||
with codecs.open(path, 'w', encoding='utf-8') as f_handle:
|
||||
f_handle.write(self.to_json())
|
||||
write_file_if_changed(path, self.to_json())
|
||||
|
||||
@staticmethod
|
||||
def _load_impl(path): # type: (str) -> Optional[EsphomeStorageJSON]
|
||||
with codecs.open(path, 'r', encoding='utf-8') as f_handle:
|
||||
text = f_handle.read()
|
||||
storage = json.loads(text, encoding='utf-8')
|
||||
storage = json.load(f_handle)
|
||||
storage_version = storage['storage_version']
|
||||
cookie_secret = storage.get('cookie_secret')
|
||||
last_update_check = storage.get('last_update_check')
|
||||
|
Reference in New Issue
Block a user