Pylint settings

This commit is contained in:
Firq 2024-07-05 16:05:35 +02:00
parent be932ea9c4
commit a99b81b0c7
Signed by: Firq
GPG key ID: 3ACC61C8CEC83C20
12 changed files with 140 additions and 3 deletions
dockge_cli/service

View file

@ -5,8 +5,14 @@ import socketio.exceptions
from . import storage
class DockgeConnection:
"""
Provider class for Dockge
Provides all the functionality for connecting, logging in and executing commands
"""
class LoginException(BaseException):
pass
"""
Special exception when login fails too often
"""
_sio: socketio.Client
_host: str
@ -61,10 +67,17 @@ class DockgeConnection:
# Functions
def connect_and_login(self):
"""
Connect to the websocket
"""
self._sio.connect(f"https://{self._host}/socket.io/", transports=['websocket'])
self.connect()
def connect(self):
"""
Log into dockge using basicauth
Retries 5 times when timeouts occur
"""
if self._logged_in:
return
@ -92,6 +105,9 @@ class DockgeConnection:
self._logged_in = True
def list_stacks(self):
"""
Requests stack list from dockge, returns list when event was sent
"""
self._sio.emit("agent", ("", "requestStackList"))
while self._stacklist is None:
time.sleep(0.5)
@ -100,29 +116,50 @@ class DockgeConnection:
return retval
def list_stack(self, name: str):
"""
Lists status for a stack
"""
ret = self._sio.call("agent", ("", "serviceStatusList", name), timeout=5)
return ret
def restart(self, name):
"""
Restarts a given stack
"""
ret = self._sio.call("agent", ("", "restartStack", name), timeout=10)
return ret
def update(self, name):
"""
Updates a given stack
"""
ret = self._sio.call("agent", ("", "updateStack", name), timeout=10)
return ret
def stop(self, name):
"""
Stops a given stack
"""
ret = self._sio.call("agent", ("", "stopStack", name), timeout=10)
return ret
def start(self, name):
"""
Starts a given stack
"""
ret = self._sio.call("agent", ("", "startStack", name), timeout=10)
return ret
def down(self, name):
"""
Stops and downs a given stack
"""
ret = self._sio.call("agent", ("", "downStack", name), timeout=10)
return ret
def disconnect(self):
"""
Logs out of dockge
"""
self._sio.emit("logout")
self._sio.disconnect()

View file

@ -9,11 +9,18 @@ _file = _storagepath / "storage.yaml"
_storagepath.mkdir(exist_ok=True, parents=True)
def fileexists():
"""
Checks if storage file does exist, creates it when necessary
"""
if not _file.exists():
with open(_file, 'a', encoding="utf-8"):
os.utime(_file, None)
def put(key: str, value: str, encoded=False):
"""
Puts a given value with a given key into the storage file
Encodes the data as base64 when encoded is set to true
"""
fileexists()
with open(_file, "r+", encoding="utf-8") as file:
content: dict[str, str] = yaml.load(file, Loader=yaml.SafeLoader) or {}
@ -22,6 +29,9 @@ def put(key: str, value: str, encoded=False):
yaml.dump(content, file, Dumper=yaml.SafeDumper)
def remove(key: str):
"""
Removed a given key from the storage file
"""
fileexists()
with open(_file, "r", encoding="utf-8") as file:
content: dict[str, str] = yaml.load(file, Loader=yaml.SafeLoader) or {}
@ -30,6 +40,10 @@ def remove(key: str):
yaml.dump(content, file, Dumper=yaml.SafeDumper)
def get(key: str, encoded=False):
"""
Retrieves a value for a given key from the storage file
If the value was encoded, encoded needs to be set True to decode it again
"""
value: str | None = None
if not _file.exists():
return None
@ -41,4 +55,7 @@ def get(key: str, encoded=False):
return base64.b64decode(value).decode() if encoded else value
def clear():
"""
Deletes the storage file
"""
_file.unlink()