Skip to content
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

Dev python exercise 1 #10

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open

Dev python exercise 1 #10

wants to merge 4 commits into from

Conversation

gertzakis
Copy link

No description provided.

@pke11y pke11y requested a review from progala August 19, 2022 15:27
def main() -> None:
"""Connect to devices and save the running config to files as backup."""
logging.basicConfig(level=logging.INFO)
devices_info = read_yaml("hosts.yaml")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A lot of frameworks in the networking automation refer to structures like that as inventory so it's good to follow that convention. Naming variables is hard :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed it to inventory. And yes, naming variables is a tough business 😨

try:
logging.info(f"Read data from { file_name }")
with open(file_name, encoding="UTF-8") as f:
yaml_data = yaml.load(f, Loader=FullLoader)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use SafeLoader instead of FullLoader. FullLoader has known vulnerabilities, see yaml/pyyaml#420 for examples.

Only use FullLoader if absolutely trust the data and SafeLoader is not working with your data structure.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright! Thank you :)

dict: data from yaml file
"""
try:
logging.info(f"Read data from { file_name }")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say this is more of a debug level message than info.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed it to .debug().

device_info["username"] = USERNAME
device_info["password"] = PASSWORD
device_output = send_command(
device=device_info, command=COMMANDS[device_info["device_type"]]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.get() is usually safer in situations like this. This also allows you to provide the default value. E.g.

... command=COMMANDS.get(device_info["device_type"], "show running-config")

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree, changed to .get().

Comment on lines 47 to 50
file_dir = Path(file_name).parent.absolute()
if not file_dir.exists():
logging.info(f"Creating directory { file_dir } for backups")
file_dir.mkdir(parents=True)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be probably moved into it's own function called once during the setup.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was my initial approach and changed it, changed it back to have it's own function.

logging.info(f"Read data from { file_name }")
with open(file_name, encoding="UTF-8") as f:
yaml_data = yaml.load(f, Loader=FullLoader)
except IOError as err:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Catch OSError instead. IOError was aliased to OSError starting wtih Python3.3.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, didn't know that tbh.

Comment on lines 47 to 50
file_dir = Path(file_name).parent.absolute()
if not file_dir.exists():
logging.info(f"Creating directory { file_dir } for backups")
file_dir.mkdir(parents=True)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can user mkdir with exist_ok=True if you want to avoid the if check.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, sounds better.

@gertzakis gertzakis requested a review from progala August 24, 2022 13:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants