-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added the format_dict.py module that converts normal csv dictionaries to Netbox friendly dictionaries. #99
Changes from 1 commit
45673fa
e69010d
a105c64
7779a3e
8a5f011
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,21 @@ | ||
import pandas as pd | ||
import pandas | ||
|
||
|
||
class CsvUtils: | ||
""" | ||
This class provides methods to read data from CSV files and allow the data to be easily read and used elsewhere. | ||
""" | ||
@staticmethod | ||
def csv_to_python(file_path: str) -> dict: | ||
def __init__(self): | ||
self.pd = pandas | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ...^ then we don't need this |
||
|
||
def csv_to_python(self, file_path: str) -> dict: | ||
""" | ||
This method reads data from csv files and writes them to a dictionary. | ||
:param file_path: The file path of the CSV file to be read from. | ||
:return: Returns the data from the csv as a dictionary. | ||
""" | ||
with pd.read_csv(file_path) as dataframe: | ||
dataframe = dataframe.to_dict(orient="list") | ||
dataframe = self.pd.read_csv(file_path) | ||
dataframe = dataframe.to_dict(orient="list") | ||
return dataframe | ||
|
||
@staticmethod | ||
|
@@ -24,7 +26,7 @@ def separate_data(data: dict) -> list: | |
:return: Returns a list of dictionaries which each represent a row of data from CSV. | ||
""" | ||
data_keys = list(data.keys()) | ||
len_rows = len(data[data_keys[0]]) - 1 | ||
len_rows = len(data[data_keys[0]]) | ||
dicts = [] | ||
for index in range(len_rows): | ||
new_dict = {} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
from typing import Optional | ||
from Enums.dcim_device_id import DeviceInfoID | ||
from Enums.dcim_device_no_id import DeviceInfoNoID | ||
from Netbox_Api.netbox_connect import NetboxConnect | ||
from operator import attrgetter | ||
|
||
|
||
class FormatDict(NetboxConnect): | ||
meoflynn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def __init__(self, url: str, token: str, dicts: list, api: Optional = None): | ||
""" | ||
This method initialises the class with the following parameters. | ||
Also, it allows dependency injection testing. | ||
:param url: Netbox website URL. | ||
:param token: Netbox authentication token. | ||
:param dicts: A list of dictionaries to format. | ||
""" | ||
if not api: | ||
self.netbox = NetboxConnect(url, token).api_object() | ||
else: | ||
self.netbox = api | ||
self.dicts = dicts | ||
self.enums_id = DeviceInfoID | ||
self.enums_no_id = DeviceInfoNoID | ||
|
||
def iterate_dicts(self) -> list: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the return typing should be uppercase List and Dict, does this apply to argument typing? like: |
||
""" | ||
This method iterates through each dictionary and calls a format method on each. | ||
:return: Returns the formatted dictionaries. | ||
""" | ||
new_dicts = [] | ||
for dictionary in self.dicts: | ||
new_dicts.append(self.format_dict(dictionary)) | ||
return new_dicts | ||
|
||
def format_dict(self, dictionary) -> dict: | ||
""" | ||
This method iterates through each value in the dictionary. | ||
If the value needs to be converted into a Pynetbox ID it calls the .get() method. | ||
:param dictionary: The dictionary to be formatted | ||
:return: Returns the formatted dictionary | ||
""" | ||
for key in dictionary: | ||
if key not in list(self.enums_no_id.__members__): | ||
value = self.get_id(key, dictionary[key], dictionary["site"]) | ||
dictionary[key] = value | ||
return dictionary | ||
|
||
def get_id(self, attr_string: str, netbox_value: str, site_value: str) -> id: | ||
""" | ||
This method uses the Pynetbox Api .get() method to retrieve the ID of a string value from Netbox. | ||
:param attr_string: The attribute string to get. | ||
:param netbox_value: The value to search for in Netbox. | ||
:param site_value: The value of the site key in the dictionary | ||
:return: Returns the value/ID | ||
""" | ||
attr_string = attr_string.upper() | ||
attr_to_look_for = getattr(self.enums_id, attr_string).value # Gets Enums value | ||
value = attrgetter(attr_to_look_for)(self.netbox) # Gets netbox attr | ||
if attr_string == "DEVICE_TYPE": | ||
value = value.get(slug=netbox_value).id | ||
elif attr_string == "LOCATION": | ||
if type(site_value) == int: | ||
site_name = self.netbox.dcim.sites.get(site_value).name | ||
site_slug = site_name.replace(" ", "-").lower() | ||
value = value.get(name=netbox_value, site=site_slug) | ||
else: | ||
value = value.get(name=netbox_value).id | ||
return value | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Format dict should only format dicts This should be in a different class, such as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from enum import Enum | ||
|
||
|
||
class DeviceInfoID(Enum): | ||
meoflynn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
DEVICE_ROLE = "dcim.device_roles" | ||
DESCRIPTION = "dcim.devices" | ||
DEVICE_TYPE = "dcim.device_types" | ||
RACK = "dcim.racks" | ||
LOCATION = "dcim.locations" | ||
TENANT = "tenancy.tenants" | ||
SITE = "dcim.sites" | ||
MANUFACTURER = "dcim.manufacturers" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from enum import Enum | ||
|
||
|
||
class DeviceInfoNoID(Enum): | ||
meoflynn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
position = "position" | ||
name = "name" | ||
serial = "serial" | ||
airflow = "airflow" | ||
status = "status" | ||
face = "face" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
import pandas as pd