Skip to content

Commit 7b604b4

Browse files
committed
Added runner docs
1 parent 2fbafa0 commit 7b604b4

File tree

6 files changed

+141
-4
lines changed

6 files changed

+141
-4
lines changed

docs/API/Runners.md

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Runners
2+
3+
## Runners Demo
4+
::: runners.demo
5+
6+
## Runners Kiosk
7+
::: runners.kiosk
8+
9+
## Runners Simulator
10+
::: runners.simulator
11+
12+
## Runners Test
13+
::: runners.test
14+
15+
## Runners Utils
16+
::: runners.utils

runners/demo.py

+16-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,14 @@
99

1010

1111
def run(demo_name, simulate, testing):
12-
"""Main function that runs the demo."""
12+
"""Main function that runs the demo.
13+
14+
Args:
15+
demo_name (str): Name of the demo to run.
16+
simulate (bool): Whether to simulate the screen or use the physical screen.
17+
testing (bool): Whether to run the demo in testing mode.
18+
19+
"""
1320

1421
if simulate:
1522
from display.virtual_screen import ( # pylint: disable=import-outside-toplevel
@@ -77,6 +84,14 @@ def run(demo_name, simulate, testing):
7784

7885

7986
def _tick(runner, demo, testing):
87+
"""Run the demo for one tick.
88+
89+
Args:
90+
runner (generator): Generator that runs the demo.
91+
demo (Demo): Demo object.
92+
testing (bool): Whether to run the demo in testing mode.
93+
94+
"""
8095
if testing:
8196
before_time = time.time()
8297

runners/kiosk.py

+53-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ def load_demo(module_name):
1515
"""
1616
Given a module name it will load the module and get the modules demo
1717
class.
18+
19+
Args:
20+
module_name (str): Name of the module to load.
21+
22+
Returns:
23+
class: The demo class.
1824
"""
1925
logger.debug(f"Loading {module_name}")
2026
return utils.get_demo_cls(import_module(module_name))
@@ -24,6 +30,12 @@ def load_demos(demo_dir="demos"):
2430
"""
2531
Loads all demos for a given directory. It returns the demos in a dictionary
2632
with the name of the demo as key and the demo module as the value.
33+
34+
Args:
35+
demo_dir (str): Directory where the demos are located.
36+
37+
Returns:
38+
dict: Dictionary with the name of the demo as key and the demo module
2739
"""
2840
logger.debug("Loading demos...")
2941

@@ -39,6 +51,9 @@ def get_random_demo(demos):
3951
"""
4052
Generator that gets a random demo. It makes sure all demos have been
4153
provided before it repeats a demo, so it's not truly random.
54+
55+
Args:
56+
demos (dict): Dictionary with the demos.
4257
"""
4358

4459
# Filter out demos that can't be shown without input
@@ -51,7 +66,15 @@ def get_random_demo(demos):
5166

5267

5368
def get_demo_from_user(system_queue, demos):
54-
"""Receives input from user and returns the selected demo."""
69+
"""Receives input from user and returns the selected demo.
70+
71+
Args:
72+
system_queue (Queue): Queue to receive input from the user.
73+
demos (dict): Dictionary with the demos.
74+
75+
Returns:
76+
class: The demo class selected by the user.
77+
"""
5578

5679
try:
5780
demo_name = system_queue.get(timeout=0.01)
@@ -81,6 +104,13 @@ def play_demo_from_user(
81104
"""
82105
Plays a demo, monitoring user input. If no input is detected then the demo
83106
is stopped. If a demo is switched by the user, then this function exits.
107+
108+
Args:
109+
demo (class): The demo class to play.
110+
handle_input (function): Function to handle input.
111+
queues (dict): Dictionary with the queues.
112+
screen (object): The screen object.
113+
user_input_timeout (int): Time to wait for user input.
84114
"""
85115

86116
frame_tick = screen.create_tick(demo.frame_rate)
@@ -113,6 +143,13 @@ def play_demo_from_user(
113143
def play_demo_from_idle(demo, handle_input, queues, screen, demo_time_override):
114144
"""
115145
Plays a demo until time expires or data comes into the system queue.
146+
147+
Args:
148+
demo (class): The demo class to play.
149+
handle_input (function): Function to handle input.
150+
queues (dict): Dictionary with the queues.
151+
screen (object): The screen object.
152+
demo_time_override (int): Time to play the demo.
116153
"""
117154

118155
frame_tick = screen.create_tick(demo.frame_rate)
@@ -152,7 +189,14 @@ def play_demo_from_idle(demo, handle_input, queues, screen, demo_time_override):
152189

153190

154191
def run_loop(screen, user_input_timeout=300, demo_time_override=None):
155-
"""Runs the event loop that takes care of input and running the demos."""
192+
"""Runs the event loop that takes care of input and running the demos.
193+
194+
Args:
195+
screen (object): The screen object.
196+
user_input_timeout (int): Time to wait for user input.
197+
demo_time_override (int): Time to play the demo.
198+
199+
"""
156200

157201
queues = utils.Queues()
158202

@@ -208,7 +252,13 @@ def run_loop(screen, user_input_timeout=300, demo_time_override=None):
208252

209253

210254
def run(simulate, testing=False):
211-
"""Runs the kiosk"""
255+
"""Runs the kiosk
256+
257+
Args:
258+
simulate (bool): If True, the kiosk will run in simulation mode.
259+
testing (bool): If True, the kiosk will run in testing mode.
260+
261+
"""
212262

213263
if simulate:
214264
from display.virtual_screen import ( # pylint: disable=import-outside-toplevel

runners/simulator.py

+37
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,16 @@
1313

1414

1515
class Simulator:
16+
"""This is the simulator class that runs the demos."""
17+
1618
def __init__(self, width, height, demo_dir="demos"):
19+
"""Constructor
20+
21+
Args:
22+
width (int): Width of the screen.
23+
height (int): Height of the screen.
24+
demo_dir (str): Directory where the demos are located.
25+
"""
1726
self.width = width
1827
self.height = height
1928
self.demo_dir = demo_dir
@@ -55,17 +64,34 @@ def __init__(self, width, height, demo_dir="demos"):
5564

5665
@staticmethod
5766
def _import_module(module):
67+
"""Import the module for the first time.
68+
69+
Args:
70+
module (str): Name of the module to import.
71+
72+
Returns:
73+
module: The imported module.
74+
"""
5875
# First time loading of the demo module
5976
logger.info(f"Loading {module}")
6077
return import_module(module)
6178

6279
@staticmethod
6380
def _reload_module(module):
81+
"""Reload the module.
82+
83+
Args:
84+
module (module): The module to reload.
85+
86+
Returns:
87+
module: The reloaded module.
88+
"""
6489
# Hot reload the demo module
6590
logger.info(f"Reloading {module}")
6691
return reload(module)
6792

6893
def _reload_demos(self):
94+
"""Reload all the demos in the demo folder."""
6995
# Hot load all the demos in the demo folder
7096
logger.info("Loading demos...")
7197
demos = utils.get_demos(self.demo_dir)
@@ -81,6 +107,7 @@ def _reload_demos(self):
81107
self._repopulate_demo_list()
82108

83109
def _repopulate_demo_list(self):
110+
"""Repopulate the demo list."""
84111
# if there are more than 13 demos break them up into different pages
85112
self.demo_lst.clear()
86113
# create lists to display demos better
@@ -91,6 +118,11 @@ def _repopulate_demo_list(self):
91118
self.demo_list_index = 0
92119

93120
def _load_game(self, game_name="template"):
121+
"""Load the game.
122+
123+
Args:
124+
game_name (str): Name of the game to load.
125+
"""
94126
self.lives_text = self.text_font.render(
95127
"LIVES: " + self.lives_prev, False, (0, 0, 0), (0, 0, 0)
96128
)
@@ -110,6 +142,7 @@ def _load_game(self, game_name="template"):
110142
self.screen.clear()
111143

112144
def repopulate(self):
145+
"""Repopulate the screen."""
113146
# Hot load demos and populate selection buttons on the screen
114147
self._reload_demos()
115148
# for i in range(1, (self.height - 50) // 50):
@@ -169,6 +202,7 @@ def repopulate(self):
169202
)
170203

171204
def _update_page_count(self):
205+
"""Update the page count."""
172206
# update which demo buttons are displayed
173207
self.demo_list_index += 1
174208
if self.demo_list_index >= len(self.demo_lst):
@@ -206,6 +240,7 @@ def _update_page_count(self):
206240
)
207241

208242
def _generate_buttons(self):
243+
"""Generate the buttons for the screen."""
209244
self.buttons = [
210245
Button(
211246
# Mandatory Parameters
@@ -271,6 +306,7 @@ def _generate_buttons(self):
271306
)
272307

273308
def start(self):
309+
"""Start the main loop."""
274310
handle_input = controllers.start_inputs(self.system_q, self.input_q)
275311
tick = self.screen.create_tick(self.game.frame_rate)
276312

@@ -325,6 +361,7 @@ def start(self):
325361

326362

327363
def run():
364+
"""Run the simulator."""
328365
sim = Simulator(25 * 48 + 150, 30 * 24 + 30, "demos")
329366
sim.start()
330367

runners/test.py

+7
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ def test_demo(demo_name, demo_module_name):
1212
"""
1313
Given a demo name and module, it runs the demo for a couple of ticks to
1414
make sure it doesn't crash.
15+
16+
Args:
17+
demo_name (str): Name of the demo to test.
18+
demo_module_name (str): Name of the module where the demo is located.
19+
20+
Returns:
21+
bool: Whether the demo passed or not.
1522
"""
1623
logger.info(f"Testing {demo_name}...")
1724
display = MagicMock()

runners/utils.py

+12
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ def get_demos(demo_dir="demos"):
1717
Given a directory, it finds all valid demos. It returns a list of tuples
1818
where the first value is the demo name and the second value is the module
1919
string of the demo.
20+
21+
Args:
22+
demo_dir (str): Directory where the demos are located.
23+
24+
Returns:
25+
list: List of tuples with the demo name and the module string.
2026
"""
2127
demo_path = Path(demo_dir)
2228

@@ -35,6 +41,12 @@ def get_demos(demo_dir="demos"):
3541
def get_demo_cls(demo_module):
3642
"""
3743
For a given demo module, it gets the demo class.
44+
45+
Args:
46+
demo_module (module): Module that contains the demo.
47+
48+
Returns:
49+
class: The demo class.
3850
"""
3951
demo_name = demo_module.__name__.rsplit(".", 2)[-2]
4052

0 commit comments

Comments
 (0)