From 6f23a38bfaa274a25a3f300e338f6dd389e79cef Mon Sep 17 00:00:00 2001 From: Ben Rady Date: Fri, 28 Jun 2024 15:19:42 -0500 Subject: [PATCH] Introducing Analyzer (#32) * Adding Analyzer class and main function * Adding EchoAnalyzer * Adding abstractmethod annotation to Analyzer methods --------- Co-authored-by: Ben Rady --- src/alogamous/__main__.py | 5 +++++ src/alogamous/analyzer.py | 19 +++++++++++++++++++ src/alogamous/echo_analyzer.py | 12 ++++++++++++ src/alogamous/example.py | 2 -- tests/echo_analyzer_test.py | 17 +++++++++++++++++ tests/example_test.py | 5 ----- 6 files changed, 53 insertions(+), 7 deletions(-) create mode 100644 src/alogamous/__main__.py create mode 100644 src/alogamous/analyzer.py create mode 100644 src/alogamous/echo_analyzer.py delete mode 100644 src/alogamous/example.py create mode 100644 tests/echo_analyzer_test.py delete mode 100644 tests/example_test.py diff --git a/src/alogamous/__main__.py b/src/alogamous/__main__.py new file mode 100644 index 0000000..c58d6aa --- /dev/null +++ b/src/alogamous/__main__.py @@ -0,0 +1,5 @@ +import sys + +from alogamous import analyzer, echo_analyzer + +analyzer.analyze_log_stream([echo_analyzer.EchoAnalyzer()], sys.stdin, sys.stdout) diff --git a/src/alogamous/analyzer.py b/src/alogamous/analyzer.py new file mode 100644 index 0000000..7597871 --- /dev/null +++ b/src/alogamous/analyzer.py @@ -0,0 +1,19 @@ +from abc import ABC, abstractmethod + + +class Analyzer(ABC): + @abstractmethod + def read_log_line(self, line): + pass + + @abstractmethod + def report(self, out_stream): + pass + + +def analyze_log_stream(analyzers, in_stream, out_stream): + for line in in_stream: + for analyzer in analyzers: + analyzer.read_log_line(line.rstrip()) + for analyzer in analyzers: + analyzer.report(out_stream) diff --git a/src/alogamous/echo_analyzer.py b/src/alogamous/echo_analyzer.py new file mode 100644 index 0000000..8b2ab73 --- /dev/null +++ b/src/alogamous/echo_analyzer.py @@ -0,0 +1,12 @@ +from alogamous import analyzer + + +class EchoAnalyzer(analyzer.Analyzer): + def __init__(self): + self.lines = [] + + def read_log_line(self, line): + self.lines.append(line) + + def report(self, out_stream): + out_stream.write("\n".join(self.lines)) diff --git a/src/alogamous/example.py b/src/alogamous/example.py deleted file mode 100644 index 788043e..0000000 --- a/src/alogamous/example.py +++ /dev/null @@ -1,2 +0,0 @@ -def hello_world(): - return "Hello World" diff --git a/tests/echo_analyzer_test.py b/tests/echo_analyzer_test.py new file mode 100644 index 0000000..68f6ba6 --- /dev/null +++ b/tests/echo_analyzer_test.py @@ -0,0 +1,17 @@ +import io + +from alogamous import analyzer, echo_analyzer + + +def test_echo_lines(): + in_stream = io.StringIO("""line1 + line2 + line3""") + out_stream = io.StringIO() + analyzer.analyze_log_stream([echo_analyzer.EchoAnalyzer()], in_stream, out_stream) + assert ( + out_stream.getvalue() + == """line1 + line2 + line3""" + ) diff --git a/tests/example_test.py b/tests/example_test.py deleted file mode 100644 index d527371..0000000 --- a/tests/example_test.py +++ /dev/null @@ -1,5 +0,0 @@ -import alogamous.example - - -def test_hello_world(): - assert alogamous.example.hello_world() == "Hello World"