Docstrings and refactor
This commit is contained in:
parent
ed47cc1d76
commit
2d50edbc30
6
main.py
6
main.py
|
@ -8,6 +8,7 @@ import os
|
|||
import signal
|
||||
import time
|
||||
import subprocess
|
||||
from sys import stderr
|
||||
|
||||
|
||||
class Runner:
|
||||
|
@ -28,10 +29,9 @@ bot_runner = Runner()
|
|||
|
||||
if __name__ == "__main__":
|
||||
branch = os.popen("git rev-parse --abbrev-ref HEAD").read()[:-1]
|
||||
print("Starting")
|
||||
bot_runner.start()
|
||||
while True:
|
||||
print("Updater")
|
||||
stderr.write("main.py: Updater\n")
|
||||
os.system("git fetch --quiet")
|
||||
if (
|
||||
os.popen("git log " + branch + "..origin/" + branch + " --oneline")
|
||||
|
@ -39,7 +39,7 @@ if __name__ == "__main__":
|
|||
.strip()
|
||||
!= ""
|
||||
):
|
||||
print("Updating")
|
||||
stderr.write("main.py: Updating from git\n")
|
||||
bot_runner.stop()
|
||||
os.system("git pull --quiet")
|
||||
os.system("git gc --quiet --aggressive --prune=all")
|
||||
|
|
|
@ -0,0 +1,43 @@
|
|||
# SPDX-FileCopyrightText: 2023 Egor Guslyancev <electromagneticcyclone@disroot.org>
|
||||
#
|
||||
# SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
"The module provides class to check timeouts."
|
||||
|
||||
import time
|
||||
import typing
|
||||
|
||||
|
||||
class Timeout:
|
||||
"Timeout checking. Period in seconds."
|
||||
__timeout = None
|
||||
__caution = True
|
||||
|
||||
def __init__(self, period: int):
|
||||
self.period = period
|
||||
|
||||
@property
|
||||
def period(self) -> int:
|
||||
"Period property."
|
||||
return self.__period
|
||||
|
||||
@period.setter
|
||||
def period(self, period: int):
|
||||
self.__period = period
|
||||
|
||||
def check(self, once_func: typing.Callable, always_func: typing.Callable) -> bool:
|
||||
"""
|
||||
Check if time is out.
|
||||
`once_func` called once if time is not out, but `check` called twice.
|
||||
`always_func` called when `check` called twice or more.
|
||||
"""
|
||||
if self.__timeout is not None:
|
||||
if time.time() - self.__timeout < self.period:
|
||||
if self.__caution:
|
||||
once_func()
|
||||
self.__caution = False
|
||||
always_func()
|
||||
return True
|
||||
self.__caution = True
|
||||
self.__timeout = time.time()
|
||||
return False
|
Loading…
Reference in New Issue