Skip to content

Commit 1ec3660

Browse files
authored
Move leads_gui.Config to leads_vec.Config to Deallocate Resources (#346)
1 parent b2cc864 commit 1ec3660

14 files changed

+77
-59
lines changed

README.md

+24-34
Original file line numberDiff line numberDiff line change
@@ -329,17 +329,6 @@ leads-vec -r reverse_proxy run
329329

330330
This will start the corresponding reverse proxy program as a subprocess in the background.
331331

332-
##### Specify a Theme
333-
334-
```shell
335-
leads-vec -t path/to/the/theme.json run
336-
```
337-
338-
> You can use ":INTERNAL" to replace the path to `leads_vec`. For example, instead of typing
339-
".../site-packages/leads_vec/devices_jarvis.py", simply use ":INTERNAL/devices_jarvis.py".
340-
341-
To learn about themes, read [Color and Themes](https://customtkinter.tomschimansky.com/documentation/color).
342-
343332
##### Magnify Font Sizes
344333

345334
```shell
@@ -483,29 +472,30 @@ following as all the settings are optional.
483472

484473
Note that a purely empty file could cause an error.
485474

486-
| Index | Type | Usage | Used By | Default |
487-
|------------------------|---------|-----------------------------------------------------------|--------------|---------------|
488-
| `w_debug_level` | `str` | `"DEBUG"`, `"INFO"`, `"WARN"`, `"ERROR"` | Main, Remote | `"DEBUG"` |
489-
| `data_seq_size` | `int` | Buffer size of history data | Main | `100` |
490-
| `width` | `int` | Window width | Main | `720` |
491-
| `height` | `int` | Window height | Main | `480` |
492-
| `fullscreen` | `bool` | `True`: auto maximize; `False`: window mode | Main | `False` |
493-
| `no_title_bar` | `bool` | `True`: no title bar; `False`: default title bar | Main | `False` |
494-
| `theme_mode` | `bool` | `"system"`, `"light"`, `"dark`" | Main | `False` |
495-
| `manual_mode` | `bool` | `True`: hide control system; `False`: show control system | Main | `False` |
496-
| `refresh_rate` | `int` | GUI frame per second | Main | `30` |
497-
| `m_ratio` | `float` | Meter widget size ratio | Main | `0.7` |
498-
| `num_external_screens` | `int` | The number of external screens used if possible | Main | `0` |
499-
| `font_size_small` | `int` | Small font size | Main | `14` |
500-
| `font_size_medium` | `int` | Medium font size | Main | `28` |
501-
| `font_size_large` | `int` | Large font size | Main | `42` |
502-
| `font_size_x_large` | `int` | Extra large font size | Main | `56` |
503-
| `comm_addr` | `str` | Communication server address | Remote | `"127.0.0.1"` |
504-
| `comm_port` | `int` | The port on which the communication system runs on | Main, Remote | `16900` |
505-
| `comm_stream` | `bool` | `True`: enable streaming; `False`: disable streaming | Main | `False` |
506-
| `comm_stream_port` | `bool` | The port on which the streaming system runs on | Main, Remote | `16901` |
507-
| `data_dir` | `str` | The directory for the data recording system | Remote | `"data"` |
508-
| `save_data` | `bool` | `True`: save data; `False`: discard data | Remote | `False` |
475+
| Index | Type | Usage | Used By | Default |
476+
|------------------------|---------|---------------------------------------------------------------------------------------|--------------|---------------|
477+
| `w_debug_level` | `str` | `"DEBUG"`, `"INFO"`, `"WARN"`, `"ERROR"` | Main, Remote | `"DEBUG"` |
478+
| `data_seq_size` | `int` | Buffer size of history data | Main | `100` |
479+
| `width` | `int` | Window width | Main | `720` |
480+
| `height` | `int` | Window height | Main | `480` |
481+
| `fullscreen` | `bool` | `True`: auto maximize; `False`: window mode | Main | `False` |
482+
| `no_title_bar` | `bool` | `True`: no title bar; `False`: default title bar | Main | `False` |
483+
| `theme` | `str` | Path to the [theme](https://customtkinter.tomschimansky.com/documentation/color) file | Main | `""` |
484+
| `theme_mode` | `bool` | `"system"`, `"light"`, `"dark`" | Main | `False` |
485+
| `manual_mode` | `bool` | `True`: hide control system; `False`: show control system | Main | `False` |
486+
| `refresh_rate` | `int` | GUI frame rate | Main | `30` |
487+
| `m_ratio` | `float` | Meter widget size ratio | Main | `0.7` |
488+
| `num_external_screens` | `int` | Number of external screens used if possible | Main | `0` |
489+
| `font_size_small` | `int` | Small font size | Main | `14` |
490+
| `font_size_medium` | `int` | Medium font size | Main | `28` |
491+
| `font_size_large` | `int` | Large font size | Main | `42` |
492+
| `font_size_x_large` | `int` | Extra large font size | Main | `56` |
493+
| `comm_addr` | `str` | Communication server address | Remote | `"127.0.0.1"` |
494+
| `comm_port` | `int` | Port on which the communication system runs on | Main, Remote | `16900` |
495+
| `comm_stream` | `bool` | `True`: enable streaming; `False`: disable streaming | Main | `False` |
496+
| `comm_stream_port` | `bool` | Port on which the streaming system runs on | Main, Remote | `16901` |
497+
| `data_dir` | `str` | Directory for the data recording system | Remote | `"data"` |
498+
| `save_data` | `bool` | `True`: save data; `False`: discard data | Remote | `False` |
509499

510500
## Devices Module
511501

leads_gui/__init__.py

+15-2
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@
1111
from typing import Callable as _Callable, Any as _Any
1212
from customtkinter import set_default_color_theme as _set_default_color_theme
1313

14-
from leads import LEADS as _LEADS, Controller as _Controller
15-
from leads_gui.prototype import *
14+
from leads import LEADS as _LEADS, Controller as _Controller, set_on_register_config as _set_on_register_config
15+
from leads.types import OnRegister as _OnRegister
1616
from leads_gui.config import *
17+
from leads_gui.prototype import *
1718
from leads_gui.icons import *
1819
from leads_gui.accelerometer import *
1920
from leads_gui.speedometer import *
@@ -25,6 +26,18 @@
2526
_set_default_color_theme(f"{_abspath(__file__)[:-11]}assets/leads-theme.json")
2627

2728

29+
def _on_register_config(chain: _OnRegister[Config]) -> _OnRegister[Config]:
30+
def _(cfg: Config) -> None:
31+
chain(cfg)
32+
if cfg.theme:
33+
_set_default_color_theme(cfg.theme)
34+
35+
return _
36+
37+
38+
_set_on_register_config(_on_register_config)
39+
40+
2841
def initialize(window: Window,
2942
render: _Callable[[ContextManager], None],
3043
leads: _LEADS[_Any],

leads_gui/config.py

+1-6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ def __init__(self, base: dict[str, _Any]) -> None:
99
self.height: int = 480
1010
self.fullscreen: bool = False
1111
self.no_title_bar: bool = False
12+
self.theme: str = ""
1213
self.theme_mode: _Literal["system", "light", "dark"] = "system"
1314
self.manual_mode: bool = False
1415
self.refresh_rate: int = 30
@@ -18,12 +19,6 @@ def __init__(self, base: dict[str, _Any]) -> None:
1819
self.font_size_medium: int = 28
1920
self.font_size_large: int = 42
2021
self.font_size_x_large: int = 56
21-
self.comm_addr: str = "127.0.0.1"
22-
self.comm_port: int = 16900
23-
self.comm_stream: bool = False
24-
self.comm_stream_port: int = 16901
25-
self.data_dir: str = "data"
26-
self.save_data: bool = False
2722
super().__init__(base)
2823

2924
def magnify_font_sizes(self, factor: float) -> None:

leads_vec/__entry__.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ def __entry__() -> None:
2828
parser.add_argument("-m", "--main", default=f"{MODULE_PATH}/cli.py", help="specify a main module")
2929
parser.add_argument("-r", "--register", choices=("systemd", "config", "reverse_proxy"), default=None,
3030
help="register a service")
31-
parser.add_argument("-t", "--theme", default=None, help="specify a theme")
3231
parser.add_argument("-mfs", "--magnify-font-sizes", type=float, default=1, help="magnify font sizes by a factor")
3332
parser.add_argument("--emu", action=_BooleanOptionalAction, default=False, help="use emulator")
3433
parser.add_argument("--auto-mfs", action=_BooleanOptionalAction, default=False,
@@ -60,5 +59,5 @@ def __entry__() -> None:
6059
args.emu = False
6160
_L.debug("Replay mode enabled")
6261
_exit(run(parse_path(args.config), parse_path(args.devices), parse_path(args.main), args.register,
63-
parse_path(args.theme), args.magnify_font_sizes, args.emu, args.auto_mfs, args.ignore_import_error))
62+
args.magnify_font_sizes, args.emu, args.auto_mfs, args.ignore_import_error))
6463
_exit()

leads_vec/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55

66
from leads_vec.run import *
77
from leads_vec.__entry__ import __entry__
8+
from leads_vec.config import *

leads_vec/_bootloader/systemd.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
from subprocess import run as _run
44

55
from leads import L as _L
6-
from leads_gui import Config as _Config
76
from leads_gui.system import get_system_kernel as _get_system_kernel
7+
from leads_vec.config import Config
88

99

1010
def register_leads_vec() -> None:
@@ -15,7 +15,7 @@ def register_leads_vec() -> None:
1515
if not _exists("/usr/local/leads"):
1616
_mkdirs("/usr/local/leads")
1717
with open("/usr/local/leads/config.json", "w") as f:
18-
f.write(str(_Config({})))
18+
f.write(str(Config({})))
1919
_chmod("/usr/local/leads/config.json", 0x644)
2020
_chmod(script := f"{_abspath(__file__)[:-10]}leads-vec.service.sh", 0o755)
2121
if not _exists(user_systemd := f"/home/{(username := _get_login())}/.config/systemd/user"):

leads_vec/config.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from typing import Any as _Any
2+
3+
from leads_gui import Config as _Config
4+
5+
6+
class Config(_Config):
7+
def __init__(self, base: dict[str, _Any]) -> None:
8+
self.comm_port: int = 16900
9+
self.comm_stream: bool = False
10+
self.comm_stream_port: int = 16901
11+
super().__init__(base)

leads_vec/devices.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
SFT, read_device_marker, has_controller
88
from leads_arduino import ArduinoMicro, WheelSpeedSensor, VoltageSensor
99
from leads_gpio import NMEAGPSReceiver, LEDGroup, LED, LEDGroupCommand, LEDCommand, Entire, Transition
10-
from leads_gui import Config
10+
from leads_vec.config import Config
1111
from leads_video import Base64Camera, get_camera
1212

1313
config: Config = require_config()

leads_vec/devices_jarvis.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from leads import device, MAIN_CONTROLLER, mark_device, FRONT_VIEW_CAMERA, LEFT_VIEW_CAMERA, RIGHT_VIEW_CAMERA, \
44
REAR_VIEW_CAMERA, require_config
5-
from leads_gui import Config
5+
from leads_vec.config import Config
66
from leads_video import LowLatencyBase64Camera
77

88
import_error: ImportError | None = None

leads_vec/run.py

+4-8
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,13 @@
33
from os.path import exists as _exists
44
from typing import Literal as _Literal
55

6-
from customtkinter import set_default_color_theme as _set_default_color_theme
7-
86
from leads import register_controller as _register_controller, MAIN_CONTROLLER as _MAIN_CONTROLLER, \
97
L as _L, load_config as _load_config, register_config as _register_config, release as _release
10-
from leads_gui import Config as _Config
8+
from leads_vec.config import Config
119

1210

1311
def run(config: str | None, devices: str, main: str, register: _Literal["systemd", "config", "reverse_proxy"] | None,
14-
theme: str | None, magnify_font_sizes: float, emu: bool, auto_mfs: bool, ignore_import_error: bool) -> int:
12+
magnify_font_sizes: float, emu: bool, auto_mfs: bool, ignore_import_error: bool) -> int:
1513
match register:
1614
case "systemd":
1715
from ._bootloader import register_leads_vec as _create_service
@@ -26,17 +24,15 @@ def run(config: str | None, devices: str, main: str, register: _Literal["systemd
2624
_L.error("Aborted")
2725
return 1
2826
with open("config.json", "w") as f:
29-
f.write(str(_Config({})))
27+
f.write(str(Config({})))
3028
_L.debug("Configuration file saved to \"config.json\"")
3129
case "reverse_proxy":
3230
from ._bootloader import start_frpc as _start_frpc
3331

3432
_start_frpc()
3533
_L.debug("`frpc` started")
36-
config = _load_config(config, _Config) if config else _Config({})
34+
config = _load_config(config, Config) if config else Config({})
3735
_L.debug("Configuration loaded:", str(config))
38-
if t := theme:
39-
_set_default_color_theme(t)
4036
if (f := magnify_font_sizes) != 1:
4137
config.magnify_font_sizes(f)
4238
if auto_mfs:

leads_vec_rc/__entry__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from uvicorn import run as _run
44

55
from leads import register_config as _register_config, load_config as _load_config
6-
from leads_gui import Config as _Config
6+
from leads_vec_rc.config import Config
77

88

99
def __entry__() -> None:
@@ -13,7 +13,7 @@ def __entry__() -> None:
1313
parser.add_argument("-c", "--config", default=None, help="specify a configuration file")
1414
parser.add_argument("-p", "--port", type=int, default=8000, help="specify a server port")
1515
args = parser.parse_args()
16-
_register_config(_load_config(args.config, _Config) if args.config else _Config({}))
16+
_register_config(_load_config(args.config, Config) if args.config else Config({}))
1717
from leads_vec_rc.cli import app
1818

1919
_run(app, host="0.0.0.0", port=args.port, log_level="warning")

leads_vec_rc/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
raise ImportError("Please install `uvicorn` to run this module\n>>>pip install \"fastapi[standard]\"")
77

88
from leads_vec_rc.__entry__ import __entry__
9+
from leads_vec_rc.config import *

leads_vec_rc/cli.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from leads import require_config, L, DataContainer
1313
from leads.comm import Service, Client, start_client, create_client, Callback, Connection, ConnectionBase
1414
from leads.data_persistence import DataPersistence, Vector, CSV, DEFAULT_HEADER_FULL, VISUAL_HEADER_FULL
15-
from leads_gui import Config
15+
from leads_vec_rc.config import Config
1616

1717
config: Config = require_config()
1818
if not exists(config.data_dir):

leads_vec_rc/config.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from typing import Any as _Any
2+
3+
from leads import ConfigTemplate as _ConfigTemplate
4+
5+
6+
class Config(_ConfigTemplate):
7+
def __init__(self, base: dict[str, _Any]) -> None:
8+
self.comm_addr: str = "127.0.0.1"
9+
self.comm_port: int = 16900
10+
self.data_dir: str = "data"
11+
self.save_data: bool = False
12+
super().__init__(base)

0 commit comments

Comments
 (0)