Skip to content

Commit

Permalink
Merge pull request #35 from corail-research/linting
Browse files Browse the repository at this point in the history
Linting
  • Loading branch information
arc-hugo authored Aug 1, 2024
2 parents b187652 + 8d177b5 commit a82df46
Show file tree
Hide file tree
Showing 20 changed files with 87 additions and 741 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/python-linting.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.9"]
python-version: ["3.12"]

steps:
- uses: Actions/checkout@v3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/python-testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.9","3.10","3.11"]
python-version: ["3.9","3.10","3.11","3.12"]

steps:
- uses: Actions/checkout@v3
Expand Down
Binary file modified dev_requirements.txt
Binary file not shown.
22 changes: 11 additions & 11 deletions gen_ref_pages.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
"""Generate the code reference pages."""

import re
from pathlib import Path

import mkdocs_gen_files
import re

for path in sorted(Path("src").rglob("*.py")): #
if len(re.findall('(^|/)__',str(path))):
for path in sorted(Path("src").rglob("*.py")):
if len(re.findall("(^|/)__",str(path))):
continue

module_path = path.relative_to("src").with_suffix("") #
doc_path = path.relative_to("src").with_suffix(".md") #
full_doc_path = Path("reference", doc_path) #
module_path = path.relative_to("src").with_suffix("")
doc_path = path.relative_to("src").with_suffix(".md")
full_doc_path = Path("reference", doc_path)

parts = list(module_path.parts)

if parts[-1] == "__init__": #
if parts[-1] == "__init__":
parts = parts[:-1]
elif parts[-1] == "__main__":
continue

with mkdocs_gen_files.open(full_doc_path, "w") as fd: #
identifier = ".".join(parts) #
print("::: " + identifier, file=fd) #
with mkdocs_gen_files.open(full_doc_path, "w") as fd:
identifier = ".".join(parts)
print("::: " + identifier, file=fd)

mkdocs_gen_files.set_edit_path(full_doc_path, path) #
mkdocs_gen_files.set_edit_path(full_doc_path, path)
Empty file removed src/seahorse/execution/__init__.py
Empty file.
140 changes: 0 additions & 140 deletions src/seahorse/execution/exec_multi.py

This file was deleted.

2 changes: 2 additions & 0 deletions src/seahorse/game/action.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from __future__ import annotations

from abc import abstractmethod

from seahorse.utils.serializer import Serializable


class Action(Serializable):
"""
A generic class representing an action in the game.
Expand Down
4 changes: 3 additions & 1 deletion src/seahorse/game/game_state.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from abc import abstractmethod
from itertools import cycle
from typing import Any

from seahorse.game.action import Action
from seahorse.game.heavy_action import HeavyAction
from seahorse.game.light_action import LightAction
Expand Down Expand Up @@ -235,5 +236,6 @@ def __eq__(self, value: object) -> bool:

def __str__(self) -> str:
to_print = f"Current scores are {self.get_scores()}.\n"
to_print += f"Next person to play is player {self.get_next_player().get_id()} ({self.get_next_player().get_name()}).\n"
to_print += f"Next person to play is player {self.get_next_player().get_id()} \
({self.get_next_player().get_name()}).\n"
return to_print
3 changes: 2 additions & 1 deletion src/seahorse/game/heavy_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ def __eq__(self, value: object) -> bool:
return hash(self) == hash(value)

def __str__(self) -> str:
return "From:\n" + self.get_current_game_state().get_rep().__str__() + "\nto:\n" + self.get_next_game_state().get_rep().__str__()
return "From:\n" + self.get_current_game_state().get_rep().__str__() + "\nto:\n" \
+ self.get_next_game_state().get_rep().__str__()

def to_json(self) -> dict:
return self.__dict__
41 changes: 24 additions & 17 deletions src/seahorse/game/io_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
import asyncio
import functools
import json
from collections import deque
import time
import re
from typing import TYPE_CHECKING, Any, Callable, Coroutine
import time
from collections import deque
from collections.abc import Coroutine
from typing import TYPE_CHECKING, Any, Callable

import socketio
from aiohttp import web
Expand All @@ -33,7 +34,8 @@ def activate(self,
Args:
identifier (str | None, optional): Must be a unique identifier. Defaults to None.
wrapped_id (int | None, optional): If the eventSlave is bound to an instance a python native id might be associated. Defaults to None.
wrapped_id (int | None, optional): If the eventSlave is instance bounded, a native id might be associated.
Defaults to None.
"""
self.sio = socketio.AsyncClient()
self.connected = False
Expand Down Expand Up @@ -92,7 +94,8 @@ def remote_action(label: str):
def meta_wrapper(fun: Callable):
@functools.wraps(fun)
async def wrapper(self:EventSlave,current_state:GameState,*_,**kwargs):
await EventMaster.get_instance().sio.emit(label,json.dumps({**current_state.to_json(),**kwargs},default=lambda x:x.to_json()),to=self.sid)
await EventMaster.get_instance().sio.emit(label,json.dumps({**current_state.to_json(),**kwargs},
default=lambda x:x.to_json()),to=self.sid)
out = await EventMaster.get_instance().wait_for_next_play(self.sid,current_state.players)
return out

Expand Down Expand Up @@ -120,7 +123,8 @@ def get_instance(game_state:type=Serializable,port: int = 8080,hostname: str="lo
"""Gets the instance object
Args:
n_clients (int, optional): the number of clients the instance is supposed to be listening, *ignored* if already initialized. Defaults to 1.
n_clients (int, optional): the number of clients the instance is supposed to be listening,
*ignored* if already initialized. Defaults to 1.
game_state : class of a game state
port (int, optional): the port to use. Defaults to 8080.
Expand All @@ -133,7 +137,8 @@ def get_instance(game_state:type=Serializable,port: int = 8080,hostname: str="lo

def __init__(self,game_state,port,hostname):
if EventMaster.__instance is not None:
msg = "Trying to initialize multiple instances of EventMaster, this is forbidden to avoid side-effects.\n Call EventMaster.get_instance() instead."
msg = "Trying to initialize multiple instances of EventMaster, this is forbidden to avoid side-effects.\n\
Call EventMaster.get_instance() instead."
raise NotImplementedError(msg)
else:
# Initializing attributes
Expand All @@ -149,7 +154,8 @@ def __init__(self,game_state,port,hostname):
self.hostname = hostname

# Standard python-socketio server
self.sio = socketio.AsyncServer(async_mode="aiohttp", async_handlers=True, cors_allowed_origins="*", ping_timeout=1e6)
self.sio = socketio.AsyncServer(async_mode="aiohttp", async_handlers=True,
cors_allowed_origins="*", ping_timeout=1e6)
self.app = web.Application()

# Starting asyncio stuff
Expand All @@ -176,7 +182,8 @@ def connect(sid, *_):
"""
self.__open_sessions.add(sid)
self.__n_clients_connected += 1
logger.info(f"Waiting for listeners {self.__n_clients_connected} out of {self.expected_clients} are connected.")
logger.info(f"Waiting for listeners {self.__n_clients_connected} out of "
f"{self.expected_clients} are connected.")

@self.sio.event
def disconnect(sid):
Expand Down Expand Up @@ -267,7 +274,9 @@ async def wait_for_event(self,sid:int,label:str,*,flush_until:float | None=None)
while not len(self.__events.get(sid,{}).get(label,[])):
await asyncio.sleep(.1)
ts,data = self.__events[sid][label].pop()
return data if (not flush_until) or ts>=flush_until else await self.wait_for_event(sid,label,flush_until=flush_until)
if (not flush_until) or ts>=flush_until:
return data
await self.wait_for_event(sid,label,flush_until=flush_until)

async def wait_for_identified_client(self,name:str,local_id:int) -> str:
""" Waits for an identified client (a player typically)
Expand Down Expand Up @@ -299,10 +308,9 @@ def start(self, task: Callable[[None], None], listeners: list[EventSlave]) -> No
Starts an emitting sequence and runs a tasks that embeds
calls to `EventMaster.__instance.sio.emit()`
The starting of the game is starting when for all `EventSlave` instances in `listeners`, the `.listen()` future is fulfilled.
The game is starting when for all `EventSlave` in `listeners`, the `.listen()` future is fulfilled.
If `len(listeners)==0` the EventMaster emits events
in the void.
If `len(listeners)==0` the EventMaster emits events in the void.
Args:
task (Callable[[None],None]): task calling `EventMaster.sio.emit()`
Expand All @@ -312,20 +320,19 @@ def start(self, task: Callable[[None], None], listeners: list[EventSlave]) -> No

# Sets the runner up and starts the tcp server
self.event_loop.run_until_complete(self.runner.setup())
#print(self.port)
site = web.TCPSite(self.runner, self.hostname, self.port)
self.event_loop.run_until_complete(site.start())

async def stop():

# Waiting for all
logger.info(f"Waiting for listeners {self.__n_clients_connected} out of {self.expected_clients} are connected.")
logger.info(f"Waiting for listeners {self.__n_clients_connected} "
f"out of {self.expected_clients} are connected.")
for x in slaves:
await x.listen(master_address=f"http://{self.hostname}:{self.port!s}", keep_alive=False)

logger.info("Starting match")

# Launching the task
logger.info("Starting match")
task_future = asyncio.wrap_future(self.sio.start_background_task(task))
await task_future

Expand Down
1 change: 1 addition & 0 deletions src/seahorse/game/light_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from seahorse.game.action import Action
from seahorse.game.heavy_action import HeavyAction
from seahorse.utils.custom_exceptions import NoGameStateProvidedError

if TYPE_CHECKING:
from seahorse.game.game_state import GameState

Expand Down
Loading

0 comments on commit a82df46

Please sign in to comment.