25
25
from __future__ import print_function
26
26
27
27
import logging
28
- import optparse
29
28
import sys
30
29
import typing
30
+ import click
31
31
32
32
import attr
33
33
import canmatrix .compare
34
34
35
35
logger = logging .getLogger (__name__ )
36
36
37
+ @click .command ()
38
+ @click .option ('-v' , '--verbose' , 'verbosity' , help = "Output verbosity" , count = True , default = 1 )
39
+ @click .option ('-s' , '--silent' , is_flag = True , default = False , help = "don't print status messages to stdout. (only errors)" )
40
+ @click .option ('-f' , '--frames' , is_flag = True , default = False , help = "show list of frames" )
41
+ @click .option ('-c' , '--comments' , 'check_comments' , is_flag = True , default = False , help = "ignore changed comments" )
42
+ @click .option ('-a' , '--attributes' , 'check_attributes' , is_flag = True , default = False , help = "ignore changed attributes" )
43
+ @click .option ('-t' , '--valueTable' , 'ignore_valuetables' , is_flag = True , default = False , help = "ignore changed valuetables" )
44
+ @click .argument ('matrix1' , required = 1 )
45
+ @click .argument ('matrix2' , required = 1 )
46
+ def cli_compare (matrix1 , matrix2 , verbosity , silent , check_comments , check_attributes , ignore_valuetables , frames ): # type: () -> int
47
+ """
48
+ canmatrix.cli.compare [options] matrix1 matrix2
49
+
50
+ matrixX can be any of *.dbc|*.dbf|*.kcd|*.arxml|*.xls(x)|*.sym
51
+ """
37
52
38
- def main (): # type: () -> int
39
53
import canmatrix .log
40
54
canmatrix .log .setup_logger ()
41
55
42
- usage = """
43
- %prog [options] cancompare matrix1 matrix2
44
56
45
- matrixX can be any of *.dbc|*.dbf|*.kcd|*.arxml
46
- """
47
57
48
- parser = optparse .OptionParser (usage = usage )
49
- parser .add_option (
50
- "-s" ,
51
- dest = "silent" ,
52
- action = "store_true" ,
53
- help = "don't print status messages to stdout. (only errors)" ,
54
- default = False )
55
- parser .add_option (
56
- "-v" ,
57
- dest = "verbosity" ,
58
- action = "count" ,
59
- help = "Output verbosity" ,
60
- default = 0 )
61
- parser .add_option (
62
- "-f" , "--frames" ,
63
- dest = "frames" ,
64
- action = "store_true" ,
65
- help = "show list of frames" ,
66
- default = False )
67
- parser .add_option (
68
- "-c" , "--comments" ,
69
- dest = "check_comments" ,
70
- action = "store_true" ,
71
- help = "check changed comments" ,
72
- default = False )
73
- parser .add_option (
74
- "-a" , "--attributes" ,
75
- dest = "check_attributes" ,
76
- action = "store_true" ,
77
- help = "check changed attributes" ,
78
- default = False )
79
- parser .add_option (
80
- "-t" , "--valueTable" ,
81
- dest = "ignore_valuetables" ,
82
- action = "store_true" ,
83
- help = "check changed valuetables" ,
84
- default = False )
85
-
86
- (cmdlineOptions , args ) = parser .parse_args ()
87
-
88
- if len (args ) < 2 :
89
- parser .print_help ()
90
- sys .exit (1 )
91
-
92
- matrix1 = args [0 ]
93
- matrix2 = args [1 ]
94
-
95
- verbosity = cmdlineOptions .verbosity
96
- if cmdlineOptions .silent :
58
+ if silent :
97
59
# Only print ERROR messages (ignore import warnings)
98
60
verbosity = - 1
99
61
canmatrix .log .set_log_level (logger , verbosity )
@@ -102,25 +64,25 @@ def main(): # type: () -> int
102
64
import canmatrix .formats # due this import we need the import alias for log module
103
65
104
66
logger .info ("Importing " + matrix1 + " ... " )
105
- db1 = next ( iter ( canmatrix .formats .loadp (matrix1 ). values ()) )
67
+ db1 = canmatrix .formats .loadp_flat (matrix1 )
106
68
logger .info ("%d Frames found" % (db1 .frames .__len__ ()))
107
69
108
70
logger .info ("Importing " + matrix2 + " ... " )
109
- db2 = next ( iter ( canmatrix .formats .loadp (matrix2 ). values ()) )
71
+ db2 = canmatrix .formats .loadp_flat (matrix2 )
110
72
logger .info ("%d Frames found" % (db2 .frames .__len__ ()))
111
73
112
74
ignore = {} # type: typing.Dict[str, typing.Union[str, bool]]
113
75
114
- if not cmdlineOptions . check_comments :
76
+ if not check_comments :
115
77
ignore ["comment" ] = "*"
116
78
117
- if not cmdlineOptions . check_attributes :
79
+ if not check_attributes :
118
80
ignore ["ATTRIBUTE" ] = "*"
119
81
120
- if cmdlineOptions . ignore_valuetables :
82
+ if ignore_valuetables :
121
83
ignore ["VALUETABLES" ] = True
122
84
123
- if cmdlineOptions . frames :
85
+ if frames :
124
86
only_in_matrix1 = [
125
87
frame .name
126
88
for frame in db1 .frames
@@ -144,4 +106,4 @@ def main(): # type: () -> int
144
106
145
107
# to be run as module `python -m canmatrix.compare`, NOT as script with argument `canmatrix/compare.py`
146
108
if __name__ == '__main__' :
147
- sys .exit (main ())
109
+ sys .exit (cli_compare ())
0 commit comments