forked from netbox-community/Device-Type-Library-Import
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog_handler.py
More file actions
53 lines (44 loc) · 1.97 KB
/
Copy pathlog_handler.py
File metadata and controls
53 lines (44 loc) · 1.97 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
from sys import exit as system_exit
class LogHandler:
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)
def verbose_log(self, message):
if self.args.verbose:
print(message)
@staticmethod
def log(message):
print(message)
def log_device_ports_created(self, created_ports: list | None = None, port_type: str = "port"):
if created_ports is None:
created_ports = []
for port in created_ports:
self.verbose_log(
f"{port_type} Template Created: {port.name} - "
f"{port.type if hasattr(port, 'type') else ''} - {port.device_type.id} - "
f"{port.id}"
)
return len(created_ports)
def log_module_ports_created(self, created_ports: list | None = None, port_type: str = "port"):
if created_ports is None:
created_ports = []
for port in created_ports:
self.verbose_log(
f"{port_type} Template Created: {port.name} - "
f"{port.type if hasattr(port, 'type') else ''} - {port.module_type.id} - "
f"{port.id}"
)
return len(created_ports)