Skip to content

Commit 2b44593

Browse files
author
Amir Ali
committed
Update extractors.py
1 parent bb15748 commit 2b44593

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

build/extractors.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def export_extracted_data_to_csv(self, output_file : str, patterns : Patterns ,c
2929
writer.writerows(extracted_data)
3030

3131
if WithLogging.with_logging:
32-
return DataExtractor.log_found_data(extracted_data_copy)
32+
return DataExtractor.get_extracted_data_string(extracted_data_copy)
3333

3434
class ExcelFileExtractor:
3535
def __init__(self, col_var, exact_var):
@@ -100,7 +100,7 @@ def export_extracted_data_to_excel(self, output_file : str, sheet_name : str, pa
100100
wb.close()
101101

102102
if WithLogging.with_logging:
103-
return DataExtractor.log_found_data(extracted_data_copy)
103+
return DataExtractor.get_extracted_data_string(extracted_data_copy)
104104

105105
class DataExtractor:
106106
def __init__(self, excel_var, log_text, col_var, exact_var):
@@ -121,15 +121,20 @@ def extract_data(patterns : Patterns, content : str) -> ExtractedData:
121121

122122
def prepare_to_extract_data(self, output_file : str, input_file : str, sheet_name : str, patterns : Patterns) -> None:
123123
try:
124+
log_string = ''
125+
124126
assert patterns, 'There is no patterns to extract data'
127+
assert output_file, 'The name of output file is required.'
128+
129+
# --- read the content of input file ---
125130

126131
with open(input_file,encoding=ENCODING) as f:
127132
try:
128133
content = f.read()
129134
except UnicodeDecodeError:
130135
raise ValueError('The input file cannot be a binary file')
131136

132-
assert output_file, 'The name of output file is required.'
137+
# --- deciding what to do with the output file ---
133138

134139
output_file_extention = os.path.splitext(output_file)[1].lower()
135140

@@ -142,13 +147,10 @@ def prepare_to_extract_data(self, output_file : str, input_file : str, sheet_nam
142147
else:
143148
log_string = self.csv_extractor.export_extracted_data_to_csv(output_file,patterns,content)
144149

150+
# --- handling the log ----
151+
145152
if WithLogging.with_logging:
146-
log_string += f'\n{output_file!r} saved.' + '\n'
147-
self.log_text.config(state='normal')
148-
self.log_text.delete('1.0','end')
149-
self.log_text.insert('end', log_string)
150-
self.log_text.config(state='disabled')
151-
self.log_text.see('end')
153+
self.log_found_data(log_string, output_file)
152154

153155
except (FileNotFoundError, AssertionError, PermissionError, ValueError, re.PatternError) as err:
154156
show_error(err)
@@ -157,14 +159,22 @@ def prepare_to_extract_data(self, output_file : str, input_file : str, sheet_nam
157159
show_error("You cannot place multiple groups in a pattern")
158160

159161
@staticmethod
160-
def log_found_data(extracted_data_copy : ExtractedData) -> str:
162+
def get_extracted_data_string(extracted_data_copy : ExtractedData) -> str:
161163
log_string = ''
162164

163165
for data_list in extracted_data_copy:
164166
log_string += '\n'.join(data_list) + '\n'
165167

166168
return log_string
167169

170+
def log_found_data(self, log_string : str, output_file : str):
171+
log_string += f'\n{output_file!r} saved.' + '\n'
172+
self.log_text.config(state='normal')
173+
self.log_text.delete('1.0','end')
174+
self.log_text.insert('end', log_string)
175+
self.log_text.config(state='disabled')
176+
self.log_text.see('end')
177+
168178
@staticmethod
169179
def create_column_order(extracted_data : ExtractedData) -> tuple[tuple[str]]:
170180
max_len = max([len(data_list) for data_list in extracted_data])

0 commit comments

Comments
 (0)