-
Notifications
You must be signed in to change notification settings - Fork 0
/
LogsReader.py
45 lines (32 loc) · 1.41 KB
/
LogsReader.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
import os
class LogsReader():
def __init__(self, reader_listner):
self._reader_listner = reader_listner
pass
def read_files(self, directory='.', file_extension='log'):
file_names = LogsReader.get_log_file_paths(directory, file_extension)
if (len(file_names) == 0):
raise RuntimeError('No files found', directory, file_extension) from os.error
read_file = self.read_file
on_read_data = self._reader_listner.on_read_data
for file_name in file_names:
file_data = read_file(file_name)
if (len(file_data) == 0):
raise RuntimeError('No data in file', file_name) from os.error
on_read_data(file_data)
@staticmethod
def get_log_file_paths(directory: str, file_extention: str) -> list:
file_names = []
for filename in os.listdir(directory):
file_full_path = os.path.join(directory, filename)
if os.path.isdir(file_full_path):
file_names.extend(LogsReader.get_log_file_paths(file_full_path, file_extention))
if filename.endswith(file_extention):
file_names.append(file_full_path)
return file_names
def read_file(self, filename) -> str:
print("Reading ", filename)
with open(filename, 'r') as logFile:
file_data = logFile.read()
logFile.close()
return file_data