From 1d861153818d60a4f658742f8bf9ac7dfe00ab88 Mon Sep 17 00:00:00 2001 From: "Daniel W. Anner" Date: Fri, 3 Mar 2023 20:06:52 +0000 Subject: [PATCH 1/5] - Added exception handler function with easy map dictionary. - Added extra default values for ENV vars. - Moved parser args to Settings for global use. - Started implementing new exception handler - Moved git functions to gitcmd.py. - Implemented exception handler for git functions. - Removed extra imports where no longer needed. --- gitcmd.py | 24 ++++++++++++++++++++ nb-dt-import.py | 59 +++++++++++-------------------------------------- settings.py | 44 +++++++++++++++++++++++++++++++----- 3 files changed, 76 insertions(+), 51 deletions(-) create mode 100644 gitcmd.py diff --git a/gitcmd.py b/gitcmd.py new file mode 100644 index 00000000..172c1588 --- /dev/null +++ b/gitcmd.py @@ -0,0 +1,24 @@ +from git import Repo, exc +from sys import exit as system_exit +import settings + +def pull_repo(path: str, branch: str): + try: + repo = Repo(path) + if not repo.remotes.origin.url.endswith('.git'): + settings.handle_exception("GitInvalidRepositoryError", repo.remotes.origin.url, f"Origin URL {repo.remotes.origin.url} does not end with .git") + repo.remotes.origin.pull() + repo.git.checkout(branch) + print(f"Pulled Repo {repo.remotes.origin.url}") + except exc.GitCommandError as git_error: + settings.handle_exception("GitCommandError", repo.remotes.origin.url, git_error) + except Exception as git_error: + settings.handle_exception("Exception", 'Git Repository Error', git_error) + +def clone_repo(url: str, clone_path: str, branch: str): + try: + return Repo.clone_from(url, clone_path, branch=branch) + except exc.GitCommandError as git_error: + settings.handle_exception("GitCommandError", url, git_error) + except Exception as git_error: + settings.handle_exception("Exception", 'Git Repository Error', git_error) diff --git a/nb-dt-import.py b/nb-dt-import.py index f0ef8e16..63fc0a96 100755 --- a/nb-dt-import.py +++ b/nb-dt-import.py @@ -5,13 +5,12 @@ import yaml import pynetbox import glob -import argparse import os -import settings -import sys import re import requests +import settings +import gitcmd as git counter = Counter( added=0, @@ -44,15 +43,7 @@ def determine_features(nb): if nb_ver[0] > 3 or (nb_ver[0] == 3 and nb_ver[1] >= 2): settings.NETBOX_FEATURES['modules'] = True -def update_package(path: str, branch: str): - try: - repo = Repo(path) - if repo.remotes.origin.url.endswith('.git'): - repo.remotes.origin.pull() - repo.git.checkout(branch) - print(f"Pulled Repo {repo.remotes.origin.url}") - except exc.InvalidGitRepositoryError: - pass + def slugFormat(name): @@ -769,25 +760,7 @@ def main(): cwd = os.getcwd() startTime = datetime.now() - - VENDORS = settings.VENDORS - REPO_URL = settings.REPO_URL - - SLUGS = settings.SLUGS - REPO_BRANCH = settings.REPO_BRANCH - - parser = argparse.ArgumentParser(description='Import Netbox Device Types') - parser.add_argument('--vendors', nargs='+', default=VENDORS, - help="List of vendors to import eg. apc cisco") - parser.add_argument('--url', '--git', default=REPO_URL, - help="Git URL with valid Device Type YAML files") - parser.add_argument('--slugs', nargs='+', default=SLUGS, - help="List of device-type slugs to import eg. ap4431 ws-c3850-24t-l") - parser.add_argument('--branch', default=REPO_BRANCH, - help="Git branch to use from repo") - parser.add_argument('--verbose', action='store_true', - help="Print verbose output") - args = parser.parse_args() + args = settings.args nbUrl = settings.NETBOX_URL nbToken = settings.NETBOX_TOKEN @@ -795,27 +768,21 @@ def main(): try: determine_features(nb) - except requests.exceptions.SSLError as e: - if args.verbose: - print(e) + except requests.exceptions.SSLError as ssl_exception: if not settings.IGNORE_SSL_ERRORS: - print("IGNORE_SSL_ERRORS is False. SSL verification failed, exiting.") - sys.exit(1) + settings.handle_exception("SSLError", settings.IGNORE_SSL_ERRORS, ssl_exception) print("IGNORE_SSL_ERRORS is True, catching exception and disabling SSL verification.") requests.packages.urllib3.disable_warnings() nb.http_session.verify = False determine_features(nb) - try: - if os.path.isdir('./repo'): - print(f"Package devicetype-library is already installed, " - + f"updating {os.path.join(cwd, 'repo')}") - update_package('./repo', branch=args.branch) - else: - repo = Repo.clone_from(args.url, os.path.join(cwd, 'repo'), branch=args.branch) - print(f"Package Installed {repo.remotes.origin.url}") - except exc.GitCommandError as error: - print("Couldn't clone {} ({})".format(args.url, error)) + if os.path.isdir(settings.REPO_PATH): + print(f"Package devicetype-library is already installed, " + + f"updating {os.path.join(cwd, 'repo')}") + git.pull_repo(settings.REPO_PATH, branch=args.branch) + else: + repo = git.clone_repo(args.url, os.path.join(cwd, 'repo'), args.branch) + print(f"Package Installed {repo.remotes.origin.url}") if not args.vendors: print("No Vendors Specified, Gathering All Device-Types") diff --git a/settings.py b/settings.py index bcbb8553..5161b4da 100644 --- a/settings.py +++ b/settings.py @@ -1,12 +1,32 @@ +from argparse import ArgumentParser +from sys import exit as system_exit import os + from dotenv import load_dotenv load_dotenv() -REPO_URL = os.getenv("REPO_URL") -REPO_BRANCH = os.getenv("REPO_BRANCH", "master") + +def handle_exception(exception_type, exception, stack_trace=None): + exception_dict = { + "EnvironmentError": f'Environment variable "{exception}" is not set.', + "SSLError": f'SSL verification failed. IGNORE_SSL_ERRORS is {exception}. EXITING.', + "GitCommandError": f'The repo "{exception}" is not a valid git repo.', + "GitInvalidRepositoryError": f'The repo "{exception}" is not a valid git repo.', + "Exception": f'An unknown error occurred: "{exception}"' + } + if args.verbose and stack_trace: + print(stack_trace) + print(exception_dict[exception_type]) + system_exit(1) + + +REPO_URL = os.getenv("REPO_URL", + default="https://github.com/netbox-community/devicetype-library.git") +REPO_BRANCH = os.getenv("REPO_BRANCH", default="master") NETBOX_URL = os.getenv("NETBOX_URL") NETBOX_TOKEN = os.getenv("NETBOX_TOKEN") -IGNORE_SSL_ERRORS = (os.getenv("IGNORE_SSL_ERRORS", "False") == "True") +IGNORE_SSL_ERRORS = (os.getenv("IGNORE_SSL_ERRORS", default="False") == "True") +REPO_PATH = "./repo" # optionally load vendors through a comma separated list as env var VENDORS = list(filter(None, os.getenv("VENDORS", "").split(","))) @@ -18,8 +38,22 @@ 'modules': False, } -MANDATORY_ENV_VARS = ["REPO_URL", "NETBOX_URL", "NETBOX_TOKEN"] +parser = ArgumentParser(description='Import Netbox Device Types') +parser.add_argument('--vendors', nargs='+', default=VENDORS, + help="List of vendors to import eg. apc cisco") +parser.add_argument('--url', '--git', default=REPO_URL, + help="Git URL with valid Device Type YAML files") +parser.add_argument('--slugs', nargs='+', default=SLUGS, + help="List of device-type slugs to import eg. ap4431 ws-c3850-24t-l") +parser.add_argument('--branch', default=REPO_BRANCH, + help="Git branch to use from repo") +parser.add_argument('--verbose', action='store_true', + help="Print verbose output") + +args = parser.parse_args() +# Evaluate environment variables and exit if one of the mandatory ones are not set +MANDATORY_ENV_VARS = ["REPO_URL", "NETBOX_URL", "NETBOX_TOKEN"] for var in MANDATORY_ENV_VARS: if var not in os.environ: - raise EnvironmentError("Failed because {} is not set.".format(var)) + handle_exception("EnvironmentError", var, f'Environment variable "{var}" is not set.\n\nMANDATORY_ENV_VARS: {str(MANDATORY_ENV_VARS)}.\n\nCURRENT_ENV_VARS: {str(os.environ)}') From f33a246c5063fba032ad812828a5c2ed2eee7d55 Mon Sep 17 00:00:00 2001 From: "Daniel W. Anner" Date: Fri, 3 Mar 2023 20:15:23 +0000 Subject: [PATCH 2/5] Added how to fix ssl error in case it pops up --- settings.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/settings.py b/settings.py index 5161b4da..04ca8d7d 100644 --- a/settings.py +++ b/settings.py @@ -9,7 +9,7 @@ def handle_exception(exception_type, exception, stack_trace=None): exception_dict = { "EnvironmentError": f'Environment variable "{exception}" is not set.', - "SSLError": f'SSL verification failed. IGNORE_SSL_ERRORS is {exception}. EXITING.', + "SSLError": f'SSL verification failed. IGNORE_SSL_ERRORS is {exception}. Set IGNORE_SSL_ERRORS to True if you want to ignore this error. EXITING.', "GitCommandError": f'The repo "{exception}" is not a valid git repo.', "GitInvalidRepositoryError": f'The repo "{exception}" is not a valid git repo.', "Exception": f'An unknown error occurred: "{exception}"' From fb08e2f65edb6fce729f8511aae0f8997d37e64f Mon Sep 17 00:00:00 2001 From: "Daniel W. Anner" Date: Fri, 3 Mar 2023 20:49:18 +0000 Subject: [PATCH 3/5] - Removed circular import of settings.py and gitcmd.py - Added exception_handler.py to handle exceptions (refer to above) - Made GitCMD a class with init and run methods - Removed git completely from nb-dt-import.py --- exception_handler.py | 14 ++++++++++ gitcmd.py | 62 ++++++++++++++++++++++++++++++-------------- nb-dt-import.py | 10 ------- settings.py | 22 ++++------------ 4 files changed, 61 insertions(+), 47 deletions(-) create mode 100644 exception_handler.py diff --git a/exception_handler.py b/exception_handler.py new file mode 100644 index 00000000..10c5c163 --- /dev/null +++ b/exception_handler.py @@ -0,0 +1,14 @@ +from sys import exit as system_exit + +def handle_exception(args, exception_type, exception, stack_trace=None): + exception_dict = { + "EnvironmentError": f'Environment variable "{exception}" is not set.', + "SSLError": f'SSL verification failed. IGNORE_SSL_ERRORS is {exception}. Set IGNORE_SSL_ERRORS to True if you want to ignore this error. EXITING.', + "GitCommandError": f'The repo "{exception}" is not a valid git repo.', + "GitInvalidRepositoryError": f'The repo "{exception}" is not a valid git repo.', + "Exception": f'An unknown error occurred: "{exception}"' + } + if args.verbose and stack_trace: + print(stack_trace) + print(exception_dict[exception_type]) + system_exit(1) \ No newline at end of file diff --git a/gitcmd.py b/gitcmd.py index 172c1588..226bf1ec 100644 --- a/gitcmd.py +++ b/gitcmd.py @@ -1,24 +1,46 @@ from git import Repo, exc from sys import exit as system_exit import settings +import os + +class GitCMD: + def __new__(cls, *args, **kwargs): + return super().__new__(cls) + + def __init__(self, args, repo_path): + self.url = args.url + self.repo_path = repo_path + self.branch = args.branch + self.repo = Repo() + self.cwd = os.getcwd() + + if os.path.isdir(self.repo_path): + self.pull_repo() + else: + self.clone_repo() + + + def pull_repo(self): + try: + print("Package devicetype-library is already installed, " + + f"updating {os.path.join(self.cwd, self.repo_path)}") + self.repo = Repo(self.repo_path) + if not self.repo.remotes.origin.url.endswith('.git'): + settings.handle_exception("GitInvalidRepositoryError", self.repo.remotes.origin.url, f"Origin URL {self.repo.remotes.origin.url} does not end with .git") + self.repo.remotes.origin.pull() + self.repo.git.checkout(self.branch) + print(f"Pulled Repo {self.repo.remotes.origin.url}") + except exc.GitCommandError as git_error: + settings.handle_exception("GitCommandError", self.repo.remotes.origin.url, git_error) + except Exception as git_error: + settings.handle_exception("Exception", 'Git Repository Error', git_error) + + def clone_repo(self): + try: + self.repo = Repo.clone_from(self.url, os.path.join(self.cwd, self.repo_path), branch=self.branch) + print(f"Package Installed {self.repo.remotes.origin.url}") + except exc.GitCommandError as git_error: + settings.handle_exception("GitCommandError", self.url, git_error) + except Exception as git_error: + settings.handle_exception("Exception", 'Git Repository Error', git_error) -def pull_repo(path: str, branch: str): - try: - repo = Repo(path) - if not repo.remotes.origin.url.endswith('.git'): - settings.handle_exception("GitInvalidRepositoryError", repo.remotes.origin.url, f"Origin URL {repo.remotes.origin.url} does not end with .git") - repo.remotes.origin.pull() - repo.git.checkout(branch) - print(f"Pulled Repo {repo.remotes.origin.url}") - except exc.GitCommandError as git_error: - settings.handle_exception("GitCommandError", repo.remotes.origin.url, git_error) - except Exception as git_error: - settings.handle_exception("Exception", 'Git Repository Error', git_error) - -def clone_repo(url: str, clone_path: str, branch: str): - try: - return Repo.clone_from(url, clone_path, branch=branch) - except exc.GitCommandError as git_error: - settings.handle_exception("GitCommandError", url, git_error) - except Exception as git_error: - settings.handle_exception("Exception", 'Git Repository Error', git_error) diff --git a/nb-dt-import.py b/nb-dt-import.py index 63fc0a96..81969520 100755 --- a/nb-dt-import.py +++ b/nb-dt-import.py @@ -1,5 +1,4 @@ #!/usr/bin/env python3 -from git import Repo, exc, RemoteProgress from collections import Counter from datetime import datetime import yaml @@ -10,7 +9,6 @@ import requests import settings -import gitcmd as git counter = Counter( added=0, @@ -776,14 +774,6 @@ def main(): nb.http_session.verify = False determine_features(nb) - if os.path.isdir(settings.REPO_PATH): - print(f"Package devicetype-library is already installed, " - + f"updating {os.path.join(cwd, 'repo')}") - git.pull_repo(settings.REPO_PATH, branch=args.branch) - else: - repo = git.clone_repo(args.url, os.path.join(cwd, 'repo'), args.branch) - print(f"Package Installed {repo.remotes.origin.url}") - if not args.vendors: print("No Vendors Specified, Gathering All Device-Types") files, vendors = getFiles() diff --git a/settings.py b/settings.py index 04ca8d7d..0b1bd61c 100644 --- a/settings.py +++ b/settings.py @@ -1,25 +1,11 @@ from argparse import ArgumentParser from sys import exit as system_exit import os - +from exception_handler import handle_exception +from gitcmd import GitCMD from dotenv import load_dotenv load_dotenv() - -def handle_exception(exception_type, exception, stack_trace=None): - exception_dict = { - "EnvironmentError": f'Environment variable "{exception}" is not set.', - "SSLError": f'SSL verification failed. IGNORE_SSL_ERRORS is {exception}. Set IGNORE_SSL_ERRORS to True if you want to ignore this error. EXITING.', - "GitCommandError": f'The repo "{exception}" is not a valid git repo.', - "GitInvalidRepositoryError": f'The repo "{exception}" is not a valid git repo.', - "Exception": f'An unknown error occurred: "{exception}"' - } - if args.verbose and stack_trace: - print(stack_trace) - print(exception_dict[exception_type]) - system_exit(1) - - REPO_URL = os.getenv("REPO_URL", default="https://github.com/netbox-community/devicetype-library.git") REPO_BRANCH = os.getenv("REPO_BRANCH", default="master") @@ -56,4 +42,6 @@ def handle_exception(exception_type, exception, stack_trace=None): MANDATORY_ENV_VARS = ["REPO_URL", "NETBOX_URL", "NETBOX_TOKEN"] for var in MANDATORY_ENV_VARS: if var not in os.environ: - handle_exception("EnvironmentError", var, f'Environment variable "{var}" is not set.\n\nMANDATORY_ENV_VARS: {str(MANDATORY_ENV_VARS)}.\n\nCURRENT_ENV_VARS: {str(os.environ)}') + handle_exception(args, "EnvironmentError", var, f'Environment variable "{var}" is not set.\n\nMANDATORY_ENV_VARS: {str(MANDATORY_ENV_VARS)}.\n\nCURRENT_ENV_VARS: {str(os.environ)}') + +git_repo = GitCMD(args, REPO_PATH) From f61421bd1d7e5d0e565f45e4ccd0307290e1b06e Mon Sep 17 00:00:00 2001 From: "Daniel W. Anner" Date: Fri, 3 Mar 2023 20:54:38 +0000 Subject: [PATCH 4/5] Fixed missing arg --- exception_handler.py | 1 + gitcmd.py | 10 +++++----- nb-dt-import.py | 2 +- settings.py | 2 +- 4 files changed, 8 insertions(+), 7 deletions(-) diff --git a/exception_handler.py b/exception_handler.py index 10c5c163..7762339f 100644 --- a/exception_handler.py +++ b/exception_handler.py @@ -8,6 +8,7 @@ def handle_exception(args, exception_type, exception, stack_trace=None): "GitInvalidRepositoryError": f'The repo "{exception}" is not a valid git repo.', "Exception": f'An unknown error occurred: "{exception}"' } + print(args) if args.verbose and stack_trace: print(stack_trace) print(exception_dict[exception_type]) diff --git a/gitcmd.py b/gitcmd.py index 226bf1ec..88cc77fc 100644 --- a/gitcmd.py +++ b/gitcmd.py @@ -26,21 +26,21 @@ def pull_repo(self): + f"updating {os.path.join(self.cwd, self.repo_path)}") self.repo = Repo(self.repo_path) if not self.repo.remotes.origin.url.endswith('.git'): - settings.handle_exception("GitInvalidRepositoryError", self.repo.remotes.origin.url, f"Origin URL {self.repo.remotes.origin.url} does not end with .git") + settings.handle_exception(settings.args, "GitInvalidRepositoryError", self.repo.remotes.origin.url, f"Origin URL {self.repo.remotes.origin.url} does not end with .git") self.repo.remotes.origin.pull() self.repo.git.checkout(self.branch) print(f"Pulled Repo {self.repo.remotes.origin.url}") except exc.GitCommandError as git_error: - settings.handle_exception("GitCommandError", self.repo.remotes.origin.url, git_error) + settings.handle_exception(settings.args, "GitCommandError", self.repo.remotes.origin.url, git_error) except Exception as git_error: - settings.handle_exception("Exception", 'Git Repository Error', git_error) + settings.handle_exception(settings.args, "Exception", 'Git Repository Error', git_error) def clone_repo(self): try: self.repo = Repo.clone_from(self.url, os.path.join(self.cwd, self.repo_path), branch=self.branch) print(f"Package Installed {self.repo.remotes.origin.url}") except exc.GitCommandError as git_error: - settings.handle_exception("GitCommandError", self.url, git_error) + settings.handle_exception(settings.args, "GitCommandError", self.url, git_error) except Exception as git_error: - settings.handle_exception("Exception", 'Git Repository Error', git_error) + settings.handle_exception(settings.args, "Exception", 'Git Repository Error', git_error) diff --git a/nb-dt-import.py b/nb-dt-import.py index 81969520..ebc3376e 100755 --- a/nb-dt-import.py +++ b/nb-dt-import.py @@ -768,7 +768,7 @@ def main(): determine_features(nb) except requests.exceptions.SSLError as ssl_exception: if not settings.IGNORE_SSL_ERRORS: - settings.handle_exception("SSLError", settings.IGNORE_SSL_ERRORS, ssl_exception) + settings.handle_exception(settings.args, "SSLError", settings.IGNORE_SSL_ERRORS, ssl_exception) print("IGNORE_SSL_ERRORS is True, catching exception and disabling SSL verification.") requests.packages.urllib3.disable_warnings() nb.http_session.verify = False diff --git a/settings.py b/settings.py index 0b1bd61c..74d5111c 100644 --- a/settings.py +++ b/settings.py @@ -33,7 +33,7 @@ help="List of device-type slugs to import eg. ap4431 ws-c3850-24t-l") parser.add_argument('--branch', default=REPO_BRANCH, help="Git branch to use from repo") -parser.add_argument('--verbose', action='store_true', +parser.add_argument('--verbose', action='store_true', default=False, help="Print verbose output") args = parser.parse_args() From df602600258342c9c09dad84997edee236874076 Mon Sep 17 00:00:00 2001 From: "Daniel W. Anner" Date: Fri, 3 Mar 2023 21:00:13 +0000 Subject: [PATCH 5/5] made exception handler a class to prevent circular import --- exception_handler.py | 38 +++++++++++++++++++++++++------------- gitcmd.py | 10 +++++----- nb-dt-import.py | 2 +- settings.py | 5 +++-- 4 files changed, 34 insertions(+), 21 deletions(-) diff --git a/exception_handler.py b/exception_handler.py index 7762339f..3bc0da4d 100644 --- a/exception_handler.py +++ b/exception_handler.py @@ -1,15 +1,27 @@ from sys import exit as system_exit -def handle_exception(args, exception_type, exception, stack_trace=None): - exception_dict = { - "EnvironmentError": f'Environment variable "{exception}" is not set.', - "SSLError": f'SSL verification failed. IGNORE_SSL_ERRORS is {exception}. Set IGNORE_SSL_ERRORS to True if you want to ignore this error. EXITING.', - "GitCommandError": f'The repo "{exception}" is not a valid git repo.', - "GitInvalidRepositoryError": f'The repo "{exception}" is not a valid git repo.', - "Exception": f'An unknown error occurred: "{exception}"' - } - print(args) - if args.verbose and stack_trace: - print(stack_trace) - print(exception_dict[exception_type]) - system_exit(1) \ No newline at end of file + + + + +class ExceptionHandler: + def __new__(cls, *args, **kwargs): + return super().__new__(cls) + + def __init__(self, args): + self.args = args + + def exception(self, exception_type, exception, stack_trace=None): + exception_dict = { + "EnvironmentError": f'Environment variable "{exception}" is not set.', + "SSLError": f'SSL verification failed. IGNORE_SSL_ERRORS is {exception}. Set IGNORE_SSL_ERRORS to True if you want to ignore this error. EXITING.', + "GitCommandError": f'The repo "{exception}" is not a valid git repo.', + "GitInvalidRepositoryError": f'The repo "{exception}" is not a valid git repo.', + "Exception": f'An unknown error occurred: "{exception}"' + } + + if self.args.verbose and stack_trace: + print(stack_trace) + print(exception_dict[exception_type]) + system_exit(1) + diff --git a/gitcmd.py b/gitcmd.py index 88cc77fc..0a6e05b7 100644 --- a/gitcmd.py +++ b/gitcmd.py @@ -26,21 +26,21 @@ def pull_repo(self): + f"updating {os.path.join(self.cwd, self.repo_path)}") self.repo = Repo(self.repo_path) if not self.repo.remotes.origin.url.endswith('.git'): - settings.handle_exception(settings.args, "GitInvalidRepositoryError", self.repo.remotes.origin.url, f"Origin URL {self.repo.remotes.origin.url} does not end with .git") + settings.handle.exception("GitInvalidRepositoryError", self.repo.remotes.origin.url, f"Origin URL {self.repo.remotes.origin.url} does not end with .git") self.repo.remotes.origin.pull() self.repo.git.checkout(self.branch) print(f"Pulled Repo {self.repo.remotes.origin.url}") except exc.GitCommandError as git_error: - settings.handle_exception(settings.args, "GitCommandError", self.repo.remotes.origin.url, git_error) + settings.handle.exception("GitCommandError", self.repo.remotes.origin.url, git_error) except Exception as git_error: - settings.handle_exception(settings.args, "Exception", 'Git Repository Error', git_error) + settings.handle.exception("Exception", 'Git Repository Error', git_error) def clone_repo(self): try: self.repo = Repo.clone_from(self.url, os.path.join(self.cwd, self.repo_path), branch=self.branch) print(f"Package Installed {self.repo.remotes.origin.url}") except exc.GitCommandError as git_error: - settings.handle_exception(settings.args, "GitCommandError", self.url, git_error) + settings.handle.exception("GitCommandError", self.url, git_error) except Exception as git_error: - settings.handle_exception(settings.args, "Exception", 'Git Repository Error', git_error) + settings.handle.exception("Exception", 'Git Repository Error', git_error) diff --git a/nb-dt-import.py b/nb-dt-import.py index ebc3376e..7cfa93d0 100755 --- a/nb-dt-import.py +++ b/nb-dt-import.py @@ -768,7 +768,7 @@ def main(): determine_features(nb) except requests.exceptions.SSLError as ssl_exception: if not settings.IGNORE_SSL_ERRORS: - settings.handle_exception(settings.args, "SSLError", settings.IGNORE_SSL_ERRORS, ssl_exception) + settings.handle.exception("SSLError", settings.IGNORE_SSL_ERRORS, ssl_exception) print("IGNORE_SSL_ERRORS is True, catching exception and disabling SSL verification.") requests.packages.urllib3.disable_warnings() nb.http_session.verify = False diff --git a/settings.py b/settings.py index 74d5111c..4a4ef56b 100644 --- a/settings.py +++ b/settings.py @@ -1,7 +1,7 @@ from argparse import ArgumentParser from sys import exit as system_exit import os -from exception_handler import handle_exception +from exception_handler import ExceptionHandler from gitcmd import GitCMD from dotenv import load_dotenv load_dotenv() @@ -38,10 +38,11 @@ args = parser.parse_args() +handle = ExceptionHandler(args) # Evaluate environment variables and exit if one of the mandatory ones are not set MANDATORY_ENV_VARS = ["REPO_URL", "NETBOX_URL", "NETBOX_TOKEN"] for var in MANDATORY_ENV_VARS: if var not in os.environ: - handle_exception(args, "EnvironmentError", var, f'Environment variable "{var}" is not set.\n\nMANDATORY_ENV_VARS: {str(MANDATORY_ENV_VARS)}.\n\nCURRENT_ENV_VARS: {str(os.environ)}') + handle.exception("EnvironmentError", var, f'Environment variable "{var}" is not set.\n\nMANDATORY_ENV_VARS: {str(MANDATORY_ENV_VARS)}.\n\nCURRENT_ENV_VARS: {str(os.environ)}') git_repo = GitCMD(args, REPO_PATH)