Skip to content

Commit

Permalink
Improving line parser config (#61)
Browse files Browse the repository at this point in the history
* adding nested dictionary of configs

* making command line arugments refer to config dictionary

* fixing type errors

---------

Co-authored-by: krady <[email protected]>
  • Loading branch information
kitrady and krady authored Jul 20, 2024
1 parent 5543793 commit 7786569
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/alogamous/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,24 @@
warning_analyzer,
)

if len(sys.argv) != int(sys.argv[1]) + 4:
sys.stdout.write(f"Usage: {sys.argv[0]} <NUM_FIELDS> <FIELD_NAME1> <FIELD_NAME2> ... <SEPARATOR> <HEADER_LINE>")
expected_arg_len = 2

if len(sys.argv) != expected_arg_len:
sys.stdout.write(f"Usage: {sys.argv[0]} <LOG_CONFIG>")
sys.stdout.write("\nPossible LOG_CONFIGS are:")
for key in log_line_parser.LOG_FILE_CONFIGS:
sys.stdout.write(f"\n- {key}")
sys.exit(1)

expected_fields = sys.argv[2 : int(sys.argv[1]) + 2]

with open("../../data/test_output_file.txt", "a") as output_file:
reader = directory_reader.DirectoryReader("../../data")
line_parser = log_line_parser.LogLineParser(
expected_fields, sys.argv[int(sys.argv[1]) + 2], sys.argv[int(sys.argv[1]) + 3]
expected_fields = list(
log_line_parser.LOG_FILE_CONFIGS[sys.argv[1]][log_line_parser.ConfigParameters.EXPECTED_FIELDS]
)
seperator = str(log_line_parser.LOG_FILE_CONFIGS[sys.argv[1]][log_line_parser.ConfigParameters.SEPERATOR])
header_line = str(log_line_parser.LOG_FILE_CONFIGS[sys.argv[1]][log_line_parser.ConfigParameters.HEADER_LINE])
line_parser = log_line_parser.LogLineParser(expected_fields, seperator, header_line)
analyzer.analyze_log_stream(
[
echo_analyzer.EchoAnalyzer(),
Expand Down
15 changes: 15 additions & 0 deletions src/alogamous/log_line_parser.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
from __future__ import annotations


class ConfigParameters:
EXPECTED_FIELDS = "expected fields"
SEPERATOR = "seperator"
HEADER_LINE = "header line"


LOG_FILE_CONFIGS = {
"default": {
ConfigParameters.EXPECTED_FIELDS: ["datetime", "source", "level", "message"],
ConfigParameters.SEPERATOR: " - ",
ConfigParameters.HEADER_LINE: "====================================================",
}
}


class LineType:
HEADER_LINE = "header line"
LOG_LINE = "log line"
Expand Down

0 comments on commit 7786569

Please sign in to comment.