-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathtransform.py
More file actions
executable file
·85 lines (69 loc) · 3.36 KB
/
transform.py
File metadata and controls
executable file
·85 lines (69 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
"""Adds command mapper, platform parsing info."""
import os
import yaml
from django.core.exceptions import MultipleObjectsReturned, ObjectDoesNotExist
from nautobot.extras.models import GitRepository
from nautobot_device_onboarding.constants import (
ONBOARDING_COMMAND_MAPPERS_CONTENT_IDENTIFIER,
ONBOARDING_COMMAND_MAPPERS_REPOSITORY_FOLDER,
)
DATA_DIR = os.path.abspath(os.path.join(os.path.dirname(os.path.dirname(__file__)), "command_mappers"))
def get_git_repo():
"""
Retrieve the Git repository object that contains onboarding command mappers.
Returns:
GitRepository or None: The GitRepository object if found, None if no repository
is found or if multiple repositories match the criteria.
"""
try:
return GitRepository.objects.get(provided_contents__contains=ONBOARDING_COMMAND_MAPPERS_CONTENT_IDENTIFIER)
except (ObjectDoesNotExist, MultipleObjectsReturned):
return None
def get_git_repo_parser_path(parser_type):
"""Get the git repo object."""
repository_record = get_git_repo()
if repository_record:
repo_data_dir = os.path.join(
repository_record.filesystem_path, "onboarding_command_mappers", "parsers", parser_type
)
if os.path.isdir(repo_data_dir):
return repo_data_dir
return None
return None
def add_platform_parsing_info():
"""Merges platform command mapper from repo or defaults."""
repository_record = get_git_repo()
if repository_record:
repo_data_dir = os.path.join(repository_record.filesystem_path, ONBOARDING_COMMAND_MAPPERS_REPOSITORY_FOLDER)
command_mappers_repo_path = load_command_mappers_from_dir(repo_data_dir)
else:
command_mappers_repo_path = {}
command_mapper_defaults = load_command_mappers_from_dir(DATA_DIR)
merged_command_mappers = {**command_mapper_defaults, **command_mappers_repo_path}
return merged_command_mappers
def load_command_mappers_from_dir(command_mappers_path):
"""Helper to load all yaml files in directory and return merged dictionary."""
command_mappers_result = {}
files = [f for f in os.listdir(command_mappers_path) if os.path.isfile(os.path.join(command_mappers_path, f))]
for filename in files:
with open(os.path.join(command_mappers_path, filename), encoding="utf-8") as fd:
network_driver = filename.split(".")[0]
command_mappers_data = yaml.safe_load(fd)
command_mappers_result[network_driver] = command_mappers_data
return command_mappers_result
def load_files_with_precedence(filesystem_dir, parser_type):
"""Utility to load files from filesystem and git repo with precedence."""
file_paths = {}
git_repo_dir = get_git_repo_parser_path(parser_type)
# List files in the first directory and add to the dictionary
if git_repo_dir:
for file_name in os.listdir(git_repo_dir):
file_path = os.path.join(git_repo_dir, file_name)
if os.path.isfile(file_path):
file_paths[file_name] = file_path
# List files in the second directory and add to the dictionary if not already present
for file_name in os.listdir(filesystem_dir):
file_path = os.path.join(filesystem_dir, file_name)
if os.path.isfile(file_path) and file_name not in file_paths:
file_paths[file_name] = file_path
return file_paths