Coverage for oc_ocdm / support / reporter.py: 71%
38 statements
« prev ^ index » next coverage.py v7.13.4, created at 2026-03-28 18:52 +0000
« prev ^ index » next coverage.py v7.13.4, created at 2026-03-28 18:52 +0000
1#!/usr/bin/python
3# SPDX-FileCopyrightText: 2020-2022 Simone Persiani <iosonopersia@gmail.com>
4# SPDX-FileCopyrightText: 2025-2026 Arcangelo Massari <arcangelo.massari@unibo.it>
5#
6# SPDX-License-Identifier: ISC
8# -*- coding: utf-8 -*-
9from __future__ import annotations
11from typing import TYPE_CHECKING
13if TYPE_CHECKING:
14 from typing import List, Optional
17class Reporter(object):
18 """This class is used as a metaphoric agent being a reporter"""
20 def __init__(self, print_sentences: bool = True, prefix: str = "") -> None:
21 self.articles: List[List[str]] = []
22 self.last_article: Optional[List[str]] = None
23 self.last_sentence: Optional[str] = None
24 self.print_sentences: bool = print_sentences
25 self.prefix: str = prefix
27 def new_article(self) -> None:
28 if self.last_article is None or len(self.last_article) > 0:
29 self.last_article = []
30 self.last_sentence = None
31 self.articles.append(self.last_article)
32 if self.print_sentences and len(self.last_article) > 0:
33 print("\n")
35 def add_sentence(self, sentence: str, print_this_sentence: bool = True) -> None:
36 assert self.last_article is not None
37 cur_sentence: str = self.prefix + sentence
38 self.last_sentence = cur_sentence
39 self.last_article.append(cur_sentence)
40 if self.print_sentences and print_this_sentence:
41 print(cur_sentence)
43 def get_last_sentence(self) -> Optional[str]:
44 return self.last_sentence
46 def get_articles_as_string(self) -> str:
47 parts = []
48 for article in self.articles:
49 for sentence in article:
50 parts.append(sentence)
51 parts.append("\n")
52 parts.append("\n")
53 return ''.join(parts)
55 def write_file(self, file_path) -> None:
56 with open(file_path, 'wt', encoding='utf-8') as f:
57 f.write(self.get_articles_as_string())
59 def is_empty(self) -> bool:
60 return self.last_sentence is None