-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathcli.py
More file actions
100 lines (79 loc) · 3.18 KB
/
cli.py
File metadata and controls
100 lines (79 loc) · 3.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
"""Tinymovr Studio CLI
Usage:
tinymovr_cli [--bus=<bus>] [--chan=<chan>] [--spec=<spec>] [--bitrate=<bitrate>]
tinymovr_cli -h | --help
tinymovr_cli --version
Options:
--bus=<bus> One or more interfaces to use, first available is used [default: canine,slcan_disco].
--chan=<chan> The bus device "channel".
--spec=<spec> A custom device spec to be added to the list of discoverable spec.
--bitrate=<bitrate> CAN bitrate [default: 1000000].
"""
import yaml
import can
import IPython
from importlib.metadata import version as get_version
from traitlets.config import Config
from docopt import docopt
from tinymovr import init_router, destroy_router
from tinymovr.device_discovery import DeviceDiscovery
from tinymovr.constants import app_name
from tinymovr.config import get_bus_config, configure_logging, add_spec
"""
Tinymovr CLI Module
Copyright 2020-2026 MotionLayer P.C.
The Tinymovr Studio IPython-based command line interface
This program is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>.
"""
def spawn():
"""
Spawns the Tinymovr Studio IPython-based CLI.
"""
version = get_version("tinymovr")
arguments = docopt(__doc__, version=app_name + " " + str(version))
logger = configure_logging()
spec_file = arguments["--spec"]
if spec_file:
with open(spec_file, 'r') as file:
spec_data = yaml.safe_load(file)
add_spec(spec_data, logger)
buses = arguments["--bus"].rsplit(sep=",")
channel = arguments["--chan"]
bitrate = int(arguments["--bitrate"])
if not channel:
params = get_bus_config(buses, bitrate=bitrate)
else:
params = {"bustype": buses[0], "channel": channel, "bitrate": bitrate}
tms = {}
user_ns = {}
user_ns["tms"] = tms
def node_appeared(node, node_id):
display_name = "{}{}".format(node.name, node_id)
print("Found {} with device uid {}".format(display_name, node.uid))
tms[node_id] = node
user_ns[display_name] = node
def node_disappeared(node_id):
display_name = "{}{}".format(tms[node_id].name, node_id)
print("Lost {}".format(display_name))
del user_ns[display_name]
del tms[node_id]
print(app_name + " " + str(version))
#TODO: router init should not happen in CLI spawn function
init_router(can.Bus, params, logger=logger)
dsc = DeviceDiscovery(node_appeared, node_disappeared, logger)
print("Listening for nodes...")
c = Config()
c.TerminalIPythonApp.display_banner = False
IPython.start_ipython(argv=[], config=c, user_ns=user_ns)
logger.debug("Exiting...")
destroy_router()
if __name__ == "__main__":
spawn()