Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move leads_gui.Config to leads_vec.Config to Deallocate Resources #346

Merged
merged 2 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 24 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -329,17 +329,6 @@ leads-vec -r reverse_proxy run

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

##### Specify a Theme

```shell
leads-vec -t path/to/the/theme.json run
```

> You can use ":INTERNAL" to replace the path to `leads_vec`. For example, instead of typing
".../site-packages/leads_vec/devices_jarvis.py", simply use ":INTERNAL/devices_jarvis.py".

To learn about themes, read [Color and Themes](https://customtkinter.tomschimansky.com/documentation/color).

##### Magnify Font Sizes

```shell
Expand Down Expand Up @@ -483,29 +472,30 @@ following as all the settings are optional.

Note that a purely empty file could cause an error.

| Index | Type | Usage | Used By | Default |
|------------------------|---------|-----------------------------------------------------------|--------------|---------------|
| `w_debug_level` | `str` | `"DEBUG"`, `"INFO"`, `"WARN"`, `"ERROR"` | Main, Remote | `"DEBUG"` |
| `data_seq_size` | `int` | Buffer size of history data | Main | `100` |
| `width` | `int` | Window width | Main | `720` |
| `height` | `int` | Window height | Main | `480` |
| `fullscreen` | `bool` | `True`: auto maximize; `False`: window mode | Main | `False` |
| `no_title_bar` | `bool` | `True`: no title bar; `False`: default title bar | Main | `False` |
| `theme_mode` | `bool` | `"system"`, `"light"`, `"dark`" | Main | `False` |
| `manual_mode` | `bool` | `True`: hide control system; `False`: show control system | Main | `False` |
| `refresh_rate` | `int` | GUI frame per second | Main | `30` |
| `m_ratio` | `float` | Meter widget size ratio | Main | `0.7` |
| `num_external_screens` | `int` | The number of external screens used if possible | Main | `0` |
| `font_size_small` | `int` | Small font size | Main | `14` |
| `font_size_medium` | `int` | Medium font size | Main | `28` |
| `font_size_large` | `int` | Large font size | Main | `42` |
| `font_size_x_large` | `int` | Extra large font size | Main | `56` |
| `comm_addr` | `str` | Communication server address | Remote | `"127.0.0.1"` |
| `comm_port` | `int` | The port on which the communication system runs on | Main, Remote | `16900` |
| `comm_stream` | `bool` | `True`: enable streaming; `False`: disable streaming | Main | `False` |
| `comm_stream_port` | `bool` | The port on which the streaming system runs on | Main, Remote | `16901` |
| `data_dir` | `str` | The directory for the data recording system | Remote | `"data"` |
| `save_data` | `bool` | `True`: save data; `False`: discard data | Remote | `False` |
| Index | Type | Usage | Used By | Default |
|------------------------|---------|---------------------------------------------------------------------------------------|--------------|---------------|
| `w_debug_level` | `str` | `"DEBUG"`, `"INFO"`, `"WARN"`, `"ERROR"` | Main, Remote | `"DEBUG"` |
| `data_seq_size` | `int` | Buffer size of history data | Main | `100` |
| `width` | `int` | Window width | Main | `720` |
| `height` | `int` | Window height | Main | `480` |
| `fullscreen` | `bool` | `True`: auto maximize; `False`: window mode | Main | `False` |
| `no_title_bar` | `bool` | `True`: no title bar; `False`: default title bar | Main | `False` |
| `theme` | `str` | Path to the [theme](https://customtkinter.tomschimansky.com/documentation/color) file | Main | `""` |
| `theme_mode` | `bool` | `"system"`, `"light"`, `"dark`" | Main | `False` |
| `manual_mode` | `bool` | `True`: hide control system; `False`: show control system | Main | `False` |
| `refresh_rate` | `int` | GUI frame rate | Main | `30` |
| `m_ratio` | `float` | Meter widget size ratio | Main | `0.7` |
| `num_external_screens` | `int` | Number of external screens used if possible | Main | `0` |
| `font_size_small` | `int` | Small font size | Main | `14` |
| `font_size_medium` | `int` | Medium font size | Main | `28` |
| `font_size_large` | `int` | Large font size | Main | `42` |
| `font_size_x_large` | `int` | Extra large font size | Main | `56` |
| `comm_addr` | `str` | Communication server address | Remote | `"127.0.0.1"` |
| `comm_port` | `int` | Port on which the communication system runs on | Main, Remote | `16900` |
| `comm_stream` | `bool` | `True`: enable streaming; `False`: disable streaming | Main | `False` |
| `comm_stream_port` | `bool` | Port on which the streaming system runs on | Main, Remote | `16901` |
| `data_dir` | `str` | Directory for the data recording system | Remote | `"data"` |
| `save_data` | `bool` | `True`: save data; `False`: discard data | Remote | `False` |

## Devices Module

Expand Down
17 changes: 15 additions & 2 deletions leads_gui/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
from typing import Callable as _Callable, Any as _Any
from customtkinter import set_default_color_theme as _set_default_color_theme

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


def _on_register_config(chain: _OnRegister[Config]) -> _OnRegister[Config]:
def _(cfg: Config) -> None:
chain(cfg)
if cfg.theme:
_set_default_color_theme(cfg.theme)

return _


_set_on_register_config(_on_register_config)


def initialize(window: Window,
render: _Callable[[ContextManager], None],
leads: _LEADS[_Any],
Expand Down
7 changes: 1 addition & 6 deletions leads_gui/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def __init__(self, base: dict[str, _Any]) -> None:
self.height: int = 480
self.fullscreen: bool = False
self.no_title_bar: bool = False
self.theme: str = ""
self.theme_mode: _Literal["system", "light", "dark"] = "system"
self.manual_mode: bool = False
self.refresh_rate: int = 30
Expand All @@ -18,12 +19,6 @@ def __init__(self, base: dict[str, _Any]) -> None:
self.font_size_medium: int = 28
self.font_size_large: int = 42
self.font_size_x_large: int = 56
self.comm_addr: str = "127.0.0.1"
self.comm_port: int = 16900
self.comm_stream: bool = False
self.comm_stream_port: int = 16901
self.data_dir: str = "data"
self.save_data: bool = False
super().__init__(base)

def magnify_font_sizes(self, factor: float) -> None:
Expand Down
3 changes: 1 addition & 2 deletions leads_vec/__entry__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ def __entry__() -> None:
parser.add_argument("-m", "--main", default=f"{MODULE_PATH}/cli.py", help="specify a main module")
parser.add_argument("-r", "--register", choices=("systemd", "config", "reverse_proxy"), default=None,
help="register a service")
parser.add_argument("-t", "--theme", default=None, help="specify a theme")
parser.add_argument("-mfs", "--magnify-font-sizes", type=float, default=1, help="magnify font sizes by a factor")
parser.add_argument("--emu", action=_BooleanOptionalAction, default=False, help="use emulator")
parser.add_argument("--auto-mfs", action=_BooleanOptionalAction, default=False,
Expand Down Expand Up @@ -60,5 +59,5 @@ def __entry__() -> None:
args.emu = False
_L.debug("Replay mode enabled")
_exit(run(parse_path(args.config), parse_path(args.devices), parse_path(args.main), args.register,
parse_path(args.theme), args.magnify_font_sizes, args.emu, args.auto_mfs, args.ignore_import_error))
args.magnify_font_sizes, args.emu, args.auto_mfs, args.ignore_import_error))
_exit()
1 change: 1 addition & 0 deletions leads_vec/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@

from leads_vec.run import *
from leads_vec.__entry__ import __entry__
from leads_vec.config import *
4 changes: 2 additions & 2 deletions leads_vec/_bootloader/systemd.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
from subprocess import run as _run

from leads import L as _L
from leads_gui import Config as _Config
from leads_gui.system import get_system_kernel as _get_system_kernel
from leads_vec.config import Config


def register_leads_vec() -> None:
Expand All @@ -15,7 +15,7 @@ def register_leads_vec() -> None:
if not _exists("/usr/local/leads"):
_mkdirs("/usr/local/leads")
with open("/usr/local/leads/config.json", "w") as f:
f.write(str(_Config({})))
f.write(str(Config({})))
_chmod("/usr/local/leads/config.json", 0x644)
_chmod(script := f"{_abspath(__file__)[:-10]}leads-vec.service.sh", 0o755)
if not _exists(user_systemd := f"/home/{(username := _get_login())}/.config/systemd/user"):
Expand Down
11 changes: 11 additions & 0 deletions leads_vec/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from typing import Any as _Any

from leads_gui import Config as _Config


class Config(_Config):
def __init__(self, base: dict[str, _Any]) -> None:
self.comm_port: int = 16900
self.comm_stream: bool = False
self.comm_stream_port: int = 16901
super().__init__(base)
2 changes: 1 addition & 1 deletion leads_vec/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
SFT, read_device_marker, has_controller
from leads_arduino import ArduinoMicro, WheelSpeedSensor, VoltageSensor
from leads_gpio import NMEAGPSReceiver, LEDGroup, LED, LEDGroupCommand, LEDCommand, Entire, Transition
from leads_gui import Config
from leads_vec.config import Config
from leads_video import Base64Camera, get_camera

config: Config = require_config()
Expand Down
2 changes: 1 addition & 1 deletion leads_vec/devices_jarvis.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from leads import device, MAIN_CONTROLLER, mark_device, FRONT_VIEW_CAMERA, LEFT_VIEW_CAMERA, RIGHT_VIEW_CAMERA, \
REAR_VIEW_CAMERA, require_config
from leads_gui import Config
from leads_vec.config import Config
from leads_video import LowLatencyBase64Camera

import_error: ImportError | None = None
Expand Down
12 changes: 4 additions & 8 deletions leads_vec/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@
from os.path import exists as _exists
from typing import Literal as _Literal

from customtkinter import set_default_color_theme as _set_default_color_theme

from leads import register_controller as _register_controller, MAIN_CONTROLLER as _MAIN_CONTROLLER, \
L as _L, load_config as _load_config, register_config as _register_config, release as _release
from leads_gui import Config as _Config
from leads_vec.config import Config


def run(config: str | None, devices: str, main: str, register: _Literal["systemd", "config", "reverse_proxy"] | None,
theme: str | None, magnify_font_sizes: float, emu: bool, auto_mfs: bool, ignore_import_error: bool) -> int:
magnify_font_sizes: float, emu: bool, auto_mfs: bool, ignore_import_error: bool) -> int:
match register:
case "systemd":
from ._bootloader import register_leads_vec as _create_service
Expand All @@ -26,17 +24,15 @@ def run(config: str | None, devices: str, main: str, register: _Literal["systemd
_L.error("Aborted")
return 1
with open("config.json", "w") as f:
f.write(str(_Config({})))
f.write(str(Config({})))
_L.debug("Configuration file saved to \"config.json\"")
case "reverse_proxy":
from ._bootloader import start_frpc as _start_frpc

_start_frpc()
_L.debug("`frpc` started")
config = _load_config(config, _Config) if config else _Config({})
config = _load_config(config, Config) if config else Config({})
_L.debug("Configuration loaded:", str(config))
if t := theme:
_set_default_color_theme(t)
if (f := magnify_font_sizes) != 1:
config.magnify_font_sizes(f)
if auto_mfs:
Expand Down
4 changes: 2 additions & 2 deletions leads_vec_rc/__entry__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from uvicorn import run as _run

from leads import register_config as _register_config, load_config as _load_config
from leads_gui import Config as _Config
from leads_vec_rc.config import Config


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

_run(app, host="0.0.0.0", port=args.port, log_level="warning")
1 change: 1 addition & 0 deletions leads_vec_rc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
raise ImportError("Please install `uvicorn` to run this module\n>>>pip install \"fastapi[standard]\"")

from leads_vec_rc.__entry__ import __entry__
from leads_vec_rc.config import *
2 changes: 1 addition & 1 deletion leads_vec_rc/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from leads import require_config, L, DataContainer
from leads.comm import Service, Client, start_client, create_client, Callback, Connection, ConnectionBase
from leads.data_persistence import DataPersistence, Vector, CSV, DEFAULT_HEADER_FULL, VISUAL_HEADER_FULL
from leads_gui import Config
from leads_vec_rc.config import Config

config: Config = require_config()
if not exists(config.data_dir):
Expand Down
12 changes: 12 additions & 0 deletions leads_vec_rc/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from typing import Any as _Any

from leads import ConfigTemplate as _ConfigTemplate


class Config(_ConfigTemplate):
def __init__(self, base: dict[str, _Any]) -> None:
self.comm_addr: str = "127.0.0.1"
self.comm_port: int = 16900
self.data_dir: str = "data"
self.save_data: bool = False
super().__init__(base)