Skip to content

Commit 40b5eab

Browse files
Merge pull request #19 from networktocode-llc/gfm-dsync-print-detailed
Add print_detailed() API to DSync and DSyncModel classes
2 parents 75eee89 + fd87e79 commit 40b5eab

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

dsync/__init__.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,26 @@ def __repr__(self):
158158
def __str__(self):
159159
return self.get_unique_id()
160160

161+
def print_detailed(self, dsync: "Optional[DSync]" = None, indent: int = 0):
162+
"""Print this model and its children."""
163+
margin = " " * indent
164+
if not dsync:
165+
dsync = self.dsync
166+
print(f"{margin}{self.get_type()}: {self.get_unique_id()}")
167+
for modelname, fieldname in self._children.items():
168+
print(f"{margin} {modelname}")
169+
child_ids = getattr(self, fieldname)
170+
if not child_ids:
171+
print(f"{margin} (none)")
172+
for child_id in child_ids:
173+
child = None
174+
if dsync:
175+
child = dsync.get(modelname, child_id)
176+
if not child:
177+
print(f"{margin} {child_id} (no details available)")
178+
else:
179+
child.print_detailed(dsync, indent + 4)
180+
161181
@classmethod
162182
def create(cls, dsync: "DSync", ids: dict, attrs: dict) -> Optional["DSyncModel"]:
163183
"""Instantiate this class, along with any platform-specific data creation.
@@ -378,6 +398,17 @@ def load(self):
378398
"""Load all desired data from whatever backend data source into this instance."""
379399
# No-op in this generic class
380400

401+
def print_detailed(self, indent: int = 0):
402+
"""Recursively print this DSync and its contained models."""
403+
margin = " " * indent
404+
for modelname in self.top_level:
405+
print(f"{margin}{modelname}")
406+
models = self.get_all(modelname)
407+
if not models:
408+
print(f"{margin} (none)")
409+
for model in models:
410+
model.print_detailed(self, indent + 2)
411+
381412
# ------------------------------------------------------------------------------
382413
# Synchronization between DSync instances
383414
# ------------------------------------------------------------------------------

examples/example1/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,15 @@ from backend_c import BackendC
1616

1717
a = BackendA()
1818
a.load()
19+
a.print_detailed()
20+
1921
b = BackendB()
2022
b.load()
23+
b.print_detailed()
24+
2125
c = BackendC()
2226
c.load()
27+
c.print_detailed()
2328
```
2429

2530
Configure verbosity of DSync's structured logging to console; the default is full verbosity (all logs including debugging)

examples/example1/main.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,17 @@ def main():
3232
print("Initializing and loading Backend A...")
3333
backend_a = BackendA(name="Backend-A")
3434
backend_a.load()
35+
backend_a.print_detailed()
3536

3637
print("Initializing and loading Backend B...")
3738
backend_b = BackendB(name="Backend-B")
3839
backend_b.load()
40+
backend_b.print_detailed()
3941

4042
print("Initializing and loading Backend C...")
4143
backend_c = BackendC()
4244
backend_c.load()
45+
backend_c.print_detailed()
4346

4447
print("Getting diffs from Backend A to Backend B...")
4548
diff_a_b = backend_a.diff_to(backend_b, diff_class=MyDiff)

0 commit comments

Comments
 (0)