-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve speedtest output, bump version
- Loading branch information
Showing
6 changed files
with
77 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[tool.poetry] | ||
name = "TrafficToll" | ||
version = "1.3.1" | ||
version = "1.4.0" | ||
description = "NetLimiter-like bandwidth limiting and QoS for Linux" | ||
authors = ["Chris Braun <[email protected]>"] | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,14 @@ | ||
class DependencyError(Exception): | ||
class TrafficTollException(Exception): | ||
pass | ||
|
||
|
||
class ConfigError(Exception): | ||
class MissingDependencyError(TrafficTollException): | ||
pass | ||
|
||
|
||
class DependencyOutputError(TrafficTollException): | ||
pass | ||
|
||
|
||
class ConfigError(TrafficTollException): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,74 @@ | ||
import collections | ||
import enum | ||
import json | ||
import subprocess | ||
from typing import Tuple, Optional | ||
from typing import Optional | ||
|
||
from .exceptions import DependencyError | ||
from .exceptions import DependencyOutputError | ||
from .utils import run | ||
|
||
SPEEDTEST_VERSION_COMMAND = "speedtest --version" | ||
OOKLA_SPEEDTEST_COMMAND = "speedtest --format=json" | ||
SIVEL_SPEEDTEST_COMMAND = "speedtest --json" | ||
|
||
SpeedTestResult = collections.namedtuple("SpeedTest", ["download_rate", "upload_rate"]) | ||
|
||
|
||
class _SpeedTestProvider(enum.Enum): | ||
Ookla = enum.auto() | ||
Sivel = enum.auto() | ||
Unknown = enum.auto() | ||
|
||
|
||
# https://www.speedtest.net/apps/cli | ||
def _ookla_speedtest_cli() -> Optional[Tuple[int, int]]: | ||
def _ookla_speedtest_cli() -> SpeedTestResult: | ||
process = run( | ||
"speedtest --format=json", stdout=subprocess.PIPE, universal_newlines=True, | ||
OOKLA_SPEEDTEST_COMMAND, stdout=subprocess.PIPE, universal_newlines=True, | ||
) | ||
|
||
try: | ||
result = json.loads(process.stdout) | ||
return result["download"]["bandwidth"], result["upload"]["bandwidth"] | ||
return SpeedTestResult( | ||
result["download"]["bandwidth"], result["upload"]["bandwidth"] | ||
) | ||
except (json.JSONDecodeError, KeyError): | ||
return None | ||
raise DependencyOutputError( | ||
f"Command: {OOKLA_SPEEDTEST_COMMAND!r} returned unrecognized output: " | ||
f"{process.stdout!r}" | ||
) | ||
|
||
|
||
# https://github.com/sivel/speedtest-cli | ||
def _sivel_speedtest_cli() -> Optional[Tuple[int, int]]: | ||
process = run("speedtest --json", stdout=subprocess.PIPE, universal_newlines=True) | ||
def _sivel_speedtest_cli() -> SpeedTestResult: | ||
process = run( | ||
SIVEL_SPEEDTEST_COMMAND, stdout=subprocess.PIPE, universal_newlines=True | ||
) | ||
|
||
try: | ||
result = json.loads(process.stdout) | ||
return round(result["download"]), round(result["upload"]) | ||
return SpeedTestResult(round(result["download"]), round(result["upload"])) | ||
except (json.JSONDecodeError, KeyError): | ||
pass | ||
raise DependencyOutputError( | ||
f"Command: {SIVEL_SPEEDTEST_COMMAND!r} returned unrecognized output: " | ||
f"{process.stdout!r}" | ||
) | ||
|
||
|
||
def test_speed() -> Optional[Tuple[int, int]]: | ||
try: | ||
process = run( | ||
"speedtest --version", stdout=subprocess.PIPE, universal_newlines=True | ||
) | ||
except DependencyError: | ||
return | ||
def _get_speedtest_provider() -> _SpeedTestProvider: | ||
process = run( | ||
SPEEDTEST_VERSION_COMMAND, stdout=subprocess.PIPE, universal_newlines=True | ||
) | ||
if process.stdout.startswith("Speedtest by Ookla"): | ||
return _SpeedTestProvider.Ookla | ||
elif process.stdout.startswith("speedtest-cli"): | ||
return _SpeedTestProvider.Sivel | ||
return _SpeedTestProvider.Unknown | ||
|
||
|
||
lines = process.stdout.splitlines() | ||
if not lines: | ||
return | ||
def test_speed() -> Optional[SpeedTestResult]: | ||
speedtest_version = _get_speedtest_provider() | ||
|
||
first_line = process.stdout.splitlines()[0] | ||
if first_line.startswith("Speedtest by Ookla"): | ||
if speedtest_version is _SpeedTestProvider.Ookla: | ||
return _ookla_speedtest_cli() | ||
elif first_line.startswith("speedtest-cli"): | ||
elif speedtest_version is _SpeedTestProvider.Sivel: | ||
return _sivel_speedtest_cli() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters