Skip to content
This repository was archived by the owner on Oct 23, 2023. It is now read-only.

Commit e9d20dc

Browse files
committed
Added some more documentation
1 parent c004fb5 commit e9d20dc

File tree

11 files changed

+193
-49
lines changed

11 files changed

+193
-49
lines changed

python/args.py

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,49 @@
44
from enum import Enum
55

66
try:
7+
# this files comes from the build system
78
from task_maker.version import TASK_MAKER_VERSION
89
except ImportError:
910
TASK_MAKER_VERSION = "unknown"
1011

1112

1213
class CacheMode(Enum):
13-
ALL = 0
14-
REEVALUATE = 1
15-
NOTHING = 2
14+
"""
15+
The mode on how the cache should be used
16+
"""
17+
ALL = 0 # cache everything
18+
REEVALUATE = 1 # do not cache the evaluations
19+
NOTHING = 2 # do not cache anything
1620

1721

1822
class UIS(Enum):
19-
CURSES = 0
20-
PRINT = 1
21-
SILENT = 2
23+
"""
24+
Kind of UI to use
25+
"""
26+
CURSES = 0 # animated terminal curses interface
27+
PRINT = 1 # print to stdout the information as soon as they arrive
28+
SILENT = 2 # do not print
2229

2330

2431
class TaskFormat(Enum):
25-
IOI = 0
26-
TERRY = 1
27-
TM = 2
32+
"""
33+
Format of the task
34+
"""
35+
IOI = 0 # IOI-like task
36+
TERRY = 1 # Terry-like task
37+
TM = 2 # Task-maker format
2838

2939

3040
class Arch(Enum):
31-
DEFAULT = 0
32-
X86_64 = 1
33-
I686 = 2
41+
"""
42+
Architecture to target the executables
43+
"""
44+
DEFAULT = 0 # use the default one
45+
X86_64 = 1 # force x86-64 64-bit executables
46+
I686 = 2 # force i686 32-bit executables
3447

3548

49+
# patch the classes for argparse
3650
for cls in [UIS, CacheMode, TaskFormat, Arch]:
3751

3852
def from_string(cls, name: str):

python/config.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,19 @@ def __init__(self):
9999
self.bulk_total = None # type: Optional[int]
100100

101101
def apply_args(self, args):
102+
"""
103+
Apply all the options from Config.OPTIONS using the argparse's result
104+
"""
102105
for group, options in Config.OPTIONS.items():
103106
for arg in options:
104107
self._apply_arg(arg, args)
105108
self._get_host_port()
106109
self._resolve_dir()
107110

108111
def apply_file(self):
112+
"""
113+
Apply the configuration from the ~/.task-maker.toml file
114+
"""
109115
path = os.path.join(os.path.expanduser("~"), ".task-maker.toml")
110116
try:
111117
with open(path, "r") as f:

python/contest_maker.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
#!/usr/bin/env python3
22

3-
import ruamel.yaml
4-
from typing import List
5-
import os
63
import sys
74

8-
from task_maker.uis.bulk_finish_ui import BulkFinishUI
5+
import os
6+
import ruamel.yaml
97
from task_maker.args import get_parser, UIS
108
from task_maker.config import Config
119
from task_maker.task_maker import setup, run
10+
from task_maker.uis.bulk_finish_ui import BulkFinishUI
11+
from typing import List
1212

1313

1414
def get_task_paths(config: Config) -> List[str]:
15+
"""
16+
Get the paths of the tasks that have to be executed
17+
"""
1518
if config.contest_yaml:
1619
yaml_dir = os.path.dirname(config.contest_yaml)
1720
with open(config.contest_yaml) as f:
@@ -21,6 +24,9 @@ def get_task_paths(config: Config) -> List[str]:
2124

2225

2326
def bulk_run(config: Config) -> int:
27+
"""
28+
Run all the tasks using task-maker, will return the exit code to use
29+
"""
2430
tasks = get_task_paths(config)
2531
if not tasks:
2632
raise ValueError("No tasks found")

python/detect_exe.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525

2626

2727
def get_exeflags(filepath: str) -> int:
28+
"""
29+
Detect if a file is an executable, EXEFLAG_NONE if not a recognized
30+
executable, something else if an executable is detected.
31+
"""
2832
with open(filepath, "rb") as file:
2933
buf = b""
3034
buf_len = 0

python/detect_format.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,15 @@
22

33
from os.path import join, isdir, dirname, exists
44
from task_maker.args import TaskFormat
5+
from typing import Tuple, Optional
56

67

7-
def find_task_dir(task_dir: str, max_depth: int, hint: TaskFormat):
8+
def find_task_dir(task_dir: str, max_depth: int,
9+
hint: TaskFormat) -> Tuple[str, Optional[TaskFormat]]:
10+
"""
11+
Find the root directory of the task which should be a parent folder of
12+
task_dir. It must be at most max_depth directories above the current one.
13+
"""
814
if task_dir.endswith("/"):
915
task_dir = task_dir[:-1]
1016
current_format = detect_format(task_dir, hint)
@@ -13,7 +19,11 @@ def find_task_dir(task_dir: str, max_depth: int, hint: TaskFormat):
1319
return find_task_dir(dirname(task_dir), max_depth - 1, hint)
1420

1521

16-
def detect_format(task_dir: str, hint: TaskFormat):
22+
def detect_format(task_dir: str, hint: TaskFormat) -> Optional[TaskFormat]:
23+
"""
24+
Detect the format of the task in task_dir, using hint if it matches the
25+
format of the task.
26+
"""
1727
valid_formats = []
1828
if is_tm_format(task_dir):
1929
valid_formats.append(TaskFormat.TM)
@@ -33,11 +43,17 @@ def detect_format(task_dir: str, hint: TaskFormat):
3343
return None
3444

3545

36-
def is_tm_format(task_dir: str):
46+
def is_tm_format(task_dir: str) -> bool:
47+
"""
48+
Check if the format could be task-maker
49+
"""
3750
return exists(join(task_dir, "gen", "cases.gen"))
3851

3952

4053
def is_ioi_format(task_dir: str):
54+
"""
55+
Check if the format could be IOI-like
56+
"""
4157
if isdir(join(task_dir, "gen")):
4258
if exists(join(task_dir, "gen", "GEN")) and \
4359
not exists(join(task_dir, "gen", "cases.gen")):
@@ -50,6 +66,9 @@ def is_ioi_format(task_dir: str):
5066

5167

5268
def is_terry_format(task_dir: str):
69+
"""
70+
Check if the format could be Terry-like
71+
"""
5372
if isdir(join(task_dir, "managers")):
5473
return True
5574
return False

python/manager.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,18 @@
1313

1414

1515
def get_task_maker_path():
16+
"""
17+
Get the path of the cpp executable
18+
"""
1619
task_maker = os.path.dirname(__file__)
1720
task_maker = os.path.join(task_maker, "bin", "task-maker")
1821
return os.path.abspath(task_maker)
1922

2023

2124
def spawn_backend(type: str, args: List[str], daemonize: bool):
25+
"""
26+
Spawn a backend service, eventually daemonizing it
27+
"""
2228
task_maker = get_task_maker_path()
2329
if daemonize:
2430
args.append("--daemon")
@@ -33,6 +39,9 @@ def spawn_backend(type: str, args: List[str], daemonize: bool):
3339

3440

3541
def spawn_server(config: Config):
42+
"""
43+
Spawn the server, passing its arguments from the config
44+
"""
3645
args = []
3746
if config.server_logfile is not None:
3847
args += ["--logfile", config.server_logfile]
@@ -54,6 +63,9 @@ def spawn_server(config: Config):
5463

5564

5665
def spawn_worker(config: Config):
66+
"""
67+
Spawn the worker, passing its arguments from the config
68+
"""
5769
args = []
5870
if config.worker_logfile is not None:
5971
args += ["--logfile", config.worker_logfile]
@@ -83,6 +95,10 @@ def spawn_worker(config: Config):
8395

8496

8597
def get_frontend(config: Config) -> Frontend:
98+
"""
99+
Run the frontend module connecting to the server and eventually spawning it
100+
if needed.
101+
"""
86102
try:
87103
return Frontend(config.host, config.port)
88104
except:

python/printer.py

Lines changed: 54 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,58 @@
33

44

55
class Printer:
6+
"""
7+
Utility class to print to various types of terminals. This class won't
8+
print anything, to print use one of the subclasses.
9+
"""
10+
611
def text(self, what: str) -> None:
12+
"""
13+
Normal, unformatted string, the newline won't be added automatically
14+
"""
715
pass
816

917
def red(self, what: str, bold: bool = True) -> None:
18+
"""
19+
Print some text in red, optionally also in bold, the newline won't be
20+
added automatically.
21+
"""
1022
pass
1123

1224
def green(self, what: str, bold: bool = True) -> None:
25+
"""
26+
Print some text in green, optionally also in bold, the newline won't be
27+
added automatically.
28+
"""
1329
pass
1430

1531
def blue(self, what: str, bold: bool = True) -> None:
32+
"""
33+
Print some text in blue, optionally also in bold, the newline won't be
34+
added automatically.
35+
"""
1636
pass
1737

1838
def yellow(self, what: str, bold: bool = True) -> None:
39+
"""
40+
Print some text in yellow, optionally also in bold, the newline won't be
41+
added automatically.
42+
"""
1943
pass
2044

2145
def bold(self, what: str, bold: bool = True) -> None:
46+
"""
47+
Print some text in bold, the newline won't be added automatically.
48+
"""
2249
pass
2350

2451

2552
class StdoutPrinter(Printer):
53+
"""
54+
Printer that will print to stdout using the escape sequences to make the
55+
colors and so on.
56+
"""
57+
2658
def __init__(self) -> None:
2759
self.bold_fmt = "\033[1m"
2860
self.green_fmt = "\033[32m"
@@ -43,40 +75,47 @@ def text(self, what: str) -> None:
4375

4476
def red(self, what: str, bold: bool = True) -> None:
4577
print(
46-
self.red_fmt + (self.bold_fmt
47-
if bold else "") + what + self.reset_fmt,
78+
self.red_fmt + (self.bold_fmt if bold else "") + what +
79+
self.reset_fmt,
4880
end="")
4981

5082
def green(self, what: str, bold: bool = True) -> None:
5183
print(
52-
self.green_fmt + (self.bold_fmt
53-
if bold else "") + what + self.reset_fmt,
84+
self.green_fmt + (self.bold_fmt if bold else "") + what +
85+
self.reset_fmt,
5486
end="")
5587

5688
def blue(self, what: str, bold: bool = True) -> None:
5789
print(
58-
self.blue_fmt + (self.bold_fmt
59-
if bold else "") + what + self.reset_fmt,
90+
self.blue_fmt + (self.bold_fmt if bold else "") + what +
91+
self.reset_fmt,
6092
end="")
6193

6294
def yellow(self, what: str, bold: bool = True) -> None:
6395
print(
64-
self.yellow_fmt + (self.bold_fmt
65-
if bold else "") + what + self.reset_fmt,
96+
self.yellow_fmt + (self.bold_fmt if bold else "") + what +
97+
self.reset_fmt,
6698
end="")
6799

68100
def bold(self, what: str, bold: bool = True) -> None:
69101
print(self.bold_fmt + what + self.reset_fmt, end="")
70102

71103
def right(self, what: str) -> None:
104+
"""
105+
Print the text aligned to the right of the screen
106+
"""
72107
print(self.right_fmt + self.left_fmt(len(what) - 1) + what)
73108

74109

75110
class CursesPrinter(Printer):
111+
"""
112+
Printer that will use the curses API in a curses Window.
113+
"""
114+
76115
def __init__(self, stdscr: 'curses._CursesWindow') -> None:
77116
self.stdscr = stdscr
78117
self.bold_fmt = curses.A_BOLD
79-
if curses.COLORS >= 256:
118+
if hasattr(curses, "COLORS") and curses.COLORS >= 256:
80119
self.green_fmt = curses.color_pair(82)
81120
else:
82121
self.green_fmt = curses.color_pair(curses.COLOR_GREEN)
@@ -91,16 +130,16 @@ def red(self, what: str, bold: bool = True) -> None:
91130
self.stdscr.addstr(what, self.red_fmt | (self.bold_fmt if bold else 0))
92131

93132
def green(self, what: str, bold: bool = True) -> None:
94-
self.stdscr.addstr(what, self.green_fmt | (self.bold_fmt
95-
if bold else 0))
133+
self.stdscr.addstr(what,
134+
self.green_fmt | (self.bold_fmt if bold else 0))
96135

97136
def blue(self, what: str, bold: bool = True) -> None:
98-
self.stdscr.addstr(what, self.blue_fmt | (self.bold_fmt
99-
if bold else 0))
137+
self.stdscr.addstr(what,
138+
self.blue_fmt | (self.bold_fmt if bold else 0))
100139

101140
def yellow(self, what: str, bold: bool = True) -> None:
102-
self.stdscr.addstr(what, self.yellow_fmt | (self.bold_fmt
103-
if bold else 0))
141+
self.stdscr.addstr(what,
142+
self.yellow_fmt | (self.bold_fmt if bold else 0))
104143

105144
def bold(self, what: str, bold: bool = True) -> None:
106145
self.stdscr.addstr(what, self.bold_fmt)

0 commit comments

Comments
 (0)