1 | import collections |
||
2 | |||
3 | from coalib.misc.Decorators import generate_repr |
||
4 | |||
5 | |||
6 | class ConflictError(Exception): |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
![]() |
|||
7 | pass |
||
8 | |||
9 | |||
10 | @generate_repr('change', 'delete', 'add_after') |
||
11 | class LineDiff: |
||
12 | """ |
||
13 | A LineDiff holds the difference between two strings. |
||
14 | """ |
||
15 | |||
16 | def __init__(self, change=False, delete=False, add_after=False): |
||
17 | """ |
||
18 | Creates a new LineDiff object. Note that a line cannot be |
||
19 | changed _and_ deleted at the same time. |
||
20 | |||
21 | :param change: False or a tuple (original, replacement) |
||
22 | :param delete: True/False |
||
23 | :param add_after: False or a list of lines to append after this ones |
||
24 | """ |
||
25 | # change property setter will need this value for assertion |
||
26 | self._delete = False |
||
27 | |||
28 | self.change = change |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
29 | self.delete = delete |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
30 | self.add_after = add_after |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
31 | |||
32 | def __eq__(self, other): |
||
33 | return (self.change == other.change and |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
34 | self.delete == other.delete and |
||
35 | self.add_after == other.add_after) |
||
36 | |||
37 | @property |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
38 | def change(self): |
||
39 | return self._change |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
40 | |||
41 | @change.setter |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
42 | def change(self, value): |
||
43 | if value is not False and not isinstance(value, tuple): |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Comprehensibility
Best Practice
introduced
by
|
|||
44 | raise TypeError("change must be False or a tuple with an original " |
||
45 | "and a replacement string.") |
||
46 | if value is not False and self.delete is not False: |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
47 | raise ConflictError("A line cannot be changed and deleted " |
||
48 | "at the same time.") |
||
49 | |||
50 | self._change = value |
||
51 | |||
52 | @property |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
53 | def delete(self): |
||
54 | return self._delete |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
55 | |||
56 | @delete.setter |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
57 | def delete(self, value): |
||
58 | if not isinstance(value, bool): |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Comprehensibility
Best Practice
introduced
by
|
|||
59 | raise TypeError("delete can only be a boolean value.") |
||
60 | if value is not False and self.change is not False: |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
61 | raise ConflictError("A line cannot be changed and deleted " |
||
62 | "at the same time.") |
||
63 | |||
64 | self._delete = value |
||
65 | |||
66 | @property |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
67 | def add_after(self): |
||
68 | return self._add_after |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
69 | |||
70 | @add_after.setter |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
71 | def add_after(self, value): |
||
72 | if value is not False and not isinstance(value, collections.Iterable): |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Comprehensibility
Best Practice
introduced
by
|
|||
73 | raise TypeError( |
||
74 | "add_after must be False or a list of lines to append.") |
||
75 | if isinstance(value, collections.Iterable): |
||
76 | value = list(value) |
||
77 | self._add_after = value if value != [] else False |
||
78 |