Debian Codegraph @

tests/apps/draw.py

raw symbols SCIP
1 from dataclasses import KW_ONLY, dataclass # pyright: ignore[reportAttributeAccessIssue]
2 from pathlib import Path
3 from typing import Annotated, Literal, NamedTuple
4
5 import cyclopts
6 from cyclopts import App, Parameter
7 from cyclopts.types import UInt8
8
9 toml_path = Path("draw.toml")
10 toml_data = """\
11 [tool.draw]
12 units = "meters"
13 no_exit_on_error = false
14
15 [tool.draw.line]
16 units = "feet"
17 """
18 toml_path.write_text(toml_data)
19
20 app = App(
21 help="Demo drawing app.",
22 config=(
23 cyclopts.config.Toml(toml_path, root_keys=("tool", "draw")),
24 cyclopts.config.Toml(toml_path, root_keys=("tool", "draw"), use_commands_as_keys=False),
25 ),
26 )
27
28
29 class Coordinate(NamedTuple):
30 x: float
31 "X coordinate."
32
33 y: float
34 "Y coordinate."
35
36
37 @Parameter(name="*")
38 @dataclass
39 class Config:
40 _: KW_ONLY
41 units: Literal["meters", "feet"] = "meters"
42 "Drawing units."
43
44 color: tuple[UInt8, UInt8, UInt8] = (0x00, 0x00, 0x00)
45 "RGB uint8 triple."
46
47
48 @app.command
49 def line(
50 start: Coordinate,
51 end: Coordinate,
52 *,
53 config: Config | None = None,
54 ):
55 """Draw a line.
56
57 Parameters
58 ----------
59 start: Coordinate
60 Start of line.
61 end: Coordinate
62 End of line.
63 """
64 if config is None:
65 config = Config()
66
67 print(f"Drawing a line with from {start} to {end} {config.units} in {config.color=}.")
68
69
70 @app.command
71 def elliptic_curve(
72 start_point: Coordinate,
73 end_point: Coordinate,
74 r1: float,
75 r2: float,
76 *,
77 config: Config | None = None,
78 ):
79 """Draw a elliptical curve."""
80
81
82 @app.command
83 def circle(
84 center: Coordinate,
85 radius: Literal["unit"] | float,
86 *,
87 config: Config | None = None,
88 ):
89 """Draw a circle.
90
91 Parameters
92 ----------
93 center: Literal["origin"] | Coordinate
94 Center of the circle to be drawn.
95 center.x: float
96 Circle center's X position.
97 center.y: float
98 Circle center's Y position.
99 radius: float
100 Radius of the circle.
101 """
102 if config is None:
103 config = Config()
104
105 if radius == "unit":
106 radius = 1.0
107 print(f"Drawing a circle with {radius=} {config.units} at {center=}")
108
109
110 @app.command
111 def polygon(*vertices: Annotated[Coordinate, Parameter(required=True)], config: Config | None = None):
112 """Draw a polygon.
113
114 Parameters
115 ----------
116 vertices: Coordinate
117 List of (x, y) coordinates that make up the polygon.
118 """
119 if config is None:
120 config = Config()
121 print(f"Drawing a polygon with {vertices=} {config.units} in {config.color=}.")
122
123
124 @app.command
125 def polygon2(vertices: list[Coordinate], /, *, config: Config | None = None):
126 """Draw a polygon (alternative implementation).
127
128 Parameters
129 ----------
130 vertices: Coordinate
131 List of (x, y) coordinates that make up the polygon.
132 """
133 if config is None:
134 config = Config()
135 print(f"Drawing a polygon with {vertices=} {config.units} in {config.color=}.")
136
137
138 @app.meta.default
139 def meta(
140 *tokens: Annotated[str, Parameter(show=False, allow_leading_hyphen=True)],
141 no_exit_on_error: Annotated[bool, Parameter(negative=())] = False,
142 ):
143 """
144 Parameters
145 ----------
146 exit_on_error: bool
147 Exit on error.
148 """
149 app(tokens, exit_on_error=not no_exit_on_error, print_error=not no_exit_on_error)
150 toml_path.unlink()
151
152
153 if __name__ == "__main__":
154 app.meta(print_error=False, exit_on_error=False)