1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-10 07:12:21 +01:00

Drop Python 2 Support (#793)

* Remove Python 2 support

* Remove u-strings

* Remove docker symlinks

* Remove from travis

* Update requirements

* Upgrade flake8/pylint

* Fixes

* Manual

* Run pyupgrade

* Lint

* Remove base_int

* Fix

* Update platformio_api.py

* Update component.cpp
This commit is contained in:
Otto Winter
2019-12-07 18:28:55 +01:00
committed by GitHub
parent b5714cd70f
commit 056c72d50d
78 changed files with 815 additions and 1097 deletions

View File

@@ -1,12 +1,8 @@
from __future__ import print_function
import codecs
import logging
import os
from esphome.py_compat import char_to_byte, text_type, IS_PY2, encode_text
_LOGGER = logging.getLogger(__name__)
@@ -18,24 +14,24 @@ def ensure_unique_string(preferred_string, current_strings):
while test_string in current_strings_set:
tries += 1
test_string = u"{}_{}".format(preferred_string, tries)
test_string = f"{preferred_string}_{tries}"
return test_string
def indent_all_but_first_and_last(text, padding=u' '):
def indent_all_but_first_and_last(text, padding=' '):
lines = text.splitlines(True)
if len(lines) <= 2:
return text
return lines[0] + u''.join(padding + line for line in lines[1:-1]) + lines[-1]
return lines[0] + ''.join(padding + line for line in lines[1:-1]) + lines[-1]
def indent_list(text, padding=u' '):
def indent_list(text, padding=' '):
return [padding + line for line in text.splitlines()]
def indent(text, padding=u' '):
return u'\n'.join(indent_list(text, padding))
def indent(text, padding=' '):
return '\n'.join(indent_list(text, padding))
# From https://stackoverflow.com/a/14945195/8924614
@@ -43,17 +39,16 @@ def cpp_string_escape(string, encoding='utf-8'):
def _should_escape(byte): # type: (int) -> bool
if not 32 <= byte < 127:
return True
if byte in (char_to_byte('\\'), char_to_byte('"')):
if byte in (ord('\\'), ord('"')):
return True
return False
if isinstance(string, text_type):
if isinstance(string, str):
string = string.encode(encoding)
result = ''
for character in string:
character = char_to_byte(character)
if _should_escape(character):
result += '\\%03o' % character
result += f'\\{character:03o}'
else:
result += chr(character)
return '"' + result + '"'
@@ -91,7 +86,7 @@ def mkdir_p(path):
pass
else:
from esphome.core import EsphomeError
raise EsphomeError(u"Error creating directories {}: {}".format(path, err))
raise EsphomeError(f"Error creating directories {path}: {err}")
def is_ip_address(host):
@@ -118,7 +113,7 @@ def _resolve_with_zeroconf(host):
try:
info = zc.resolve_host(host + '.')
except Exception as err:
raise EsphomeError("Error resolving mDNS hostname: {}".format(err))
raise EsphomeError(f"Error resolving mDNS hostname: {err}")
finally:
zc.close()
if info is None:
@@ -141,7 +136,7 @@ def resolve_ip_address(host):
try:
return socket.gethostbyname(host)
except socket.error as err:
except OSError as err:
errs.append(str(err))
raise EsphomeError("Error resolving IP address: {}"
"".format(', '.join(errs)))
@@ -167,10 +162,10 @@ def read_file(path):
return f_handle.read()
except OSError as err:
from esphome.core import EsphomeError
raise EsphomeError(u"Error reading file {}: {}".format(path, err))
raise EsphomeError(f"Error reading file {path}: {err}")
except UnicodeDecodeError as err:
from esphome.core import EsphomeError
raise EsphomeError(u"Error reading file {}: {}".format(path, err))
raise EsphomeError(f"Error reading file {path}: {err}")
def _write_file(path, text):
@@ -179,20 +174,17 @@ def _write_file(path, text):
mkdir_p(directory)
tmp_path = None
data = encode_text(text)
data = text
if isinstance(text, str):
data = text.encode()
try:
with tempfile.NamedTemporaryFile(mode="wb", dir=directory, delete=False) as f_handle:
tmp_path = f_handle.name
f_handle.write(data)
# Newer tempfile implementations create the file with mode 0o600
os.chmod(tmp_path, 0o644)
if IS_PY2:
if os.path.exists(path):
os.remove(path)
os.rename(tmp_path, path)
else:
# If destination exists, will be overwritten
os.replace(tmp_path, path)
# If destination exists, will be overwritten
os.replace(tmp_path, path)
finally:
if tmp_path is not None and os.path.exists(tmp_path):
try:
@@ -206,7 +198,7 @@ def write_file(path, text):
_write_file(path, text)
except OSError:
from esphome.core import EsphomeError
raise EsphomeError(u"Could not write file at {}".format(path))
raise EsphomeError(f"Could not write file at {path}")
def write_file_if_changed(path, text):
@@ -226,7 +218,7 @@ def copy_file_if_changed(src, dst):
shutil.copy(src, dst)
except OSError as err:
from esphome.core import EsphomeError
raise EsphomeError(u"Error copying file {} to {}: {}".format(src, dst, err))
raise EsphomeError(f"Error copying file {src} to {dst}: {err}")
def list_starts_with(list_, sub):
@@ -273,10 +265,6 @@ _TYPE_OVERLOADS = {
list: type('EList', (list,), dict()),
}
if IS_PY2:
_TYPE_OVERLOADS[long] = type('long', (long,), dict())
_TYPE_OVERLOADS[unicode] = type('unicode', (unicode,), dict())
# cache created classes here
_CLASS_LOOKUP = {}