Coverage for oc_ocdm/support/reporter.py: 68%

38 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2025-05-30 22:05 +0000

1#!/usr/bin/python 

2# -*- coding: utf-8 -*- 

3# Copyright (c) 2016, Silvio Peroni <essepuntato@gmail.com> 

4# 

5# Permission to use, copy, modify, and/or distribute this software for any purpose 

6# with or without fee is hereby granted, provided that the above copyright notice 

7# and this permission notice appear in all copies. 

8# 

9# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH 

10# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND 

11# FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, 

12# OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 

13# DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS 

14# ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 

15# SOFTWARE. 

16from __future__ import annotations 

17 

18from typing import TYPE_CHECKING 

19 

20if TYPE_CHECKING: 

21 from typing import List, Optional 

22 

23 

24class Reporter(object): 

25 """This class is used as a metaphoric agent being a reporter""" 

26 

27 def __init__(self, print_sentences: bool = True, prefix: str = "") -> None: 

28 self.articles: List[List[str]] = [] 

29 self.last_article: Optional[List[str]] = None 

30 self.last_sentence: Optional[str] = None 

31 self.print_sentences: bool = print_sentences 

32 self.prefix: str = prefix 

33 

34 def new_article(self) -> None: 

35 if self.last_article is None or len(self.last_article) > 0: 

36 self.last_article = [] 

37 self.last_sentence = None 

38 self.articles.append(self.last_article) 

39 if self.print_sentences and len(self.last_article) > 0: 

40 print("\n") 

41 

42 def add_sentence(self, sentence: str, print_this_sentence: bool = True) -> None: 

43 cur_sentence: str = self.prefix + sentence 

44 self.last_sentence = cur_sentence 

45 self.last_article.append(cur_sentence) 

46 if self.print_sentences and print_this_sentence: 

47 print(cur_sentence) 

48 

49 def get_last_sentence(self) -> Optional[str]: 

50 return self.last_sentence 

51 

52 def get_articles_as_string(self) -> str: 

53 result: str = "" 

54 for article in self.articles: 

55 for sentence in article: 

56 result += sentence + "\n" 

57 result += "\n" 

58 return result 

59 

60 def write_file(self, file_path) -> None: 

61 with open(file_path, 'wt', encoding='utf-8') as f: 

62 f.write(self.get_articles_as_string()) 

63 

64 def is_empty(self) -> bool: 

65 return self.last_sentence is None