-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer.py
143 lines (113 loc) · 4.98 KB
/
writer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
"""contains ResultsWriter class"""
import os
from typing import Union
from constants import BranchSearchResults, Constants, Messages
from config import ConfigurationManager
class ResultsWriter:
"""used for writing results files (details, matches, words)"""
def __init__(self, date: str) -> None:
config_folder = Constants.RESULTS_FOLDER
path = os.path.join(config_folder, date)
if not os.path.exists(path):
os.makedirs(path)
self.__config_file = os.path.join(path, Constants.CONFIG_FILE)
self.__details_file = os.path.join(path, Constants.DETAILS_FILE)
self.__matches_file = os.path.join(path, Constants.MATCHES_FILE)
self.__words_file = os.path.join(path, Constants.FOUND_FILE)
self.__found_words = set()
def write_repo_start(self, name: str) -> None:
"""writes repo start section"""
self.__write_to_details_file(name)
def write_branch_start(self, name: str) -> None:
"""writes branch start section"""
line = Constants.TAB + name
self.__write_to_details_file(line)
def write_branch_skip(self, reason: str) -> None:
"""writes branch skip section"""
line = Constants.TAB + f"skipped - {reason}"
self.__write_to_details_file(line)
def write_branch_results(
self, repo: str, branch: str, results: BranchSearchResults
) -> None:
"""writes all branch search results sections"""
lines, count = self.__format_matches(results.matches)
if lines:
self.__write_to_matches_file(repo)
self.__write_to_matches_file(f"{Constants.TAB}{branch} ({count})")
self.__write_to_matches_file(lines, Constants.TAB * 2)
self.__write_to_details_file(
f"{Constants.TAB * 2}{Messages.MATCHES} ({count})"
)
self.__write_to_details_file(lines, Constants.TAB * 3)
sections = {
Messages.ERRORS: results.errors,
Messages.SKIPPED_FOLDERS: results.skipped_folders,
Messages.SKIPPED_FILES: results.skipped_files,
Messages.SEARCHED_FOLDERS: results.folders,
Messages.SEARCHED_FILES: results.files,
}
for name, section in sections.items():
lines = self.__format_results_section(name, section)
if lines:
self.__write_to_details_file(lines)
def __format_matches(self, matches: dict) -> tuple:
if len(matches) == 0:
return [], -1
count = 0
lines = []
for path in matches:
lines.append(path)
for word in matches[path].keys():
self.__found_words.add(word)
lines.append(Constants.TAB + word)
for match in matches[path][word]:
lines.append(Constants.TAB * 2 + match)
count += 1
if not count:
return [], -1
return lines, count
def __format_results_section(self, name: str, section: list) -> list[str]:
"""formats branch search results section (other than matches) for output"""
if len(section) == 0:
return []
spacer = Constants.TAB * 2
lines = [f"{spacer}{name} ({len(section)})"]
spacer += Constants.TAB
for i in section:
lines.append(spacer + i)
return lines
def write_config(self, config: ConfigurationManager) -> None:
"""writes config info"""
self.__write_to_config_file(config.config_str())
def write_found_words(self) -> None:
"""writes list of words found in search"""
if self.__found_words:
self.__write_to_words_file(list(self.__found_words))
def __write_to_config_file(
self, output: Union[str, list[str]], prefix: str = ""
) -> None:
self.__write(self.__config_file, output, prefix)
def __write_to_words_file(
self, output: Union[str, list[str]], prefix: str = ""
) -> None:
self.__write(self.__words_file, output, prefix)
def __write_to_details_file(
self, output: Union[str, list[str]], prefix: str = ""
) -> None:
self.__write(self.__details_file, output, prefix)
def __write_to_matches_file(
self, output: Union[str, list[str]], prefix: str = ""
) -> None:
self.__write(self.__matches_file, output, prefix)
def __write(self, file: str, output: Union[str, list[str]], prefix: str) -> None:
if isinstance(output, str):
self.__write_line(file, output, prefix)
else:
self.__write_lines(file, output, prefix)
def __write_line(self, filename, line, prefix) -> None:
with open(filename, "a", encoding=Constants.ENCODING) as file:
file.write(prefix + line + Constants.NEWLINE)
def __write_lines(self, filename, lines, prefix) -> None:
with open(filename, "a", encoding=Constants.ENCODING) as file:
for line in lines:
file.write(prefix + line + Constants.NEWLINE)