From 9f56d1ff50b9bcbdb5ee90ecc75c22cff0692bb3 Mon Sep 17 00:00:00 2001 From: Damien Garros Date: Mon, 11 Oct 2021 09:01:24 -0400 Subject: [PATCH] Pylint and pydocstyle --- examples/example3/diff.py | 1 + examples/example3/local_adapter.py | 7 ++++--- examples/example3/main.py | 2 -- examples/example3/models.py | 1 + examples/example3/nautobot_adapter.py | 12 +++++++----- examples/example3/nautobot_models.py | 7 ++----- 6 files changed, 15 insertions(+), 15 deletions(-) diff --git a/examples/example3/diff.py b/examples/example3/diff.py index a7b082e4..13f1c625 100644 --- a/examples/example3/diff.py +++ b/examples/example3/diff.py @@ -1,3 +1,4 @@ +"""Custom Diff class for DiffSync to influence the behavior of the core Engine.""" from diffsync.diff import Diff diff --git a/examples/example3/local_adapter.py b/examples/example3/local_adapter.py index 34679716..e8fbc35b 100644 --- a/examples/example3/local_adapter.py +++ b/examples/example3/local_adapter.py @@ -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" @@ -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)) diff --git a/examples/example3/main.py b/examples/example3/main.py index 7225ead9..77ea97e1 100644 --- a/examples/example3/main.py +++ b/examples/example3/main.py @@ -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 diff --git a/examples/example3/models.py b/examples/example3/models.py index 7d779a46..baa03d0e 100644 --- a/examples/example3/models.py +++ b/examples/example3/models.py @@ -1,3 +1,4 @@ +"""Main DiffSync models for example3.""" from typing import List, Optional from diffsync import DiffSyncModel diff --git a/examples/example3/nautobot_adapter.py b/examples/example3/nautobot_adapter.py index c52a97b9..affb8771 100644 --- a/examples/example3/nautobot_adapter.py +++ b/examples/example3/nautobot_adapter.py @@ -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") @@ -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() @@ -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) diff --git a/examples/example3/nautobot_models.py b/examples/example3/nautobot_models.py index 22af7baa..2a316bbe 100644 --- a/examples/example3/nautobot_models.py +++ b/examples/example3/nautobot_models.py @@ -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 @@ -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 """ @@ -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")) @@ -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)