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
+19 -30
View File
@@ -1,5 +1,4 @@
# pylint: disable=wrong-import-position
from __future__ import print_function
import codecs
import collections
@@ -29,7 +28,6 @@ import tornado.websocket
from esphome import const, util
from esphome.__main__ import get_serial_ports
from esphome.helpers import mkdir_p, get_bool_env, run_system_command
from esphome.py_compat import IS_PY2, decode_text, encode_text
from esphome.storage_json import EsphomeStorageJSON, StorageJSON, \
esphome_storage_path, ext_storage_path, trash_storage_path
from esphome.util import shlex_quote
@@ -42,7 +40,7 @@ from esphome.zeroconf import DashboardStatus, Zeroconf
_LOGGER = logging.getLogger(__name__)
class DashboardSettings(object):
class DashboardSettings:
def __init__(self):
self.config_dir = ''
self.password_digest = ''
@@ -58,10 +56,7 @@ class DashboardSettings(object):
self.username = args.username or os.getenv('USERNAME', '')
self.using_password = bool(password)
if self.using_password:
if IS_PY2:
self.password_digest = hmac.new(password).digest()
else:
self.password_digest = hmac.new(password.encode()).digest()
self.password_digest = hmac.new(password.encode()).digest()
self.config_dir = args.configuration[0]
@property
@@ -88,8 +83,8 @@ class DashboardSettings(object):
if username != self.username:
return False
password_digest = hmac.new(encode_text(password)).digest()
return hmac.compare_digest(self.password_digest, password_digest)
password = hmac.new(password.encode()).digest()
return username == self.username and hmac.compare_digest(self.password_digest, password)
def rel_path(self, *args):
return os.path.join(self.config_dir, *args)
@@ -100,10 +95,7 @@ class DashboardSettings(object):
settings = DashboardSettings()
if IS_PY2:
cookie_authenticated_yes = 'yes'
else:
cookie_authenticated_yes = b'yes'
cookie_authenticated_yes = b'yes'
def template_args():
@@ -181,7 +173,7 @@ def websocket_method(name):
@websocket_class
class EsphomeCommandWebSocket(tornado.websocket.WebSocketHandler):
def __init__(self, application, request, **kwargs):
super(EsphomeCommandWebSocket, self).__init__(application, request, **kwargs)
super().__init__(application, request, **kwargs)
self._proc = None
self._is_closed = False
@@ -204,7 +196,7 @@ class EsphomeCommandWebSocket(tornado.websocket.WebSocketHandler):
# spawn can only be called once
return
command = self.build_command(json_message)
_LOGGER.info(u"Running command '%s'", ' '.join(shlex_quote(x) for x in command))
_LOGGER.info("Running command '%s'", ' '.join(shlex_quote(x) for x in command))
self._proc = tornado.process.Subprocess(command,
stdout=tornado.process.Subprocess.STREAM,
stderr=subprocess.STDOUT,
@@ -227,10 +219,7 @@ class EsphomeCommandWebSocket(tornado.websocket.WebSocketHandler):
@tornado.gen.coroutine
def _redirect_stdout(self):
if IS_PY2:
reg = '[\n\r]'
else:
reg = b'[\n\r]'
reg = b'[\n\r]'
while True:
try:
@@ -336,8 +325,8 @@ class WizardRequestHandler(BaseHandler):
def post(self):
from esphome import wizard
kwargs = {k: u''.join(decode_text(x) for x in v) for k, v in self.request.arguments.items()}
destination = settings.rel_path(kwargs['name'] + u'.yaml')
kwargs = {k: ''.join(str(x) for x in v) for k, v in self.request.arguments.items()}
destination = settings.rel_path(kwargs['name'] + '.yaml')
wizard.wizard_write(path=destination, **kwargs)
self.redirect('./?begin=True')
@@ -355,8 +344,8 @@ class DownloadBinaryRequestHandler(BaseHandler):
path = storage_json.firmware_bin_path
self.set_header('Content-Type', 'application/octet-stream')
filename = '{}.bin'.format(storage_json.name)
self.set_header("Content-Disposition", 'attachment; filename="{}"'.format(filename))
filename = f'{storage_json.name}.bin'
self.set_header("Content-Disposition", f'attachment; filename="{filename}"')
with open(path, 'rb') as f:
while True:
data = f.read(16384)
@@ -371,7 +360,7 @@ def _list_dashboard_entries():
return [DashboardEntry(file) for file in files]
class DashboardEntry(object):
class DashboardEntry:
def __init__(self, path):
self.path = path
self._storage = None
@@ -609,8 +598,8 @@ class LoginHandler(BaseHandler):
'X-HASSIO-KEY': os.getenv('HASSIO_TOKEN'),
}
data = {
'username': decode_text(self.get_argument('username', '')),
'password': decode_text(self.get_argument('password', ''))
'username': self.get_argument('username', ''),
'password': self.get_argument('password', '')
}
try:
req = requests.post('http://hassio/auth', headers=headers, data=data)
@@ -627,8 +616,8 @@ class LoginHandler(BaseHandler):
self.render_login_page(error="Invalid username or password")
def post_native_login(self):
username = decode_text(self.get_argument("username", ''))
password = decode_text(self.get_argument("password", ''))
username = self.get_argument("username", '')
password = self.get_argument("password", '')
if settings.check_password(username, password):
self.set_secure_cookie("authenticated", cookie_authenticated_yes)
self.redirect("/")
@@ -663,7 +652,7 @@ def get_static_file_url(name):
with open(path, 'rb') as f_handle:
hash_ = hashlib.md5(f_handle.read()).hexdigest()[:8]
_STATIC_FILE_HASHES[name] = hash_
return u'./static/{}?hash={}'.format(name, hash_)
return f'./static/{name}?hash={hash_}'
def make_app(debug=False):
@@ -754,7 +743,7 @@ def start_web_server(args):
if args.open_ui:
import webbrowser
webbrowser.open('localhost:{}'.format(args.port))
webbrowser.open(f'localhost:{args.port}')
if settings.status_use_ping:
status_thread = PingStatusThread()