23
23
import logging
24
24
import sys
25
25
from dataclasses import dataclass , field
26
- from pprint import pformat
27
- from typing import TYPE_CHECKING , Callable , Generator , Sequence , TypeVar
26
+ from typing import Callable , Sequence , TypeVar
28
27
29
28
from colorama import Fore , Style , init
30
29
31
30
import objdictgen
32
31
from objdictgen import jsonod
33
32
from objdictgen .node import Node
34
- from objdictgen .printing import format_node
35
- from objdictgen .typing import TDiffEntries , TDiffNodes , TPath
33
+ from objdictgen .printing import format_diff_nodes , format_node
34
+ from objdictgen .typing import TPath
36
35
37
36
T = TypeVar ('T' )
38
37
@@ -88,38 +87,6 @@ def open_od(fname: TPath|str, validate=True, fix=False) -> "Node":
88
87
raise
89
88
90
89
91
- def print_diffs (diffs : TDiffNodes , show = False ):
92
- """ Print the differences between two object dictionaries"""
93
-
94
- def _pprint (text : str ):
95
- for line in pformat (text ).splitlines ():
96
- print (" " , line )
97
-
98
- def _printlines (entries : TDiffEntries ):
99
- for chtype , change , path in entries :
100
- if 'removed' in chtype :
101
- print (f"<<< { path } only in LEFT" )
102
- if show :
103
- _pprint (change .t1 )
104
- elif 'added' in chtype :
105
- print (f" >>> { path } only in RIGHT" )
106
- if show :
107
- _pprint (change .t2 )
108
- elif 'changed' in chtype :
109
- print (f"<< - >> { path } value changed from '{ change .t1 } ' to '{ change .t2 } '" )
110
- else :
111
- print (f"{ Fore .RED } { chtype } { path } { change } { Style .RESET_ALL } " )
112
-
113
- rest = diffs .pop ('' , None )
114
- if rest :
115
- print (f"{ Fore .GREEN } Changes:{ Style .RESET_ALL } " )
116
- _printlines (rest )
117
-
118
- for index in sorted (diffs ):
119
- print (f"{ Fore .GREEN } Index 0x{ index :04x} ({ index } ){ Style .RESET_ALL } " )
120
- _printlines (diffs [index ])
121
-
122
-
123
90
@debug_wrapper ()
124
91
def main (debugopts : DebugOpts , args : Sequence [str ]| None = None ):
125
92
""" Main command dispatcher """
@@ -144,6 +111,7 @@ def main(debugopts: DebugOpts, args: Sequence[str]|None = None):
144
111
opt_debug = dict (action = 'store_true' , help = "Debug: enable tracebacks on errors" )
145
112
opt_od = dict (metavar = 'od' , default = None , help = "Object dictionary" )
146
113
opt_novalidate = dict (action = 'store_true' , help = "Don't validate input files" )
114
+ opt_nocolor = dict (action = 'store_true' , help = "Disable colored output" )
147
115
148
116
parser .add_argument ('--version' , action = 'version' , version = '%(prog)s ' + objdictgen .__version__ )
149
117
parser .add_argument ('--no-color' , action = 'store_true' , help = "Disable colored output" )
@@ -183,11 +151,13 @@ def main(debugopts: DebugOpts, args: Sequence[str]|None = None):
183
151
""" , aliases = ['compare' ])
184
152
subp .add_argument ('od1' , ** opt_od ) # type: ignore[arg-type]
185
153
subp .add_argument ('od2' , ** opt_od ) # type: ignore[arg-type]
154
+ subp .add_argument ('--show' , action = "store_true" , help = "Show difference data" )
186
155
subp .add_argument ('--internal' , action = "store_true" , help = "Diff internal object" )
156
+ subp .add_argument ('--data' , action = "store_true" , help = "Show difference as data" )
157
+ subp .add_argument ('--raw' , action = "store_true" , help = "Show raw difference" )
158
+ subp .add_argument ('--no-color' , ** opt_nocolor ) # type: ignore[arg-type]
187
159
subp .add_argument ('--novalidate' , ** opt_novalidate ) # type: ignore[arg-type]
188
- subp .add_argument ('--show' , action = "store_true" , help = "Show difference data" )
189
160
subp .add_argument ('-D' , '--debug' , ** opt_debug ) # type: ignore[arg-type]
190
- subp .add_argument ('--no-color' , action = 'store_true' , help = "Disable colored output" )
191
161
192
162
# -- EDIT --
193
163
subp = subparser .add_parser ('edit' , help = """
@@ -210,9 +180,9 @@ def main(debugopts: DebugOpts, args: Sequence[str]|None = None):
210
180
subp .add_argument ('--short' , action = "store_true" , help = "Do not list sub-index" )
211
181
subp .add_argument ('--unused' , action = "store_true" , help = "Include unused profile parameters" )
212
182
subp .add_argument ('--internal' , action = "store_true" , help = "Show internal data" )
213
- subp .add_argument ('-D' , '--debug ' , ** opt_debug ) # type: ignore[arg-type]
183
+ subp .add_argument ('--no-color ' , ** opt_nocolor ) # type: ignore[arg-type]
214
184
subp .add_argument ('--novalidate' , ** opt_novalidate ) # type: ignore[arg-type]
215
- subp .add_argument ('--no-color ' , action = 'store_true ' , help = "Disable colored output" )
185
+ subp .add_argument ('-D ' , '--debug ' , ** opt_debug ) # type: ignore[arg-type]
216
186
217
187
# -- NETWORK --
218
188
subp = subparser .add_parser ('network' , help = """
@@ -306,22 +276,22 @@ def main(debugopts: DebugOpts, args: Sequence[str]|None = None):
306
276
307
277
# -- DIFF command --
308
278
elif opts .command in ("diff" , "compare" ):
279
+
309
280
od1 = open_od (opts .od1 , validate = not opts .novalidate )
310
281
od2 = open_od (opts .od2 , validate = not opts .novalidate )
311
282
312
- diffs = jsonod .diff_nodes (
313
- od1 , od2 , asdict = not opts .internal ,
314
- validate = not opts .novalidate ,
315
- )
283
+ lines = list (format_diff_nodes (od1 , od2 , data = opts .data , raw = opts .raw ,
284
+ internal = opts .internal , show = opts .show ))
285
+
286
+ for line in lines :
287
+ print (line )
316
288
317
- if diffs :
318
- errcode = 1
289
+ errcode = 1 if lines else 0
290
+ if errcode :
319
291
print (f"{ objdictgen .ODG_PROGRAM } : '{ opts .od1 } ' and '{ opts .od2 } ' differ" )
320
292
else :
321
- errcode = 0
322
293
print (f"{ objdictgen .ODG_PROGRAM } : '{ opts .od1 } ' and '{ opts .od2 } ' are equal" )
323
294
324
- print_diffs (diffs , show = opts .show )
325
295
if errcode :
326
296
parser .exit (errcode )
327
297
0 commit comments