From 4d3f2b32c7369044eeab5a854522f9c52480a78d Mon Sep 17 00:00:00 2001 From: deeplow Date: Thu, 25 Jan 2024 10:17:07 +0000 Subject: [PATCH] Revert "Add Stopwatch implementation" This reverts commit 344d6f7bfa8f4e379ee7a89455b7b9254b220bea. Stopwatch is no longer needed now that we're removing timeouts. --- dangerzone/util.py | 61 +--------------------------------------------- 1 file changed, 1 insertion(+), 60 deletions(-) diff --git a/dangerzone/util.py b/dangerzone/util.py index bdd6452..dc188f2 100644 --- a/dangerzone/util.py +++ b/dangerzone/util.py @@ -3,8 +3,7 @@ import platform import string import subprocess import sys -import time -from typing import Optional, Self +from typing import Optional import appdirs @@ -73,61 +72,3 @@ def replace_control_chars(untrusted_str: str) -> str: for char in untrusted_str: sanitized_str += char if char in string.printable else "_" return sanitized_str - - -class Stopwatch: - """A simple stopwatch implementation. - - This class offers a very simple stopwatch implementation, with the following - interface: - - * self.start(): Start the stopwatch. - * self.stop(): Stop the stopwatch. - * self.elapsed: Measure the time from now since when the stopwatch started. If the - stopwatch has stopped, measure the time until stopped. - * self.remaining: If the user has provided a timeout, measure the time remaining - until the timeout expires. Will raise a TimeoutError if the timeout has been - surpassed. - - This class can also be used as a context manager. - """ - - def __init__(self, timeout: Optional[float] = None) -> None: - self.timeout = timeout - self.start_time: Optional[float] = None - self.end_time: Optional[float] = None - - @property - def elapsed(self) -> float: - """Check how much time has passed since the start of the stopwatch.""" - if self.start_time is None: - raise RuntimeError("The stopwatch has not started yet") - return (self.end_time or time.monotonic()) - self.start_time - - @property - def remaining(self) -> float: - """Check how much time remains until the timeout expires (if provided).""" - if self.timeout is None: - raise RuntimeError("Cannot calculate remaining time without timeout") - - remaining = self.timeout - self.elapsed - - if remaining < 0: - raise TimeoutError( - "Timeout ({timeout}s) has been surpassed by {-remaining}s" - ) - - return remaining - - def __enter__(self) -> "Stopwatch": - self.start_time = time.monotonic() - return self - - def start(self) -> None: - self.__enter__() - - def __exit__(self, *args: list) -> None: - self.end_time = time.monotonic() - - def stop(self) -> None: - self.__exit__()