Skip to content

Commit

Permalink
STY: Pylint
Browse files Browse the repository at this point in the history
  • Loading branch information
khalford committed Nov 20, 2023
1 parent cec4bcf commit 3bcdbc1
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 21 deletions.
5 changes: 5 additions & 0 deletions pynetbox_query/pynetboxquery/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@


def main():
"""
This function will:
Run the argparse method to collect arguments from the CLI.
Then run the correct user script for the action specified in the CLI.
"""
argparse_args = Parsers().arg_parser()
kwargs = vars(argparse_args)
match kwargs["subparsers"]:
Expand Down
6 changes: 6 additions & 0 deletions pynetbox_query/pynetboxquery/netbox_api/netbox_connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,10 @@


def api_object(url: str, token: str) -> api:
"""
This function returns the Pynetbox Api object used to interact with Netbox.
:param url: The Netbox URL.
:param token: User Api token.
:return: The Pynetbox api object.
"""
return api(url, token)
2 changes: 0 additions & 2 deletions pynetbox_query/pynetboxquery/netbox_api/netbox_create.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
from typing import Union, Dict, List
from dataclasses import asdict
from pynetboxquery.utils.device_dataclass import Device


class NetboxCreate:
Expand Down
22 changes: 15 additions & 7 deletions pynetbox_query/pynetboxquery/netbox_api/validate_data.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import sys
from typing import List
from pynetbox import api
from pynetboxquery.utils.device_dataclass import Device
from pynetboxquery.netbox_api.validate_methods import _ValidateMethods
from pynetbox import api


# Disabling this Pylint warning as it is unnecessary.
# pylint: disable = R0903
class ValidateData(_ValidateMethods):
"""
This class contains methods that check if things exist in Netbox or not.
"""

def validate_data(self, device_list: List[Device], netbox_api: api, fields: List[str]):
def validate_data(
self, device_list: List[Device], netbox_api: api, fields: List[str]
):
"""
This method will take a list of dataclasses.
Validate any data specified by the key word arguments.
Expand All @@ -23,7 +27,9 @@ def validate_data(self, device_list: List[Device], netbox_api: api, fields: List
for result in results:
sys.stdout.write(f"{result}\n")

def _call_validation_methods(self, device_list: List[Device], netbox_api: api, field: str) -> List[str]:
def _call_validation_methods(
self, device_list: List[Device], netbox_api: api, field: str
) -> List[str]:
"""
This method will validate the field data by calling the correct validate method.
:param device_list: List of devices to validate.
Expand All @@ -34,12 +40,14 @@ def _call_validation_methods(self, device_list: List[Device], netbox_api: api, f
match field:
case "name":
device_names = [device.name for device in device_list]
results = self._check_list_device_name_in_netbox(device_names, netbox_api)
results = self._check_list_device_name_in_netbox(
device_names, netbox_api
)
case "device_type":
device_types = [device.device_type for device in device_list]
results = self._check_list_device_type_in_netbox(device_types, netbox_api)
results = self._check_list_device_type_in_netbox(
device_types, netbox_api
)
case _:
results = [f"Could not find a field for the argument: {field}."]
return results


12 changes: 8 additions & 4 deletions pynetbox_query/pynetboxquery/netbox_api/validate_methods.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from typing import List
from pynetboxquery.utils.error_classes import *
from pynetbox import api


# Disabling this Pylint warning, there are too few public methods.
# pylint: disable = R0903
class _ValidateMethods:

@staticmethod
def _check_device_name_in_netbox(device_name: str, netbox_api: api) -> bool:
"""
Expand All @@ -15,7 +15,9 @@ def _check_device_name_in_netbox(device_name: str, netbox_api: api) -> bool:
device = netbox_api.dcim.devices.get(name=device_name)
return bool(device)

def _check_list_device_name_in_netbox(self, device_names: List[str], netbox_api: api) -> List[str]:
def _check_list_device_name_in_netbox(
self, device_names: List[str], netbox_api: api
) -> List[str]:
"""
This method will call the validate method on each device name in the list and return the results.
:param device_names: List of device names to check.
Expand All @@ -37,7 +39,9 @@ def _check_device_type_in_netbox(device_type: str, netbox_api: api) -> bool:
device_type = netbox_api.dcim.device_types.get(slug=device_type)
return bool(device_type)

def _check_list_device_type_in_netbox(self, device_type_list: List[str], netbox_api: api) -> List[str]:
def _check_list_device_type_in_netbox(
self, device_type_list: List[str], netbox_api: api
) -> List[str]:
"""
This method will call the validate method on each device type in the list and return the results.
:param device_names: List of device types to check.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ def upload_devices_to_netbox(url: str, token: str, file_path: str, **kwargs):
"""
api = api_object(url, token)
device_list = ReadData().read(file_path, **kwargs)
ValidateData().validate_data(device_list, api, **{"fields": ["name", "device_type"]})
ValidateData().validate_data(
device_list, api, **{"fields": ["name", "device_type"]}
)
queried_devices = QueryDevice(api).query_list(device_list)
dictionary_devices = [asdict(device) for device in queried_devices]
NetboxCreate(api).create_device(dictionary_devices)
Expand Down
6 changes: 6 additions & 0 deletions pynetbox_query/pynetboxquery/utils/error_classes.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# Disabling this Pylint error as these classes do not need Docstring.
# They are just errors
# pylint: disable = C0115
"""Custom exceptions for the package."""


class DeviceFoundError(Exception):
pass

Expand Down
9 changes: 5 additions & 4 deletions pynetbox_query/pynetboxquery/utils/parsers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import argparse


# Disabling this Pylint warning as it's irrelevant.
# pylint: disable = R0903
class Parsers:
"""
This class contains the argparse methods for different commands.
Expand All @@ -20,7 +22,7 @@ def arg_parser(self):
subparsers = main_parser.add_subparsers(
title="Actions",
description="Valid Subcommands:\ncreate_devices\nvalidate_data_fields_in_netbox",
dest="subparsers"
dest="subparsers",
)
self._create_parser(subparsers, parent_parser)
self._validate_parser(subparsers, parent_parser)
Expand All @@ -42,12 +44,12 @@ def _parent_parser():

@staticmethod
def _create_parser(subparsers, parent_parser):
parser_upload_devices_to_netbox = subparsers.add_parser(
subparsers.add_parser(
"create_devices",
description="Create devices in Netbox from a file.",
usage="pynetboxquery create_devices <filepath> <url> <token> <options>",
parents=[parent_parser],
aliases=["create"]
aliases=["create"],
)

@staticmethod
Expand All @@ -68,4 +70,3 @@ def _validate_parser(subparsers, parent_parser):
help="To include all results or only bad results from Netbox.",
dest="TRUE/FALSE",
)

1 change: 0 additions & 1 deletion pynetbox_query/pynetboxquery/utils/query_device.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from typing import List
from dataclasses import replace
from pynetboxquery.utils.device_dataclass import Device
from pynetboxquery.netbox_api.netbox_get_id import NetboxGetId

Expand Down
12 changes: 10 additions & 2 deletions pynetbox_query/pynetboxquery/utils/read_data.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
from pathlib import Path
from typing import List, Dict
from pynetboxquery.utils.error_classes import *
from pynetboxquery.utils.device_dataclass import Device
from pynetboxquery.utils.error_classes import (
FileTypeNotSupported,
DelimiterNotSpecifiedError,
SheetNameNotSpecifiedError,
)
from pynetboxquery.utils.read_methods import _ReadMethods


# Disabling this pylint warning as it is unnecessary.
# pylint: disable = R0903
class ReadData(_ReadMethods):
"""
This class contains methods to read data from different file types into a list of dictionaries.
Expand All @@ -31,7 +37,9 @@ def read(self, file_path: str, **kwargs) -> List[Device]:
case "csv":
dictionary_list = self._read_csv(file_path)
case "txt":
dictionary_list = self._read_delimited_txt(file_path, kwargs["delimiter"])
dictionary_list = self._read_delimited_txt(
file_path, kwargs["delimiter"]
)
case "xlsx":
dictionary_list = self._read_xlsx(file_path, kwargs["sheet_name"])
case _:
Expand Down
3 changes: 3 additions & 0 deletions pynetbox_query/pynetboxquery/utils/read_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
from pandas import read_excel


# Disabling this pylint warning as it is unnecessary.
# pylint: disable = R0903
class _ReadMethods:
"""
This class provides the read methods for different file types.
All of which accept a file path and return a list of dictionaries.
"""

@staticmethod
def _read_csv(file_path: str) -> List[Dict]:
"""
Expand Down

0 comments on commit 3bcdbc1

Please sign in to comment.