Skip to content

Commit

Permalink
Introducing Analyzer (#32)
Browse files Browse the repository at this point in the history
* Adding Analyzer class and main function

* Adding EchoAnalyzer

* Adding abstractmethod annotation to Analyzer methods

---------

Co-authored-by: Ben Rady <[email protected]>
  • Loading branch information
benrady and benrady-aq authored Jun 28, 2024
1 parent 02dd315 commit 6f23a38
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 7 deletions.
5 changes: 5 additions & 0 deletions src/alogamous/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import sys

from alogamous import analyzer, echo_analyzer

analyzer.analyze_log_stream([echo_analyzer.EchoAnalyzer()], sys.stdin, sys.stdout)
19 changes: 19 additions & 0 deletions src/alogamous/analyzer.py
Original file line number Diff line number Diff line change
@@ -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)
12 changes: 12 additions & 0 deletions src/alogamous/echo_analyzer.py
Original file line number Diff line number Diff line change
@@ -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))
2 changes: 0 additions & 2 deletions src/alogamous/example.py

This file was deleted.

17 changes: 17 additions & 0 deletions tests/echo_analyzer_test.py
Original file line number Diff line number Diff line change
@@ -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"""
)
5 changes: 0 additions & 5 deletions tests/example_test.py

This file was deleted.

0 comments on commit 6f23a38

Please sign in to comment.