Skip to content

Commit

Permalink
Pylint and pydocstyle
Browse files Browse the repository at this point in the history
  • Loading branch information
dgarros committed Oct 11, 2021
1 parent 3912bf1 commit 9f56d1f
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 15 deletions.
1 change: 1 addition & 0 deletions examples/example3/diff.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""Custom Diff class for DiffSync to influence the behavior of the core Engine."""
from diffsync.diff import Diff


Expand Down
7 changes: 4 additions & 3 deletions examples/example3/local_adapter.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
"""DiffSync adapter to load data from a local file."""
import json

from slugify import slugify

from diffsync import DiffSync
from models import Region, Country
from diffsync import DiffSync


COUNTRIES_FILE = "countries.json"

Expand All @@ -25,14 +27,13 @@ class LocalAdapter(DiffSync):

def load(self, filename=COUNTRIES_FILE):
"""Load all regions and countries from a local JSON file."""

data_file = open(filename, "r")
countries = json.loads(data_file.read())

# Load all regions first
# A Region object will be create for each region and it will be store inside the object with self.add
# To create a Region we are using "self.region" instead of "Region" directly to allow someone to extend this adapter without redefining everything.
region_names = set([country.get("region") for country in countries])
region_names = {country.get("region") for country in countries}
for region in region_names:
self.add(self.region(slug=slugify(region), name=region))

Expand Down
2 changes: 0 additions & 2 deletions examples/example3/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
"""Main executable for DiffSync "example2"."""
import sys
import argparse
import pprint

from diffsync import Diff
from diffsync.enum import DiffSyncFlags
from diffsync.logging import enable_console_logging

Expand Down
1 change: 1 addition & 0 deletions examples/example3/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""Main DiffSync models for example3."""
from typing import List, Optional
from diffsync import DiffSyncModel

Expand Down
12 changes: 7 additions & 5 deletions examples/example3/nautobot_adapter.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
"""DiffSync Adapter for Nautobot to manage regions."""
import os
import pynautobot

from nautobot_models import NautobotCountry, NautobotRegion

from diffsync import DiffSync

from nautobot_models import NautobotCountry, NautobotRegion

NAUTOBOT_URL = os.getenv("NAUTOBOT_URL", "https://demo.nautobot.com")
NAUTOBOT_TOKEN = os.getenv("NAUTOBOT_TOKEN", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
Expand Down Expand Up @@ -40,10 +42,11 @@ class NautobotAdapter(DiffSync):

def load(self):
"""Load all data from Nautobot into the internal cache after transformation."""

# Initialize pynautobot to interact with Nautobot and store it within the adapter
# to reuse it later
self.nautobot = pynautobot.api(url=NAUTOBOT_URL, token=NAUTOBOT_TOKEN)
self.nautobot = pynautobot.api(
url=NAUTOBOT_URL, token=NAUTOBOT_TOKEN
) # pylint: disable=attribute-defined-outside-init

# Pull all regions from Nautobot, which includes all regions and all countries
regions = self.nautobot.dcim.regions.all()
Expand Down Expand Up @@ -75,10 +78,9 @@ def load(self):

def sync_from(self, *args, **kwargs):
"""Sync the data with Nautobot but first ensure that all the required Custom fields are present in Nautobot."""

# Check if all required custom fields exist, create them if they don't
for custom_field in CUSTOM_FIELDS:
nb_cfs = self.cfs = self.nautobot.extras.custom_fields.filter(name=custom_field.get("name"))
nb_cfs = self.nautobot.extras.custom_fields.filter(name=custom_field.get("name"))
if not nb_cfs:
self.nautobot.extras.custom_fields.create(**custom_field)

Expand Down
7 changes: 2 additions & 5 deletions examples/example3/nautobot_models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import os
"""Extension of the Base model for the Nautobot DiffSync Adapter to manage the CRUD operations."""
import pynautobot


from diffsync import DiffSync
from models import Region, Country

Expand All @@ -18,7 +17,7 @@ class NautobotRegion(Region):

class NautobotCountry(Country):
"""Extend the Country to manage Country in Nautobot. CREATE/UPDATE/DELETE.
Country are represented in Nautobot as a dcim.region object as well but a country must have a parent.
Subregion information will be store in the description of the object in Nautobot
"""
Expand All @@ -38,7 +37,6 @@ def create(cls, diffsync: DiffSync, ids: dict, attrs: dict):
Returns:
NautobotCountry: DiffSync object newly created
"""

# Retrieve the parent region in internal cache to access its UUID
# because the UUID is required to associate the object to its parent region in Nautobot
region = diffsync.get(diffsync.region, attrs.get("region"))
Expand Down Expand Up @@ -75,7 +73,6 @@ def update(self, attrs: dict):
Raises:
ObjectNotUpdated: if an error occurred.
"""

# Retrive the pynautobot object from Nautobot since we only have the UUID internally
remote = self.diffsync.nautobot.dcim.regions.get(self.remote_id)

Expand Down

0 comments on commit 9f56d1f

Please sign in to comment.