Skip to content

Commit 989e283

Browse files
authored
Add Private Credentials to a Private Repository (#166)
* Unified type of `args.magnify_font_sizes`. * Added `run()`. (#162) * Migrated main body to `run()`. (#162) * Normalized imports. (#162)
1 parent bd3f781 commit 989e283

5 files changed

Lines changed: 107 additions & 78 deletions

File tree

leads_vec/__init__.py

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

33
if not _find_spec("pynput"):
44
raise ImportError("Please install `pynput` to run this module\n>>>pip install pynput")
5+
6+
from leads_vec.run import *

leads_vec/__main__.py

Lines changed: 14 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,30 @@
11
from argparse import ArgumentParser as _ArgumentParser, BooleanOptionalAction as _BooleanOptionalAction
22
from importlib.metadata import version as _package_version, PackageNotFoundError as _PackageNotFoundError
3-
from importlib.util import spec_from_file_location as _spec_from_file_location, module_from_spec as _module_from_spec
43
from os import getlogin as _get_login
54
from os.path import abspath as _abspath
6-
from os.path import exists as _exists
75
from sys import exit as _exit, version as _version
86
from warnings import filterwarnings as _filterwarnings
97

10-
from customtkinter import set_default_color_theme as _set_default_color_theme
11-
12-
from leads import register_controller as _register_controller, MAIN_CONTROLLER as _MAIN_CONTROLLER, \
13-
L as _L, load_config as _load_config, register_config as _register_config, reset as _reset
14-
from leads.data_persistence import Dataset as _Dataset
15-
from leads_gui import Config as _Config
8+
from leads import L as _L
169
from leads_gui.system import get_system_kernel as _get_system_kernel
10+
from leads_vec.run import run
1711

1812
if __name__ == "__main__":
1913
_filterwarnings("ignore")
20-
_MODULE_PATH = _abspath(__file__)[:-12]
14+
MODULE_PATH = _abspath(__file__)[:-12]
2115

2216
parser = _ArgumentParser(prog="LEADS VeC",
2317
description="Lightweight Embedded Assisted Driving System VeC",
2418
epilog="ProjectNeura: https://projectneura.org\n"
2519
"GitHub: https://github.com/ProjectNeura/LEADS")
2620
parser.add_argument("action", choices=("info", "replay", "run"))
2721
parser.add_argument("-c", "--config", default=None, help="specify a configuration file")
28-
parser.add_argument("-d", "--devices", default=f"{_MODULE_PATH}/devices.py",
22+
parser.add_argument("-d", "--devices", default=f"{MODULE_PATH}/devices.py",
2923
help="specify a devices module")
3024
parser.add_argument("-r", "--register", choices=("systemd", "config", "reverse_proxy"), default=None,
3125
help="register a service")
3226
parser.add_argument("-t", "--theme", default=None, help="specify a theme")
33-
parser.add_argument("-mfs", "--magnify-font-sizes", type=float, default=None, help="magnify font sizes by a factor")
27+
parser.add_argument("-mfs", "--magnify-font-sizes", type=float, default=1, help="magnify font sizes by a factor")
3428
parser.add_argument("--emu", action=_BooleanOptionalAction, default=False, help="use emulator")
3529
parser.add_argument("--auto-mfs", action=_BooleanOptionalAction, default=False,
3630
help="automatically magnify font sizes to match the original proportion")
@@ -51,68 +45,15 @@
5145
f"Python Version: {_version}",
5246
f"User: {_get_login()}",
5347
f"`frpc` Available: {_frpc_exists()}",
54-
f"Module Path: {_MODULE_PATH}",
48+
f"Module Path: {MODULE_PATH}",
5549
f"LEADS Version: {leads_version}",
5650
f"LEADS VeC Version: {__version__}",
5751
sep="\n")
58-
_exit()
59-
if args.register == "systemd":
60-
from ._bootloader import register_leads_vec as _create_service
61-
62-
_create_service()
63-
_L.info("Service registered")
64-
_L.info(f"Service script is located at \"{_MODULE_PATH}/_bootloader/leads-vec.service.sh\"")
65-
elif args.register == "config":
66-
if _exists("config.json"):
67-
r = input("\"config.json\" already exists. Overwrite? (y/N) >>>").lower()
68-
if r.lower() != "y":
69-
_exit("Error: Aborted")
70-
with open("config.json", "w") as f:
71-
f.write(str(_Config({})))
72-
_L.info("Configuration file saved to \"config.json\"")
73-
elif args.register == "reverse_proxy":
74-
from ._bootloader import start_frpc as _start_frpc
75-
76-
_start_frpc()
77-
_L.info("`frpc` started")
78-
config = _load_config(args.config, _Config) if args.config else _Config({})
79-
_L.debug("Configuration loaded:", str(config))
80-
if t := args.theme:
81-
_set_default_color_theme(t)
82-
if f := args.magnify_font_sizes:
83-
config.magnify_font_sizes(f)
84-
if args.auto_mfs:
85-
config.auto_magnify_font_sizes()
86-
_register_config(config)
87-
from leads_vec.cli import main
88-
89-
if args.action == "replay":
90-
from leads_emulation.replay import ReplayController as _Controller
91-
92-
_register_controller(_MAIN_CONTROLLER, _Controller(_Dataset(f"{config.data_dir}/main.csv")))
93-
_L.info("Replay started")
94-
_exit(main())
95-
96-
try:
97-
if args.emu:
98-
raise SystemError("User specifies to use emulator")
99-
spec = _spec_from_file_location("_", args.devices)
100-
spec.loader.exec_module(_module_from_spec(spec))
101-
except (ImportError, SystemError) as e:
102-
_L.debug(repr(e))
103-
if isinstance(e, ImportError):
104-
if args.ignore_import_error:
105-
_L.debug(f"Ignoring import error: {repr(e)}")
106-
_exit(main())
107-
else:
108-
_L.warn(
109-
f"Specified devices module ({args.devices}) is not available, using emulation module instead...")
110-
_reset()
111-
try:
112-
from leads_emulation import SinController as _Controller
113-
114-
_register_controller(_MAIN_CONTROLLER, _Controller(0, 200, .05))
115-
except ImportError as e:
116-
_L.error(f"Emulator error: {repr(e)}")
117-
_exit(1)
118-
_exit(main())
52+
else:
53+
if args.action == "replay":
54+
args.devices = f"{MODULE_PATH}/replay.py"
55+
args.emu = False
56+
_L.debug("Replay mode enabled")
57+
_exit(run(args.config, args.devices, args.register, args.theme, args.magnify_font_sizes, args.emu,
58+
args.auto_mfs, args.ignore_import_error))
59+
_exit()

leads_vec/cli.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,16 @@
66
CTkSegmentedButton as _CTkSegmentedButton
77
from pynput.keyboard import Listener as _Listener, Key as _Key, KeyCode as _KeyCode
88

9-
from leads import *
10-
from leads.comm import *
11-
from leads_audio import *
12-
from leads_gui import *
13-
from leads_raspberry_pi import *
9+
from leads import LEADS, SystemLiteral, require_config, register_context, DTCS, ABS, EBI, ATBS, GPSSpeedCorrection, \
10+
ESCMode, get_controller, MAIN_CONTROLLER, L, EventListener, DataPushedEvent, UpdateEvent, has_device, \
11+
GPS_RECEIVER, get_device, InterventionEvent, SuspensionEvent, Event, LEFT_INDICATOR, RIGHT_INDICATOR, SFT, \
12+
initialize_main
13+
from leads.comm import Callback, Service, start_server, create_server, my_ip_addresses
14+
from leads_audio import DIRECTION_INDICATOR_ON, DIRECTION_INDICATOR_OFF, WARNING, CONFIRM
15+
from leads_gui import RuntimeData, Window, GForceVar, FrequencyGenerator, Left, Color, Right, ContextManager, \
16+
Typography, Speedometer, ProxyCanvas, SpeedTrendMeter, GForceMeter, Stopwatch, Hazard, initialize, Battery, Brake, \
17+
ESC, Satellite, Motor, Speed
18+
from leads_raspberry_pi import LEDGroupCommand, LEDCommand, Transition, Entire
1419
from leads_vec.__version__ import __version__
1520

1621

leads_vec/replay.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from leads import controller, MAIN_CONTROLLER, require_config
2+
from leads.data_persistence import Dataset
3+
from leads_emulation.replay import ReplayController
4+
5+
6+
@controller(MAIN_CONTROLLER, args=(Dataset(f"{require_config().data_dir}/main.csv"),))
7+
class MainController(ReplayController):
8+
pass

leads_vec/run.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
from importlib.util import spec_from_file_location as _spec_from_file_location, module_from_spec as _module_from_spec
2+
from os.path import abspath as _abspath
3+
from os.path import exists as _exists
4+
from typing import Literal as _Literal
5+
6+
from customtkinter import set_default_color_theme as _set_default_color_theme
7+
8+
from leads import register_controller as _register_controller, MAIN_CONTROLLER as _MAIN_CONTROLLER, \
9+
L as _L, load_config as _load_config, register_config as _register_config, reset as _reset
10+
from leads_gui import Config as _Config
11+
12+
13+
def run(config: str,
14+
devices: str,
15+
register: _Literal["systemd", "config", "reverse_proxy"],
16+
theme: str,
17+
magnify_font_sizes: float,
18+
emu: bool,
19+
auto_mfs: bool,
20+
ignore_import_error: bool) -> int:
21+
if register == "systemd":
22+
from ._bootloader import register_leads_vec as _create_service
23+
24+
_create_service()
25+
_L.debug("Service registered")
26+
_L.debug(f"Service script is located at \"{_abspath(__file__)[:-6]}_bootloader/leads-vec.service.sh\"")
27+
elif register == "config":
28+
if _exists("config.json"):
29+
r = input("\"config.json\" already exists. Overwrite? (Y/n) >>>").lower()
30+
if r.lower() != "y":
31+
_L.error("Aborted")
32+
return 1
33+
with open("config.json", "w") as f:
34+
f.write(str(_Config({})))
35+
_L.debug("Configuration file saved to \"config.json\"")
36+
elif register == "reverse_proxy":
37+
from ._bootloader import start_frpc as _start_frpc
38+
39+
_start_frpc()
40+
_L.debug("`frpc` started")
41+
config = _load_config(config, _Config) if config else _Config({})
42+
_L.debug("Configuration loaded:", str(config))
43+
if t := theme:
44+
_set_default_color_theme(t)
45+
if (f := magnify_font_sizes) != 1:
46+
config.magnify_font_sizes(f)
47+
if auto_mfs:
48+
config.auto_magnify_font_sizes()
49+
_register_config(config)
50+
from leads_vec.cli import main
51+
52+
try:
53+
if emu:
54+
raise SystemError("User specifies to use emulator")
55+
spec = _spec_from_file_location("_", devices)
56+
spec.loader.exec_module(_module_from_spec(spec))
57+
except (ImportError, SystemError) as e:
58+
_L.debug(repr(e))
59+
if isinstance(e, ImportError):
60+
if ignore_import_error:
61+
_L.debug(f"Ignoring import error: {repr(e)}")
62+
return main()
63+
else:
64+
_L.warn(f"Specified devices module ({devices}) is not available, using emulation module instead...")
65+
_reset()
66+
try:
67+
from leads_emulation import SinController as _Controller
68+
69+
_register_controller(_MAIN_CONTROLLER, _Controller(0, 200, .05))
70+
except ImportError as e:
71+
_L.error(f"Emulator error: {repr(e)}")
72+
return 1
73+
return main()

0 commit comments

Comments
 (0)