-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path__main__.py
349 lines (276 loc) · 12.3 KB
/
__main__.py
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
"""Main entry point for objdictgen / odg."""
#
# Copyright (C) 2022-2024 Svein Seldal, Laerdal Medical AS
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
# USA
from __future__ import annotations
import argparse
import functools
import logging
import sys
from dataclasses import dataclass, field
from typing import Callable, Sequence, TypeVar
from colorama import Fore, Style, init
from objdictgen import ODG_PROGRAM, __version__, jsonod
from objdictgen.node import Node
from objdictgen.printing import format_diff_nodes, format_node
from objdictgen.typing import TPath
from objdictgen.utils import exc_amend
T = TypeVar('T')
# Initalize the python logger to simply output to stdout
log = logging.getLogger()
log.setLevel(logging.INFO)
log.addHandler(logging.StreamHandler(sys.stdout))
@dataclass
class DebugOpts:
""" Options for main to control the debug_wrapper """
show_debug: bool = field(default=False)
def set_debug(self, dbg: bool) -> None:
"""Set debug level"""
self.show_debug = dbg
log.setLevel(logging.DEBUG)
def debug_wrapper() -> Callable[[Callable[..., T]], Callable[..., T]]:
""" Wrapper to catch all exceptions and supress the output unless debug
is set
"""
def decorator(fn: Callable[..., T]) -> Callable[..., T]:
@functools.wraps(fn)
def inner(*args, **kw):
opts = DebugOpts()
try:
return fn(opts, *args, **kw)
except Exception as exc: # pylint: disable=broad-except
if opts.show_debug:
raise
print(f"{ODG_PROGRAM}: {exc.__class__.__name__}: {exc}")
sys.exit(1)
return inner
return decorator
def open_od(fname: TPath|str, validate=True, fix=False) -> Node:
""" Open and validate the OD file"""
try:
od = Node.LoadFile(fname, validate=validate)
if validate:
od.Validate(fix=fix)
return od
except Exception as exc:
exc_amend(exc, f"{fname}: ")
raise
@debug_wrapper()
def main(debugopts: DebugOpts, args: Sequence[str]|None = None):
""" Main command dispatcher """
parser = argparse.ArgumentParser(
prog=ODG_PROGRAM,
description="""
A tool to read and convert object dictionary files for the
CAN festival library
""",
add_help=True,
)
# FIXME: New options: new file, add parameter, delete parameter, copy parameter
subparser = parser.add_subparsers(title="command", dest="command", metavar="command", help="""
Commands
""", required=True)
# -- COMMON --
opt_debug = dict(action='store_true', help="Debug: enable tracebacks on errors")
opt_od = dict(metavar='od', default=None, help="Object dictionary")
opt_novalidate = dict(action='store_true', help="Don't validate input files")
opt_nocolor = dict(action='store_true', help="Disable colored output")
parser.add_argument('--version', action='version', version='%(prog)s ' + __version__)
parser.add_argument('--no-color', action='store_true', help="Disable colored output")
parser.add_argument('-D', '--debug', **opt_debug) # type: ignore[arg-type]
# -- HELP --
subp = subparser.add_parser('help', help="""
Show help of all commands
""")
subp.add_argument('subcommand', nargs='?', help="Show help of specific command")
subp.add_argument('-D', '--debug', **opt_debug) # type: ignore[arg-type]
# -- CONVERT --
subp = subparser.add_parser('convert', help="""
Generate
""", aliases=['gen', 'conv'])
subp.add_argument('od', **opt_od) # type: ignore[arg-type]
subp.add_argument('out', default=None, help="Output file")
subp.add_argument('-i', '--index', action="append",
help="OD Index to include. Filter out the rest.")
subp.add_argument('-x', '--exclude', action="append", help="OD Index to exclude.")
subp.add_argument('-f', '--fix', action="store_true",
help="Fix any inconsistency errors in OD before generate output")
subp.add_argument('-t', '--type', choices=['od', 'eds', 'json', 'jsonc', 'c'],
help="Select output file type")
subp.add_argument('--drop-unused', action="store_true", help="Remove unused parameters")
subp.add_argument('--internal', action="store_true",
help="Store in internal format (json only)")
subp.add_argument('--no-sort', action="store_true",
help="Don't order of parameters in output OD")
subp.add_argument('--novalidate', **opt_novalidate) # type: ignore[arg-type]
subp.add_argument('-D', '--debug', **opt_debug) # type: ignore[arg-type]
# -- DIFF --
subp = subparser.add_parser('diff', help="""
Compare OD files
""", aliases=['compare'])
subp.add_argument('od1', **opt_od) # type: ignore[arg-type]
subp.add_argument('od2', **opt_od) # type: ignore[arg-type]
subp.add_argument('--show', action="store_true", help="Show difference data")
subp.add_argument('--internal', action="store_true", help="Diff internal object")
subp.add_argument('--data', action="store_true", help="Show difference as data")
subp.add_argument('--raw', action="store_true", help="Show raw difference")
subp.add_argument('--no-color', **opt_nocolor) # type: ignore[arg-type]
subp.add_argument('--novalidate', **opt_novalidate) # type: ignore[arg-type]
subp.add_argument('-D', '--debug', **opt_debug) # type: ignore[arg-type]
# -- EDIT --
subp = subparser.add_parser('edit', help="""
Edit OD (UI)
""")
subp.add_argument('od', nargs="*", help="Object dictionary")
subp.add_argument('-D', '--debug', **opt_debug) # type: ignore[arg-type]
# -- LIST --
subp = subparser.add_parser('list', help="""
List
""", aliases=['cat'])
subp.add_argument('od', nargs="+", help="Object dictionary")
subp.add_argument('-i', '--index', action="append", help="Specify parameter index to show")
subp.add_argument('--all', action="store_true",
help="Show all subindexes, including subindex 0")
subp.add_argument('--sort', action="store_true", help="Sort output")
subp.add_argument('--compact', action="store_true", help="Compact listing")
subp.add_argument('--raw', action="store_true", help="Show raw parameter values")
subp.add_argument('--short', action="store_true", help="Do not list sub-index")
subp.add_argument('--unused', action="store_true", help="Include unused profile parameters")
subp.add_argument('--internal', action="store_true", help="Show internal data")
subp.add_argument('--no-color', **opt_nocolor) # type: ignore[arg-type]
subp.add_argument('--novalidate', **opt_novalidate) # type: ignore[arg-type]
subp.add_argument('-D', '--debug', **opt_debug) # type: ignore[arg-type]
# -- NETWORK --
subp = subparser.add_parser('network', help="""
Edit network (UI)
""")
subp.add_argument('dir', nargs="?", help="Project directory")
subp.add_argument('-D', '--debug', **opt_debug) # type: ignore[arg-type]
# -- NODELIST --
subp = subparser.add_parser('nodelist', help="""
List project nodes
""")
subp.add_argument('dir', nargs="?", help="Project directory")
subp.add_argument('-D', '--debug', **opt_debug) # type: ignore[arg-type]
# -- COMMON --
# Parse command-line arguments
opts = parser.parse_args(args)
# Enable debug mode
if opts.debug:
debugopts.set_debug(opts.debug)
# Enable colored output
if opts.no_color:
init(strip=True)
else:
init()
# -- HELP command --
if opts.command == "help":
if opts.subcommand:
for subparsers_action in (
a for a in parser._actions # pylint: disable=protected-access
if isinstance(a, argparse._SubParsersAction) # pylint: disable=protected-access
):
for choice, subparser in subparsers_action.choices.items():
if choice != opts.subcommand:
continue
# FIXME: Not sure why mypy doesn't know about format_help
print(subparser.format_help(), end="") # type: ignore[attr-defined]
else:
parser.print_help()
print()
print("""For detailed help for each command:
odg <command> --help
""")
# -- CONVERT command --
elif opts.command in ("convert", "conv", "gen"):
od = open_od(opts.od, fix=opts.fix, validate=not opts.novalidate)
to_remove: set[int] = set()
# Drop excluded parameters
if opts.exclude:
to_remove |= set(jsonod.str_to_int(i) for i in opts.exclude)
# Drop unused parameters
if opts.drop_unused:
to_remove |= set(od.GetUnusedParameters())
# Drop all other indexes than specified
if opts.index:
index = [jsonod.str_to_int(i) for i in opts.index]
to_remove |= (set(od.GetAllIndices()) - set(index))
# Have any parameters to delete?
if to_remove:
print("Removed parameters:")
info = [
od.GetPrintEntryHeader(k, unused=True)
for k in sorted(to_remove)
]
od.RemoveIndex(to_remove)
for line, fmt in info:
print(line.format(**fmt))
# Write the data
od.DumpFile(opts.out,
filetype=opts.type,
# These additional options are only used for JSON output
sort=not opts.no_sort, internal=opts.internal, validate=not opts.novalidate
)
# -- DIFF command --
elif opts.command in ("diff", "compare"):
od1 = open_od(opts.od1, validate=not opts.novalidate)
od2 = open_od(opts.od2, validate=not opts.novalidate)
lines = list(format_diff_nodes(od1, od2, data=opts.data, raw=opts.raw,
internal=opts.internal, show=opts.show))
for line in lines:
print(line)
errcode = 1 if lines else 0
if errcode:
print(f"{ODG_PROGRAM}: '{opts.od1}' and '{opts.od2}' differ")
else:
print(f"{ODG_PROGRAM}: '{opts.od1}' and '{opts.od2}' are equal")
if errcode:
parser.exit(errcode)
# -- EDIT command --
elif opts.command == "edit":
# Import here to prevent including optional UI components for cmd-line use
from .ui.objdictedit import \
uimain # pylint: disable=import-outside-toplevel
uimain(opts.od)
# -- LIST command --
elif opts.command in ("list", "cat"):
for n, name in enumerate(opts.od):
if n > 0:
print()
if len(opts.od) > 1:
print(Fore.LIGHTBLUE_EX + name + '\n' + "=" * len(name) + Style.RESET_ALL)
od = open_od(name, validate=not opts.novalidate)
for line in format_node(od, name, index=opts.index, opts=opts):
print(line)
# -- NETWORK command --
elif opts.command == "network":
# Import here to prevent including optional UI components for cmd-line use
from .ui.networkedit import \
uimain # pylint: disable=import-outside-toplevel
uimain(opts.dir)
# -- NODELIST command --
elif opts.command == "nodelist":
# Import here to prevent including optional UI components for cmd-line use
from .nodelist import \
main as _main # pylint: disable=import-outside-toplevel
_main(opts.dir)
else:
parser.error(f"Programming error: Uknown option '{opts.command}'")
# To support -m objdictgen
if __name__ == '__main__':
# pylint: disable=no-value-for-parameter
main()