Skip to content

Commit 19c716b

Browse files
author
Juliya Smith
committed
Use dest args dto
1 parent 4becc5c commit 19c716b

File tree

5 files changed

+57
-55
lines changed

5 files changed

+57
-55
lines changed

c42seceventcli/aed/args.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ def verify_username_arg(self):
118118
self._raise_value_error("Code42 username not provided.")
119119

120120
def verify_destination_args(self):
121+
self.destination_type = self.destination_type.lower()
121122
if self.destination_type == "stdout" and self.destination is not None:
122123
msg = (
123124
"Destination '{0}' not applicable for stdout. "

c42seceventcli/aed/main.py

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -58,49 +58,50 @@ def _create_handlers(args):
5858
handlers.handle_error = error_logger.error
5959
output_format = args.output_format
6060
logger_formatter = _get_log_formatter(output_format)
61+
destination_args = _create_destination_args(args)
6162
logger = _get_logger(
62-
formatter=logger_formatter,
63-
service_name=_SERVICE_NAME,
64-
destination=args.destination,
65-
destination_type=args.destination_type,
66-
destination_port=int(args.destination_port),
67-
destination_protocol=args.destination_protocol,
63+
formatter=logger_formatter, service_name=_SERVICE_NAME, destination_args=destination_args
6864
)
6965
handlers.handle_response = _get_response_handler(logger)
7066
return handlers
7167

7268

73-
def _get_logger(
74-
formatter,
75-
service_name,
76-
destination,
77-
destination_type,
78-
destination_port=514,
79-
destination_protocol="TCP",
80-
):
69+
def _create_destination_args(args):
70+
destination_args = common.DestinationArgs()
71+
destination_args.destination_type = args.destination_type
72+
destination_args.destination = args.destination
73+
destination_args.destination_port = args.destination_port
74+
destination_args.destination_protocol = args.destination_protocol
75+
return destination_args
76+
77+
78+
def _get_logger(formatter, service_name, destination_args):
8179
try:
8280
return common.get_logger(
83-
formatter=formatter,
84-
service_name=service_name,
85-
destination=destination,
86-
destination_type=destination_type,
87-
destination_port=destination_port,
88-
destination_protocol=destination_protocol,
81+
formatter=formatter, service_name=service_name, destination_args=destination_args
8982
)
9083
except (herror, gaierror, timeout) as ex:
9184
print(repr(ex))
92-
print(
93-
"Hostname={0}, port={1}, protocol={2}.".format(
94-
destination, destination_port, destination_protocol
95-
)
96-
)
85+
_print_server_args(destination_args)
9786
exit(1)
9887
except IOError as ex:
9988
print(repr(ex))
100-
print("File path: {0}.".format(destination))
89+
if ex.errno == 61:
90+
_print_server_args(destination_args)
91+
return
92+
93+
print("File path: {0}.".format(destination_args.destination))
10194
exit(1)
10295

10396

97+
def _print_server_args(server_args):
98+
print(
99+
"Hostname={0}, port={1}, protocol={2}.".format(
100+
server_args.destination, server_args.destination_port, server_args.destination_protocol
101+
)
102+
)
103+
104+
104105
def _set_up_cursor_store(record_cursor, clear_cursor, handlers):
105106
if record_cursor or clear_cursor:
106107
store = AEDCursorStore()

c42seceventcli/common/util.py

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -65,39 +65,33 @@ def get_error_logger(service_name):
6565
return logger
6666

6767

68-
def get_logger(
69-
formatter,
70-
service_name,
71-
destination,
72-
destination_type,
73-
destination_port=514,
74-
destination_protocol="TCP",
75-
):
76-
destination_type = destination_type.lower()
68+
class DestinationArgs(object):
69+
destination_type = None
70+
destination = None
71+
destination_port = 514
72+
destination_protocol = "TCP"
73+
74+
75+
def get_logger(formatter, service_name, destination_args):
7776
logger = logging.getLogger("{0}_logger".format(service_name))
78-
handler = _get_log_handler(
79-
destination=destination,
80-
destination_type=destination_type,
81-
destination_port=destination_port,
82-
destination_protocol=destination_protocol,
83-
)
77+
handler = _get_log_handler(destination_args)
8478
handler.setFormatter(formatter)
8579
logger.addHandler(handler)
8680
logger.setLevel(logging.INFO)
8781
return logger
8882

8983

90-
def _get_log_handler(
91-
destination, destination_type, destination_port=514, destination_protocol="TCP"
92-
):
93-
if destination_type == "stdout":
84+
def _get_log_handler(destination_args):
85+
if destination_args.destination_type == "stdout":
9486
return logging.StreamHandler(sys.stdout)
95-
elif destination_type == "server":
87+
elif destination_args.destination_type == "server":
9688
return NoPrioritySysLogHandler(
97-
hostname=destination, port=destination_port, protocol=destination_protocol
89+
hostname=destination_args.destination,
90+
port=destination_args.destination_port,
91+
protocol=destination_args.destination_protocol,
9892
)
99-
elif destination_type == "file":
100-
return logging.FileHandler(filename=destination)
93+
elif destination_args.destination_type == "file":
94+
return logging.FileHandler(filename=destination_args.destination)
10195

10296

10397
def get_stored_password(service_name, username):

tests/aed/test_main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,15 +222,15 @@ def test_main_when_destination_port_is_set_passes_port_to_get_logger(patches):
222222
expected = 1000
223223
patches.aed_args.destination_port = expected
224224
main.main()
225-
actual = patches.get_logger.call_args[1]["destination_port"]
225+
actual = patches.get_logger.call_args[1]["destination_args"].destination_port
226226
assert actual == expected
227227

228228

229229
def test_main_when_given_destination_protocol_via_cli_passes_port_to_get_logger(patches):
230230
expected = "SOME PROTOCOL"
231231
patches.aed_args.destination_protocol = expected
232232
main.main()
233-
actual = patches.get_logger.call_args[1]["destination_protocol"]
233+
actual = patches.get_logger.call_args[1]["destination_args"].destination_protocol
234234
assert actual == expected
235235

236236

tests/common/test_common.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from os import path
33
from datetime import datetime, timedelta
44
from logging import StreamHandler, FileHandler
5-
from logging.handlers import RotatingFileHandler
65

76
from c42secevents.logging.handlers import NoPrioritySysLogHandler
87

@@ -15,6 +14,7 @@
1514
get_stored_password,
1615
delete_stored_password,
1716
get_user_project_path,
17+
DestinationArgs,
1818
)
1919

2020

@@ -223,7 +223,9 @@ def test_get_error_logger_uses_rotating_file_with_expected_args(mocker, mock_get
223223

224224
def test_get_logger_when_destination_type_is_stdout_adds_stream_handler_to_logger(mock_get_logger):
225225
service = "TEST_SERVICE"
226-
logger = get_logger(None, service, "Somewhere", "stdout")
226+
args = DestinationArgs()
227+
args.destination_type = "stdout"
228+
logger = get_logger(None, service, args)
227229
actual = type(logger.addHandler.call_args[0][0])
228230
expected = StreamHandler
229231
assert actual == expected
@@ -233,7 +235,9 @@ def test_get_logger_when_destination_type_is_file_adds_file_handler_to_logger(
233235
mock_get_logger, mock_file_handler
234236
):
235237
service = "TEST_SERVICE"
236-
logger = get_logger(None, service, "Somewhere", "file")
238+
args = DestinationArgs()
239+
args.destination_type = "file"
240+
logger = get_logger(None, service, args)
237241
actual = type(logger.addHandler.call_args[0][0])
238242
expected = FileHandler
239243
assert actual == expected
@@ -243,7 +247,9 @@ def test_get_logger_when_destination_type_is_server_adds_no_priority_syslog_hand
243247
mock_get_logger, mock_no_priority_syslog_handler
244248
):
245249
service = "TEST_SERVICE"
246-
logger = get_logger(None, service, "Somewhere", "server")
250+
args = DestinationArgs()
251+
args.destination_type = "server"
252+
logger = get_logger(None, service, args)
247253
actual = type(logger.addHandler.call_args[0][0])
248254
expected = NoPrioritySysLogHandler
249255
assert actual == expected

0 commit comments

Comments
 (0)