|
29 | 29 | import sys
|
30 | 30 | import typing
|
31 | 31 |
|
| 32 | +import attr |
| 33 | + |
32 | 34 | import canmatrix
|
33 | 35 |
|
34 | 36 | logger = logging.getLogger(__name__)
|
35 | 37 | ConfigDict = typing.Optional[typing.Mapping[str, typing.Union[str, bool]]]
|
36 | 38 | WithAttribute = typing.TypeVar("WithAttribute", canmatrix.CanMatrix, canmatrix.Ecu, canmatrix.Frame, canmatrix.Signal)
|
37 | 39 |
|
38 | 40 |
|
| 41 | +@attr.s |
39 | 42 | class CompareResult(object):
|
40 | 43 | """Hold comparison results in logical tree."""
|
41 |
| - |
42 |
| - def __init__(self, result=None, mtype=None, ref=None, changes=None): |
43 |
| - # type: (str, str, typing.Any, typing.List) -> None |
44 |
| - # any of equal, added, deleted, changed |
45 |
| - self.result = result |
46 |
| - # db, ecu, frame, signal, attribute |
47 |
| - self._type = mtype |
48 |
| - # reference to related object |
49 |
| - self._ref = ref |
50 |
| - self._changes = changes |
51 |
| - self._children = [] # type: typing.List[CompareResult] |
| 44 | + result = attr.ib(default=None) # type: typing.Optional[str] # any of equal, added, deleted, changed |
| 45 | + type = attr.ib(default=None) # type: typing.Optional[str] # db, ecu, frame, signal, signalGroup or attribute |
| 46 | + ref = attr.ib(default=None) # type: typing.Any # reference to related object |
| 47 | + changes = attr.ib(default=None) # type: typing.Optional[typing.List] |
| 48 | + _children = attr.ib(factory=list) # type: typing.List[CompareResult] # nested CompareResults |
52 | 49 |
|
53 | 50 | def add_child(self, child):
|
54 | 51 | # type: (CompareResult) -> None
|
@@ -112,15 +109,15 @@ def compare_db(db1, db2, ignore=None):
|
112 | 109 | db2.global_defines))
|
113 | 110 |
|
114 | 111 | temp = compare_define_list(db1.ecu_defines, db2.ecu_defines)
|
115 |
| - temp._type = "ECU Defines" |
| 112 | + temp.type = "ECU Defines" |
116 | 113 | result.add_child(temp)
|
117 | 114 |
|
118 | 115 | temp = compare_define_list(db1.frame_defines, db2.frame_defines)
|
119 |
| - temp._type = "Frame Defines" |
| 116 | + temp.type = "Frame Defines" |
120 | 117 | result.add_child(temp)
|
121 | 118 |
|
122 | 119 | temp = compare_define_list(db1.signal_defines, db2.signal_defines)
|
123 |
| - temp._type = "Signal Defines" |
| 120 | + temp.type = "Signal Defines" |
124 | 121 | result.add_child(temp)
|
125 | 122 |
|
126 | 123 | if "VALUETABLES" in ignore and ignore["VALUETABLES"]:
|
@@ -475,33 +472,33 @@ def compare_signal(s1, s2, ignore=None):
|
475 | 472 |
|
476 | 473 | def dump_result(res, depth=0):
|
477 | 474 | # type: (CompareResult, int) -> None
|
478 |
| - if res._type is not None and res.result != "equal": |
| 475 | + if res.type is not None and res.result != "equal": |
479 | 476 | for _ in range(0, depth):
|
480 | 477 | print(" ", end=' ')
|
481 |
| - print(res._type + " " + res.result + " ", end=' ') |
482 |
| - if hasattr(res._ref, 'name'): |
483 |
| - print(res._ref.name) |
| 478 | + print(res.type + " " + res.result + " ", end=' ') |
| 479 | + if hasattr(res.ref, 'name'): |
| 480 | + print(res.ref.name) |
484 | 481 | else:
|
485 | 482 | print(" ")
|
486 |
| - if res._changes is not None and res._changes[0] is not None and res._changes[1] is not None: |
| 483 | + if res.changes is not None and res.changes[0] is not None and res.changes[1] is not None: |
487 | 484 | for _ in range(0, depth):
|
488 | 485 | print(" ", end=' ')
|
489 |
| - print(type(res._changes[0])) |
| 486 | + print(type(res.changes[0])) |
490 | 487 | if sys.version_info[0] < 3:
|
491 |
| - if isinstance(res._changes[0], type(u'')): |
492 |
| - res._changes[0] = res._changes[0].encode('ascii', 'ignore') |
493 |
| - if isinstance(res._changes[1], type(u'')): |
494 |
| - res._changes[1] = res._changes[1].encode('ascii', 'ignore') |
| 488 | + if isinstance(res.changes[0], type(u'')): |
| 489 | + res.changes[0] = res.changes[0].encode('ascii', 'ignore') |
| 490 | + if isinstance(res.changes[1], type(u'')): |
| 491 | + res.changes[1] = res.changes[1].encode('ascii', 'ignore') |
495 | 492 | else:
|
496 |
| - if type(res._changes[0]) == str: |
497 |
| - res._changes[0] = res._changes[0].encode('ascii', 'ignore') |
498 |
| - if type(res._changes[1]) == str: |
499 |
| - res._changes[1] = res._changes[1].encode('ascii', 'ignore') |
| 493 | + if type(res.changes[0]) == str: |
| 494 | + res.changes[0] = res.changes[0].encode('ascii', 'ignore') |
| 495 | + if type(res.changes[1]) == str: |
| 496 | + res.changes[1] = res.changes[1].encode('ascii', 'ignore') |
500 | 497 | print("old: " +
|
501 |
| - str(res._changes[0]) + |
| 498 | + str(res.changes[0]) + |
502 | 499 | " new: " +
|
503 |
| - str(res._changes[1])) |
504 |
| - for child in res._children: |
| 500 | + str(res.changes[1])) |
| 501 | + for child in res.children: |
505 | 502 | dump_result(child, depth + 1)
|
506 | 503 |
|
507 | 504 |
|
|
0 commit comments