mirror of
https://github.com/Threnklyn/esphome-dev.git
synced 2026-05-30 01:38:27 +02: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:
+12
-12
@@ -1,5 +1,6 @@
|
||||
import functools
|
||||
import sys
|
||||
import codecs
|
||||
|
||||
PYTHON_MAJOR = sys.version_info[0]
|
||||
IS_PY2 = PYTHON_MAJOR == 2
|
||||
@@ -75,15 +76,14 @@ def indexbytes(buf, i):
|
||||
return ord(buf[i])
|
||||
|
||||
|
||||
if IS_PY2:
|
||||
def decode_text(data, encoding='utf-8', errors='strict'):
|
||||
# type: (str, str, str) -> unicode
|
||||
if isinstance(data, unicode):
|
||||
return data
|
||||
return unicode(data, encoding=encoding, errors=errors)
|
||||
else:
|
||||
def decode_text(data, encoding='utf-8', errors='strict'):
|
||||
# type: (bytes, str, str) -> str
|
||||
if isinstance(data, str):
|
||||
return data
|
||||
return data.decode(encoding=encoding, errors=errors)
|
||||
def decode_text(data, encoding='utf-8', errors='strict'):
|
||||
if isinstance(data, text_type):
|
||||
return data
|
||||
return codecs.decode(data, encoding, errors)
|
||||
|
||||
|
||||
def encode_text(data, encoding='utf-8', errors='strict'):
|
||||
if isinstance(data, binary_type):
|
||||
return data
|
||||
|
||||
return codecs.encode(data, encoding, errors)
|
||||
|
||||
Reference in New Issue
Block a user