Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci, https://pre-commit.com and
the `.pre-commit-config.yaml` file in this repository.
  • Loading branch information
pre-commit-ci[bot] committed Dec 4, 2023
1 parent 0ca2117 commit 0b73226
Show file tree
Hide file tree
Showing 31 changed files with 428 additions and 405 deletions.
1 change: 0 additions & 1 deletion .github/ISSUE_TEMPLATE/config.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
blank_issues_enabled: true
contact_links:

2 changes: 0 additions & 2 deletions plan/basic-protocol.txt
Original file line number Diff line number Diff line change
Expand Up @@ -239,5 +239,3 @@ state #END:close
state :end

{pass}


4 changes: 2 additions & 2 deletions python/ectec-gui/src/ectec/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@

# ---- Logging

logger = logs.getLogger(__name__) # Parent logger for the module
logger = logs.getLogger(__name__) # Parent logger for the module
logger.setLevel(logs.DEBUG)

# Disable logging output sot that users of this lib can log as desired.
nullhandler = logs.NullHandler() # Bin for logs
nullhandler = logs.NullHandler() # Bin for logs
logger.addHandler(nullhandler)

# ---- Exceptions Client Side
Expand Down
38 changes: 19 additions & 19 deletions python/ectec-gui/src/ectec/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ def filter_recipient(self, *recipients: str):
for recipient in recipients:
if recipient in pkg.recipient:
yield pkg
break # breaks inner loop
break # breaks inner loop


# ---- Client - General
Expand Down Expand Up @@ -513,13 +513,13 @@ class Client:
This class provides methods useful for all Ectec clients.
"""

TIMEOUT = 2 #: s timeout for awaited commands
TIMEOUT = 2 #: s timeout for awaited commands

TRANSMISSION_TIMEOUT = 0.200 #: seconds of timeout between parts
COMMAND_TIMEOUT = 0.300 #: s timeout for a command to end
SOCKET_BUFSIZE = 8192 #: bytes to read from socket at once
COMMAND_SEPERATOR = b'\n' #: seperates commands of the ectec protocol
COMMAND_LENGTH = 4096 #: bytes - the maximum length of a command
TRANSMISSION_TIMEOUT = 0.200 #: seconds of timeout between parts
COMMAND_TIMEOUT = 0.300 #: s timeout for a command to end
SOCKET_BUFSIZE = 8192 #: bytes to read from socket at once
COMMAND_SEPERATOR = b'\n' #: seperates commands of the ectec protocol
COMMAND_LENGTH = 4096 #: bytes - the maximum length of a command

def __init__(self):
self.socket: socket.SocketType = None
Expand Down Expand Up @@ -563,7 +563,7 @@ def recv_bytes(self, length, start_timeout=None, timeout=None) -> bytes:
self.socket.settimeout(start_timeout)

try:
msg = self.socket.recv(bufsize) # msg was empty
msg = self.socket.recv(bufsize) # msg was empty
except socket.timeout as error:
raise CommandTimeout(
"The receiving of the data timed out.") from error
Expand All @@ -572,7 +572,7 @@ def recv_bytes(self, length, start_timeout=None, timeout=None) -> bytes:
# connection was closed
raise ConnectionClosed("The connection was closed.")

if len(msg) >= length: # separator found
if len(msg) >= length: # separator found
data = msg[:length]
self._buffer = msg[length:]

Expand All @@ -589,7 +589,7 @@ def recv_bytes(self, length, start_timeout=None, timeout=None) -> bytes:

data_length = len(msg)
needed = length - data_length
while True: # ends by an error or a return
while True: # ends by an error or a return
try:
part = self.socket.recv(bufsize)
except socket.timeout as error:
Expand Down Expand Up @@ -671,7 +671,7 @@ class variable `COMMAND_SEPERATOR`. Commands also must not exceed a
self.socket.settimeout(start_timeout)

try:
msg = self.socket.recv(bufsize) # msg was empty
msg = self.socket.recv(bufsize) # msg was empty
except socket.timeout as error:
raise CommandTimeout(
"The receiving of the command timed out.") from error
Expand All @@ -683,9 +683,9 @@ class variable `COMMAND_SEPERATOR`. Commands also must not exceed a
# check buffer or first data received
i = msg.find(seperator)

if i >= 0: # separator found
if i >= 0: # separator found
command = msg[:i]
self._buffer = msg[i + len(seperator):] # seperator is removed
self._buffer = msg[i + len(seperator):] # seperator is removed

# for expected behavoir check length of command
cmd_length = len(command)
Expand All @@ -706,7 +706,7 @@ class variable `COMMAND_SEPERATOR`. Commands also must not exceed a
timeout = timeout * 0.000000001 if timeout is not None else None

length = len(msg)
while True: # ends by an error or a return
while True: # ends by an error or a return
try:
part = self.socket.recv(bufsize)
except socket.timeout as error:
Expand All @@ -718,8 +718,8 @@ class variable `COMMAND_SEPERATOR`. Commands also must not exceed a

time_elapsed = time.perf_counter_ns() - start_time

i = part.find(seperator) # end of command?
if i >= 0: # separator found
i = part.find(seperator) # end of command?
if i >= 0: # separator found
command = msg + part[:i]
# seperator is removed
self._buffer = part[i + len(seperator):]
Expand Down Expand Up @@ -1080,7 +1080,7 @@ def run(self):

# Handle UPDATE USERS command
res = self.client.parse_update(cmd)
if res is not False: # in case of empty list
if res is not False: # in case of empty list
# update users
self.client._update_users(res)
continue
Expand Down Expand Up @@ -1266,7 +1266,7 @@ def __exit__(self, exc_type, exc_value, traceback):
self.client.disconnect()

# wether to raise the Exception or suppress
return False # do not suppress
return False # do not suppress

def connect(self, server: str, port: int):
"""
Expand Down Expand Up @@ -1377,7 +1377,7 @@ def disconnect(self):
"""
if self._thread and self._thread.is_alive():
self._thread.end.set()
self._thread.join() # raises an exception if thread isn't alive
self._thread.join() # raises an exception if thread isn't alive

if self.socket:
self.socket.close()
Expand Down
8 changes: 4 additions & 4 deletions python/ectec-gui/src/ectec/logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ def indent(text, by, space=" ", prefix="", suffix=""):
The indented text.
"""
t = "" # Stores the indented lines and will be returned
t = "" # Stores the indented lines and will be returned

# Iterate of the lines of the text
for l in text.splitlines(True):
# The indented line is added to the result
t += prefix + by*space + suffix + l
t += prefix + by * space + suffix + l

return t

Expand All @@ -87,5 +87,5 @@ def formatException(self, exc_info):
"""
Format an exception so that it prints on a single line.
"""
result = super().formatException(exc_info) # Super formats for us.
return indent(result, 2, prefix="| >>>") # Indent output
result = super().formatException(exc_info) # Super formats for us.
return indent(result, 2, prefix="| >>>") # Indent output
63 changes: 33 additions & 30 deletions python/ectec-gui/src/ectec/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ class ConnectionAdapter(logging.LoggerAdapter):
More context. The default is {}.
"""

def __init__(self, logger, remote, extra=None):
"""
Adapter to add connection context information.
Expand Down Expand Up @@ -264,13 +265,13 @@ class ClientHandler(socketserver.BaseRequestHandler):
"""

# ---- Process (server) wide constants
TIMEOUT = 0.5 #: s timeout for awaited commands
TIMEOUT = 0.5 #: s timeout for awaited commands

TRANSMISSION_TIMEOUT = 0.200 #: seconds of timeout between parts
COMMAND_TIMEOUT = 0.300 #: s timeout for a command to end
SOCKET_BUFSIZE = 8192 #: bytes to read from socket at once
COMMAND_SEPERATOR = b'\n' #: seperates commands of the ectec protocol
COMMAND_LENGTH = 4096 #: bytes - the maximum length of a command
TRANSMISSION_TIMEOUT = 0.200 #: seconds of timeout between parts
COMMAND_TIMEOUT = 0.300 #: s timeout for a command to end
SOCKET_BUFSIZE = 8192 #: bytes to read from socket at once
COMMAND_SEPERATOR = b'\n' #: seperates commands of the ectec protocol
COMMAND_LENGTH = 4096 #: bytes - the maximum length of a command

#: Users with the listed roles are shared with all clients
PUBLIC_ROLES = [Role.USER]
Expand Down Expand Up @@ -310,7 +311,7 @@ def setup(self):
self.client_data: ClientData = None

#: stores received bytes that belong to the next command
self.buffer: bytes = bytes(0) # bytes object with zero bytes
self.buffer: bytes = bytes(0) # bytes object with zero bytes

#: lock used when accessing socket for sending
self.sending_lock = threading.Lock()
Expand Down Expand Up @@ -428,9 +429,9 @@ def run(self):

if not compatible:
# Version is incompatible
self.send_info(False) # Tell the client
self.send_info(False) # Tell the client
self.log.debug('Incompatible version. Refused')
return # The connection socket is closed automatically
return # The connection socket is closed automatically

self.log.debug("Version check passed. Sending answer.")

Expand All @@ -453,14 +454,14 @@ def run(self):
raise RequestRefusedError(
f"'{role_str}' is not a valid role") from None

if role_str not in self.clients: # Defined as class variable
if role_str not in self.clients: # Defined as class variable
raise RequestRefusedError(f"'{role_str}' is not a valid role")

# Check if name is already used
with self.Locks.clients:
for dummy, client_list in self.clients.items():
for client in client_list: # client : ClientData
if client.name.lower() == name.lower(): # Design choice
for client in client_list: # client : ClientData
if client.name.lower() == name.lower(): # Design choice
# Name in use
raise RequestRefusedError(
f"'{name}' collides with an existing clients name")
Expand Down Expand Up @@ -532,7 +533,7 @@ def handle_user(self):
while True:
try:
package = self.recv_pkg()
except (OSError, ConnectionClosed) as error: # Connection closed
except (OSError, ConnectionClosed) as error: # Connection closed
return
except CommandTimeout as error:
# wait for next command
Expand Down Expand Up @@ -596,7 +597,7 @@ def recv_bytes(self, length, start_timeout=None, timeout=None) -> bytes:
self.request.settimeout(start_timeout)

try:
msg = self.request.recv(bufsize) # msg was empty
msg = self.request.recv(bufsize) # msg was empty
except socket.timeout as error:
raise CommandTimeout(
"The receiving of the data timed out.") from error
Expand All @@ -606,7 +607,7 @@ def recv_bytes(self, length, start_timeout=None, timeout=None) -> bytes:
raise ConnectionClosed(
"The connection was closed by the client.")

if len(msg) >= length: # separator found
if len(msg) >= length: # separator found
data = msg[:length]
self.buffer = msg[length:]

Expand All @@ -623,7 +624,7 @@ def recv_bytes(self, length, start_timeout=None, timeout=None) -> bytes:

data_length = len(msg)
needed = length - data_length
while True: # ends by an error or a return
while True: # ends by an error or a return
try:
part = self.request.recv(bufsize)
except socket.timeout as error:
Expand Down Expand Up @@ -704,7 +705,7 @@ class variable `COMMAND_SEPERATOR`. Commands also must not exceed a
self.request.settimeout(start_timeout)

try:
msg = self.request.recv(bufsize) # msg was empty
msg = self.request.recv(bufsize) # msg was empty
except socket.timeout as error:
raise CommandTimeout(
"The receiving of the command timed out.") from error
Expand All @@ -717,9 +718,9 @@ class variable `COMMAND_SEPERATOR`. Commands also must not exceed a
# check buffer or first data received
i = msg.find(seperator)

if i >= 0: # separator found
if i >= 0: # separator found
command = msg[:i]
self.buffer = msg[i + len(seperator):] # seperator is removed
self.buffer = msg[i + len(seperator):] # seperator is removed

# for expected behavoir check length of command
cmd_length = len(command)
Expand All @@ -740,7 +741,7 @@ class variable `COMMAND_SEPERATOR`. Commands also must not exceed a
timeout = timeout * 0.000000001 if timeout is not None else None

length = len(msg)
while True: # ends by an error or a return
while True: # ends by an error or a return
try:
part = self.request.recv(bufsize)
except socket.timeout as error:
Expand All @@ -753,10 +754,11 @@ class variable `COMMAND_SEPERATOR`. Commands also must not exceed a

time_elapsed = time.perf_counter_ns() - start_time

i = part.find(seperator) # end of command?
if i >= 0: # separator found
i = part.find(seperator) # end of command?
if i >= 0: # separator found
command = msg + part[:i]
self.buffer = part[i + len(seperator):] # seperator is removed
self.buffer = part[i +
len(seperator):] # seperator is removed

# for expected behavoir check length of command
cmd_length = len(command)
Expand Down Expand Up @@ -816,7 +818,7 @@ def recv_info(self):
cmd = raw_cmd.decode(encoding='utf-8', errors='backslashreplace')

# match regular expression
match = self.regex_info.fullmatch(cmd) # should match the whole text
match = self.regex_info.fullmatch(cmd) # should match the whole text

if not match:
raise CommandError("Received data doesn't match INFO command.")
Expand Down Expand Up @@ -1217,7 +1219,7 @@ def shutdown_request(self, request):
# the socket and waits for GC to perform the actual close.
request.shutdown(socket.SHUT_RDWR)
except OSError:
pass # some platforms may raise ENOTCONN here
pass # some platforms may raise ENOTCONN here
self.close_request(request)

def process_request_thread(self, request, client_address):
Expand Down Expand Up @@ -1246,12 +1248,12 @@ def process_request(self, request, client_address):
t.start()

# Extra part
t.name = str(client_address[0]) # name thread after ip
t.request_socket = request # add attribute
t.name = str(client_address[0]) # name thread after ip
t.request_socket = request # add attribute

def server_close(self):
"""Cleanup the server."""
super().server_close() # should call socketserver.TCPServer's method
super().server_close() # should call socketserver.TCPServer's method

# Changed from socketserver.ThreadingMixIn
if self.block_on_close:
Expand Down Expand Up @@ -1428,20 +1430,21 @@ def running(self) -> bool:

class ServerRunningContextManager:
"""The ContextManager for a running server."""

def __init__(self, server):
"""Init the ContextManager"""
self.server = server

def __enter__(self):
"""Enter the context. Does nothing."""
return self # not necessary
return self # not necessary

def __exit__(self, exc_type, exc_value, traceback):
"""Exit the context and stop the server."""
self.server.stop()

# wether to raise the Exception or suppress
return False # do not suppress
return False # do not suppress

def start(self, port: int, address: str = ""):
"""
Expand Down
2 changes: 2 additions & 0 deletions python/ectec-gui/src/ectec/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class VersionException(Exception):
Superclass of all Exceptions related to versions
"""


class Version:
"""
The superclass of all Versions.
Expand All @@ -54,6 +55,7 @@ class SubVersionException(SemanticVersionException):
Raise if there are too many subversions in a semantic version.
"""


@total_ordering
class SemanticVersion(Version):
"""
Expand Down
Loading

0 comments on commit 0b73226

Please sign in to comment.