Skip to content

Commit bf3a8dd

Browse files
author
Leo Kirchner
committed
renames diffsync.DiffSync to diffsync.Adapter
1 parent 2c1577f commit bf3a8dd

File tree

21 files changed

+76
-69
lines changed

21 files changed

+76
-69
lines changed

diffsync/__init__.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ class DiffSyncModel(BaseModel):
9191
Can be set as a class attribute or an instance attribute as needed.
9292
"""
9393

94-
diffsync: Optional["DiffSync"] = None
94+
diffsync: Optional["Adapter"] = None
9595
"""Optional: the DiffSync instance that owns this model instance."""
9696

9797
_status: DiffSyncStatus = PrivateAttr(DiffSyncStatus.SUCCESS)
@@ -183,7 +183,7 @@ def set_status(self, status: DiffSyncStatus, message: StrType = "") -> None:
183183
self._status_message = message
184184

185185
@classmethod
186-
def create_base(cls, diffsync: "DiffSync", ids: Dict, attrs: Dict) -> Optional[Self]:
186+
def create_base(cls, diffsync: "Adapter", ids: Dict, attrs: Dict) -> Optional[Self]:
187187
"""Instantiate this class, along with any platform-specific data creation.
188188
189189
This method is not meant to be subclassed, users should redefine create() instead.
@@ -201,7 +201,7 @@ def create_base(cls, diffsync: "DiffSync", ids: Dict, attrs: Dict) -> Optional[S
201201
return model
202202

203203
@classmethod
204-
def create(cls, diffsync: "DiffSync", ids: Dict, attrs: Dict) -> Optional[Self]:
204+
def create(cls, diffsync: "Adapter", ids: Dict, attrs: Dict) -> Optional[Self]:
205205
"""Instantiate this class, along with any platform-specific data creation.
206206
207207
Subclasses must call `super().create()` or `self.create_base()`; they may wish to then override the default status information
@@ -402,7 +402,7 @@ def remove_child(self, child: "DiffSyncModel") -> None:
402402
childs.remove(child.get_unique_id())
403403

404404

405-
class DiffSync: # pylint: disable=too-many-public-methods
405+
class Adapter: # pylint: disable=too-many-public-methods
406406
"""Class for storing a group of DiffSyncModel instances and diffing/synchronizing to another DiffSync instance."""
407407

408408
# In any subclass, you would add mapping of names to specific model classes here:
@@ -535,7 +535,7 @@ def load_from_dict(self, data: Dict) -> None:
535535

536536
def sync_from( # pylint: disable=too-many-arguments
537537
self,
538-
source: "DiffSync",
538+
source: "Adapter",
539539
diff_class: Type[Diff] = Diff,
540540
flags: DiffSyncFlags = DiffSyncFlags.NONE,
541541
callback: Optional[Callable[[StrType, int, int], None]] = None,
@@ -573,7 +573,7 @@ def sync_from( # pylint: disable=too-many-arguments
573573

574574
def sync_to( # pylint: disable=too-many-arguments
575575
self,
576-
target: "DiffSync",
576+
target: "Adapter",
577577
diff_class: Type[Diff] = Diff,
578578
flags: DiffSyncFlags = DiffSyncFlags.NONE,
579579
callback: Optional[Callable[[StrType, int, int], None]] = None,
@@ -597,7 +597,7 @@ def sync_to( # pylint: disable=too-many-arguments
597597

598598
def sync_complete(
599599
self,
600-
source: "DiffSync",
600+
source: "Adapter",
601601
diff: Diff,
602602
flags: DiffSyncFlags = DiffSyncFlags.NONE,
603603
logger: Optional[structlog.BoundLogger] = None,
@@ -623,7 +623,7 @@ def sync_complete(
623623

624624
def diff_from(
625625
self,
626-
source: "DiffSync",
626+
source: "Adapter",
627627
diff_class: Type[Diff] = Diff,
628628
flags: DiffSyncFlags = DiffSyncFlags.NONE,
629629
callback: Optional[Callable[[StrType, int, int], None]] = None,
@@ -644,7 +644,7 @@ def diff_from(
644644

645645
def diff_to(
646646
self,
647-
target: "DiffSync",
647+
target: "Adapter",
648648
diff_class: Type[Diff] = Diff,
649649
flags: DiffSyncFlags = DiffSyncFlags.NONE,
650650
callback: Optional[Callable[[StrType, int, int], None]] = None,
@@ -854,5 +854,8 @@ def count(self, model: Union[StrType, "DiffSyncModel", Type["DiffSyncModel"], No
854854
return self.store.count(model=model)
855855

856856

857+
# For backwards-compatibility, keep around the old name
858+
DiffSync = Adapter
859+
857860
# DiffSyncModel references DiffSync and DiffSync references DiffSyncModel. Break the typing loop:
858861
DiffSyncModel.update_forward_refs()

diffsync/helpers.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
if TYPE_CHECKING: # pragma: no cover
2828
# For type annotation purposes, we have a circular import loop between __init__.py and this file.
29-
from . import DiffSync, DiffSyncModel # pylint: disable=cyclic-import
29+
from . import Adapter, DiffSyncModel # pylint: disable=cyclic-import
3030

3131

3232
class DiffSyncDiffer: # pylint: disable=too-many-instance-attributes
@@ -37,8 +37,8 @@ class DiffSyncDiffer: # pylint: disable=too-many-instance-attributes
3737

3838
def __init__( # pylint: disable=too-many-arguments
3939
self,
40-
src_diffsync: "DiffSync",
41-
dst_diffsync: "DiffSync",
40+
src_diffsync: "Adapter",
41+
dst_diffsync: "Adapter",
4242
flags: DiffSyncFlags,
4343
diff_class: Type[Diff] = Diff,
4444
callback: Optional[Callable[[str, int, int], None]] = None,
@@ -288,8 +288,8 @@ class DiffSyncSyncer: # pylint: disable=too-many-instance-attributes
288288
def __init__( # pylint: disable=too-many-arguments
289289
self,
290290
diff: Diff,
291-
src_diffsync: "DiffSync",
292-
dst_diffsync: "DiffSync",
291+
src_diffsync: "Adapter",
292+
dst_diffsync: "Adapter",
293293
flags: DiffSyncFlags,
294294
callback: Optional[Callable[[str, int, int], None]] = None,
295295
):

diffsync/store/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
if TYPE_CHECKING:
88
from diffsync import DiffSyncModel
9-
from diffsync import DiffSync
9+
from diffsync import Adapter
1010

1111

1212
class BaseStore:
@@ -15,7 +15,7 @@ class BaseStore:
1515
def __init__(
1616
self, # pylint: disable=unused-argument
1717
*args: Any, # pylint: disable=unused-argument
18-
diffsync: Optional["DiffSync"] = None,
18+
diffsync: Optional["Adapter"] = None,
1919
name: str = "",
2020
**kwargs: Any, # pylint: disable=unused-argument
2121
) -> None:

docs/source/core_engine/01-flags.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ diff = nautobot.diff_from(local, flags=flags)
3434
Model flags are stored in the attribute `model_flags` of each model and are usually set when the data is being loaded into the adapter.
3535

3636
```python
37-
from diffsync import DiffSync
37+
from diffsync import Adapter
3838
from diffsync.enum import DiffSyncModelFlags
3939
from model import MyDeviceModel
4040

41-
class MyAdapter(DiffSync):
4241

42+
class MyAdapter(Adapter):
4343
device = MyDeviceModel
4444

4545
def load(self, data):

docs/source/core_engine/03-store.md

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ The `store` is a class attribute in the `DiffSync` class, but all the store oper
99
When you initialize the `Diffsync` Adapter class, there is an optional keyed-argument, `internal_storage_engine`, defaulting to the `LocalStore` class.
1010

1111
```python
12-
>>> from diffsync import DiffSync
13-
>>> adapter = DiffSync()
14-
>>> type(adapter.store)
15-
<class 'diffsync.store.local.LocalStore'>
12+
>> > from diffsync import Adapter
13+
>> > adapter = Adapter()
14+
>> > type(adapter.store)
15+
<
16+
17+
class 'diffsync.store.local.LocalStore'>
1618
```
1719

1820
## Use the `RedisStore` Backend
@@ -22,12 +24,14 @@ To get it, you have to install diffsync package with the "redis" extra option: `
2224
The `RedisStore` backend, as the name suggests, connects to an external Redis service, to store data loaded by the `DiffSync` tasks. The biggest change is that it requires to initialize the Redis store class, before using it in the `DiffSync` adapter class.
2325

2426
```python
25-
>>> from diffsync import DiffSync
26-
>>> from diffsync.store.redis import RedisStore
27-
>>> store = RedisStore(host="redis host")
28-
>>> adapter = DiffSync(internal_storage_engine=store)
29-
>>> type(adapter.store)
30-
<class 'diffsync.store.local.RedisStore'>
27+
>> > from diffsync import Adapter
28+
>> > from diffsync.store.redis import RedisStore
29+
>> > store = RedisStore(host="redis host")
30+
>> > adapter = Adapter(internal_storage_engine=store)
31+
>> > type(adapter.store)
32+
<
33+
34+
class 'diffsync.store.local.RedisStore'>
3135
```
3236

3337
Notice that the `RedisStore` will validate, when initialized, that there is a reachability to the Redis host, and if not, will raise an exception:

docs/source/getting_started/01-getting-started.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ Currently the relationships between models are very loose by design. Instead of
4444
A `DiffSync` "adapter" subclass must reference each model available at the top of the object by its modelname and must have a `top_level` attribute defined to indicate how the diff and the synchronization should be done. In the example below, `"site"` is the only top level object so the synchronization engine will only check all known `Site` instances and all children of each Site. In this case, as shown in the code above, `Device`s are children of `Site`s, so this is exactly the intended logic.
4545

4646
```python
47-
from diffsync import DiffSync
47+
from diffsync import Adapter
4848

49-
class BackendA(DiffSync):
5049

50+
class BackendA(Adapter):
5151
site = Site
5252
device = Device
5353

examples/01-multiple-data-sources/backend_a.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"""
1717

1818
# pylint: disable=wrong-import-order
19-
from diffsync import DiffSync
19+
from diffsync import Adapter
2020
from models import Site, Device, Interface # pylint: disable=no-name-in-module
2121

2222
DATA = {
@@ -31,7 +31,7 @@
3131
}
3232

3333

34-
class BackendA(DiffSync):
34+
class BackendA(Adapter):
3535
"""Example of a DiffSync adapter implementation."""
3636

3737
site = Site

examples/01-multiple-data-sources/backend_b.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"""
1717

1818
# pylint: disable=wrong-import-order
19-
from diffsync import DiffSync
19+
from diffsync import Adapter
2020
from models import Site, Device, Interface # pylint: disable=no-name-in-module
2121

2222
DATA = {
@@ -35,7 +35,7 @@
3535
}
3636

3737

38-
class BackendB(DiffSync):
38+
class BackendB(Adapter):
3939
"""Example of a DiffSync adapter implementation."""
4040

4141
site = Site

examples/01-multiple-data-sources/backend_c.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"""
1717

1818
# pylint: disable=wrong-import-order
19-
from diffsync import DiffSync
19+
from diffsync import Adapter
2020
from models import Site, Device, Interface # pylint: disable=no-name-in-module
2121

2222
DATA = {
@@ -31,7 +31,7 @@
3131
}
3232

3333

34-
class BackendC(DiffSync):
34+
class BackendC(Adapter):
3535
"""Example of a DiffSync adapter implementation."""
3636

3737
site = Site

examples/02-callback-function/main.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"""
1818
import random
1919

20-
from diffsync import DiffSync, DiffSyncModel
20+
from diffsync import Adapter, DiffSyncModel
2121
from diffsync.logging import enable_console_logging
2222

2323

@@ -30,7 +30,7 @@ class Number(DiffSyncModel):
3030
number: int
3131

3232

33-
class DiffSync1(DiffSync):
33+
class Adapter1(Adapter):
3434
"""DiffSync adapter that contains a number of Numbers constructed in order."""
3535

3636
number = Number
@@ -43,7 +43,7 @@ def load(self, count): # pylint: disable=arguments-differ
4343
self.add(Number(number=(i + 1)))
4444

4545

46-
class DiffSync2(DiffSync):
46+
class Adapter2(Adapter):
4747
"""DiffSync adapter that contains a number of Numbers spread randomly across a range."""
4848

4949
number = Number
@@ -69,11 +69,11 @@ def main():
6969
enable_console_logging(verbosity=0) # Show WARNING and ERROR logs only
7070

7171
# Create a DiffSync1 instance and load it with records numbered 1-100
72-
ds1 = DiffSync1()
72+
ds1 = Adapter1()
7373
ds1.load(count=100)
7474

7575
# Create a DiffSync2 instance and load it with 100 random records in the range 1-200
76-
ds2 = DiffSync2()
76+
ds2 = Adapter2()
7777
ds2.load(count=100)
7878

7979
# Identify and attempt to resolve the differences between the two,

0 commit comments

Comments
 (0)