Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 3d7a03fb2b | |||
| 7e26db26c7 | |||
| 732afb65c5 | |||
| 2c2e3b0597 | |||
| 181b928c25 |
@@ -30,6 +30,9 @@ build-backend = "setuptools.build_meta"
|
||||
[project.scripts]
|
||||
elironwars = "elironwars.app:app"
|
||||
|
||||
[project.entry-points."elironwars.ui"]
|
||||
console = "elironwars.console:ConsoleUi"
|
||||
|
||||
[tool.isort]
|
||||
profile = "black"
|
||||
src_paths = ["src", "tests"]
|
||||
|
||||
@@ -2,14 +2,17 @@ import argparse
|
||||
import logging
|
||||
import os
|
||||
import sys
|
||||
from typing import Any
|
||||
|
||||
from .console import UI
|
||||
from .entrypoints import Entrypoints
|
||||
from .level import Level
|
||||
from .menu import Action
|
||||
from .mob import BaseMob, load_character
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# FIXME replace this global declaration
|
||||
UI: Any = None
|
||||
|
||||
def play_room(room: list[BaseMob], level: Level) -> int:
|
||||
if room is None:
|
||||
@@ -55,13 +58,15 @@ def play_donjon(level: Level) -> None:
|
||||
|
||||
|
||||
def app() -> None:
|
||||
entrypoints = Entrypoints()
|
||||
|
||||
parser = argparse.ArgumentParser(
|
||||
prog='elironwars',
|
||||
description='elironwars CLI',
|
||||
)
|
||||
parser.add_argument('--player', default='Jamin')
|
||||
parser.add_argument('--ui', default='cli')
|
||||
parser.add_argument('--log-level', default='ERROR')
|
||||
parser.add_argument('--ui', default='console', choices=entrypoints.ui.names)
|
||||
parser.add_argument('--log-level', default='ERROR', choices=logging.getLevelNamesMapping().keys())
|
||||
args = parser.parse_args()
|
||||
|
||||
os.makedirs('logs', exist_ok=True)
|
||||
@@ -72,6 +77,7 @@ def app() -> None:
|
||||
if not loaded_character:
|
||||
sys.exit(-1)
|
||||
|
||||
UI = entrypoints.load_ui(args.ui)
|
||||
UI.select_player(loaded_character)
|
||||
|
||||
# Créer des instances de BaseMob
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
import logging
|
||||
from importlib.metadata import entry_points
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class Entrypoints:
|
||||
|
||||
def __init__(self):
|
||||
self.ui = entry_points(group='elironwars.ui')
|
||||
|
||||
def load_ui(self, ui):
|
||||
logger.info('Loading ui %s', ui)
|
||||
try:
|
||||
ui_class = self.ui[ui].load()
|
||||
return ui_class()
|
||||
except KeyError:
|
||||
logger.critical('Unable to load ui %s', ui)
|
||||
@@ -0,0 +1,25 @@
|
||||
import unittest
|
||||
|
||||
from elironwars.entrypoints import Entrypoints
|
||||
from elironwars.console import ConsoleUi
|
||||
|
||||
|
||||
class TestEntrypoints(unittest.TestCase):
|
||||
|
||||
def test_load_console_ui(self):
|
||||
console_ep = 'console'
|
||||
|
||||
ep = Entrypoints()
|
||||
self.assertIn(console_ep, ep.ui.names)
|
||||
self.assertIsInstance(ep.load_ui(console_ep), ConsoleUi)
|
||||
|
||||
def test_load_unknown_ui(self):
|
||||
unknown_ep = 'unknown'
|
||||
|
||||
ep = Entrypoints()
|
||||
self.assertNotIn(unknown_ep, ep.ui.names)
|
||||
self.assertIsNone(ep.load_ui(unknown_ep), ConsoleUi)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user