8000 Merge branch 'dga-issue-12' into dga-int-20201011 · networktocode/diffsync@a7f21bd · GitHub
[go: up one dir, main page]

Skip to content

Commit a7f21bd

Browse files
committed
Merge branch 'dga-issue-12' into dga-int-20201011
2 parents b88d114 + 466309d commit a7f21bd

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

dsync/diff.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,30 @@ def has_diffs(self) -> bool:
6868
return False
6969

7070
def get_children(self) -> Iterator["DiffElement"]:
71-
"""Iterate over all child elements in all groups in self.children."""
71+
"""Iterate over all child elements in all groups in self.children.
72+
73+
For each group of children, check if an order method is defined,
74+
Otherwise use the default method.
75+
"""
76+
order_default = "order_children_default"
77+
7278
for group in self.groups():
73-
for child in self.children[group].values():
74-
yield child
79+
order_method_name = f"order_children_{group}"
80+
if hasattr(self, order_method_name):
81+
order_method = getattr(self, order_method_name)
82+
else:
83+
order_method = getattr(self, order_default)
84+
85+
yield from order_method(self.children[group])
86+
87+
@classmethod
88+
def order_children_default(cls, children: dict) -> Iterator["DiffElement"]:
89+
"""Default method to an Iterator for children.
90+
91+
Since children is already an OrderedDefaultDict, this method is not doing anything special.
92+
"""
93+
for child in children.values():
94+
yield child
7595

7696
def print_detailed(self, indent: int = 0):
7797
"""Print all diffs to screen for all child elements.
@@ -82,7 +102,7 @@ def print_detailed(self, indent: int = 0):
82102
margin = " " * indent
83103
for group in self.groups():
84104
print(f"{margin}{group}")
85-
for child in self.children[group].values():
105+
for child in self.get_children():
86106
if child.has_diffs():
87107
child.print_detailed(indent + 2)
88108

examples/example1/main.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,20 @@
55
from backend_c import BackendC
66

77

8+
from dsync import Diff
9+
10+
11+
class MyDiff(Diff):
12+
"""Custom Diff class to control the order of the site objects."""
13+
14+
@classmethod
15+
def order_children_site(cls, children):
16+
"""Return the site children ordered in alphabetical order."""
17+
keys = sorted(children.keys(), reverse=False)
18+
for key in keys:
19+
yield children[key]
20+
21+
822
def main():
923
"""Demonstrate DSync behavior using the example backends provided."""
1024
# pylint: disable=invalid-name
@@ -18,7 +32,7 @@ def main():
1832
c = BackendC()
1933
c.load()
2034

21-
diff_a_b = a.diff_to(b)
35+
diff_a_b = a.diff_to(b, diff_class=MyDiff)
2236
diff_a_b.print_detailed()
2337

2438
a.sync_to(b)

0 commit comments

Comments
 (0)
0