Format all code using black

This commit is contained in:
Micah Lee 2019-10-12 21:01:25 -07:00
parent 90c244ee2f
commit 3037727890
No known key found for this signature in database
GPG key ID: 403C2657CD994F73
87 changed files with 4293 additions and 2374 deletions

View file

@ -27,21 +27,26 @@ from onionshare.onion import Onion
from onionshare import strings
class UpdateCheckerCheckError(Exception):
"""
Error checking for updates because of some Tor connection issue, or because
the OnionShare website is down.
"""
pass
class UpdateCheckerInvalidLatestVersion(Exception):
"""
Successfully downloaded the latest version, but it doesn't appear to be a
valid version string.
"""
def __init__(self, latest_version):
self.latest_version = latest_version
class UpdateChecker(QtCore.QObject):
"""
Load http://elx57ue5uyfplgva.onion/latest-version.txt to see what the latest
@ -50,6 +55,7 @@ class UpdateChecker(QtCore.QObject):
Only check at most once per day, unless force is True.
"""
update_available = QtCore.pyqtSignal(str, str, str)
update_not_available = QtCore.pyqtSignal()
update_error = QtCore.pyqtSignal()
@ -60,12 +66,12 @@ class UpdateChecker(QtCore.QObject):
self.common = common
self.common.log('UpdateChecker', '__init__')
self.common.log("UpdateChecker", "__init__")
self.onion = onion
self.config = config
def check(self, force=False, config=False):
self.common.log('UpdateChecker', 'check', 'force={}'.format(force))
self.common.log("UpdateChecker", "check", "force={}".format(force))
# Load the settings
settings = Settings(self.common, config)
settings.load()
@ -77,7 +83,7 @@ class UpdateChecker(QtCore.QObject):
check_for_updates = False
# See if it's been 1 day since the last check
autoupdate_timestamp = settings.get('autoupdate_timestamp')
autoupdate_timestamp = settings.get("autoupdate_timestamp")
if autoupdate_timestamp:
last_checked = datetime.datetime.fromtimestamp(autoupdate_timestamp)
now = datetime.datetime.now()
@ -90,45 +96,61 @@ class UpdateChecker(QtCore.QObject):
# Check for updates
if check_for_updates:
self.common.log('UpdateChecker', 'check', 'checking for updates')
self.common.log("UpdateChecker", "check", "checking for updates")
# Download the latest-version file over Tor
try:
# User agent string includes OnionShare version and platform
user_agent = 'OnionShare {}, {}'.format(self.common.version, self.common.platform)
user_agent = "OnionShare {}, {}".format(
self.common.version, self.common.platform
)
# If the update is forced, add '?force=1' to the URL, to more
# accurately measure daily users
path = '/latest-version.txt'
path = "/latest-version.txt"
if force:
path += '?force=1'
path += "?force=1"
if Version(self.onion.tor_version) >= Version('0.3.2.9'):
onion_domain = 'lldan5gahapx5k7iafb3s4ikijc4ni7gx5iywdflkba5y2ezyg6sjgyd.onion'
if Version(self.onion.tor_version) >= Version("0.3.2.9"):
onion_domain = (
"lldan5gahapx5k7iafb3s4ikijc4ni7gx5iywdflkba5y2ezyg6sjgyd.onion"
)
else:
onion_domain = 'elx57ue5uyfplgva.onion'
onion_domain = "elx57ue5uyfplgva.onion"
self.common.log('UpdateChecker', 'check', 'loading http://{}{}'.format(onion_domain, path))
self.common.log(
"UpdateChecker",
"check",
"loading http://{}{}".format(onion_domain, path),
)
(socks_address, socks_port) = self.onion.get_tor_socks_port()
socks.set_default_proxy(socks.SOCKS5, socks_address, socks_port)
s = socks.socksocket()
s.settimeout(15) # 15 second timeout
s.settimeout(15) # 15 second timeout
s.connect((onion_domain, 80))
http_request = 'GET {} HTTP/1.0\r\n'.format(path)
http_request += 'Host: {}\r\n'.format(onion_domain)
http_request += 'User-Agent: {}\r\n'.format(user_agent)
http_request += '\r\n'
s.sendall(http_request.encode('utf-8'))
http_request = "GET {} HTTP/1.0\r\n".format(path)
http_request += "Host: {}\r\n".format(onion_domain)
http_request += "User-Agent: {}\r\n".format(user_agent)
http_request += "\r\n"
s.sendall(http_request.encode("utf-8"))
http_response = s.recv(1024)
latest_version = http_response[http_response.find(b'\r\n\r\n'):].strip().decode('utf-8')
latest_version = (
http_response[http_response.find(b"\r\n\r\n") :]
.strip()
.decode("utf-8")
)
self.common.log('UpdateChecker', 'check', 'latest OnionShare version: {}'.format(latest_version))
self.common.log(
"UpdateChecker",
"check",
"latest OnionShare version: {}".format(latest_version),
)
except Exception as e:
self.common.log('UpdateChecker', 'check', '{}'.format(e))
self.common.log("UpdateChecker", "check", "{}".format(e))
self.update_error.emit()
raise UpdateCheckerCheckError
@ -140,22 +162,32 @@ class UpdateChecker(QtCore.QObject):
raise UpdateCheckerInvalidLatestVersion(latest_version)
# Update the last checked timestamp (dropping the seconds and milliseconds)
timestamp = datetime.datetime.now().replace(microsecond=0).replace(second=0).timestamp()
timestamp = (
datetime.datetime.now()
.replace(microsecond=0)
.replace(second=0)
.timestamp()
)
# Re-load the settings first before saving, just in case they've changed since we started our thread
settings.load()
settings.set('autoupdate_timestamp', timestamp)
settings.set("autoupdate_timestamp", timestamp)
settings.save()
# Do we need to update?
update_url = 'https://github.com/micahflee/onionshare/releases/tag/v{}'.format(latest_version)
update_url = "https://github.com/micahflee/onionshare/releases/tag/v{}".format(
latest_version
)
installed_version = self.common.version
if installed_version < latest_version:
self.update_available.emit(update_url, installed_version, latest_version)
self.update_available.emit(
update_url, installed_version, latest_version
)
return
# No updates are available
self.update_not_available.emit()
class UpdateThread(QtCore.QThread):
update_available = QtCore.pyqtSignal(str, str, str)
update_not_available = QtCore.pyqtSignal()
@ -167,13 +199,13 @@ class UpdateThread(QtCore.QThread):
self.common = common
self.common.log('UpdateThread', '__init__')
self.common.log("UpdateThread", "__init__")
self.onion = onion
self.config = config
self.force = force
def run(self):
self.common.log('UpdateThread', 'run')
self.common.log("UpdateThread", "run")
u = UpdateChecker(self.common, self.onion, self.config)
u.update_available.connect(self._update_available)
@ -182,28 +214,28 @@ class UpdateThread(QtCore.QThread):
u.update_invalid_version.connect(self._update_invalid_version)
try:
u.check(config=self.config,force=self.force)
u.check(config=self.config, force=self.force)
except Exception as e:
# If update check fails, silently ignore
self.common.log('UpdateThread', 'run', '{}'.format(e))
self.common.log("UpdateThread", "run", "{}".format(e))
pass
def _update_available(self, update_url, installed_version, latest_version):
self.common.log('UpdateThread', '_update_available')
self.common.log("UpdateThread", "_update_available")
self.active = False
self.update_available.emit(update_url, installed_version, latest_version)
def _update_not_available(self):
self.common.log('UpdateThread', '_update_not_available')
self.common.log("UpdateThread", "_update_not_available")
self.active = False
self.update_not_available.emit()
def _update_error(self):
self.common.log('UpdateThread', '_update_error')
self.common.log("UpdateThread", "_update_error")
self.active = False
self.update_error.emit()
def _update_invalid_version(self, latest_version):
self.common.log('UpdateThread', '_update_invalid_version')
self.common.log("UpdateThread", "_update_invalid_version")
self.active = False
self.update_invalid_version.emit(latest_version)