-
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
Conversation
Pynetbox friendly dictionaries
@@ -1,19 +1,21 @@ | |||
import pandas as pd | |||
import pandas |
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
@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 comment
The reason will be displayed to describe this comment to others. Learn more.
...^ then we don't need this
Netbox_CSV_Read/CSV/format_dict.py
Outdated
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 comment
The reason will be displayed to describe this comment to others. Learn more.
This should be List
and Dict
, as list != List
(it's one of those Python gotchas that was only fixed in later versions which we partially have access to)
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.
If the return typing should be uppercase List and Dict, does this apply to argument typing? like:
def func(values: List):
pass
or
def func(values: list):
pass
Netbox_CSV_Read/CSV/format_dict.py
Outdated
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 comment
The 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 NetboxData
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.
👍
…ox api logic from format_dict.py to netbox_data.py.
@@ -1,24 +1,25 @@ | |||
from pynetbox import api | |||
import pynetbox |
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.
Use import pynetbox as nb
""" | ||
self.url = url | ||
self.token = token | ||
self.pnb = pynetbox |
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.
If we use import pynetbox as nb
we would not need to use self.pnb
here
|
||
def api_object(self): | ||
""" | ||
This method returns the Pynetbox Api object. | ||
:return: Returns the Api object | ||
""" | ||
return api(self.url, self.token) | ||
obj = self.pnb.api(self.url, self.token) |
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.
This would then become nb.api(self.url, self.token)
Pynetbox friendly dictionaries