-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileOperations.py
132 lines (113 loc) · 4.09 KB
/
FileOperations.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
import glob
from os import listdir
from os.path import isfile, join
class FileOperation:
path = "./test_path"
EPOCH_SIZE = 10
POINTER_FILE = ""
POINTER_EPOCH = 0
FILES_COMPLETED = []
FILE_IN_PROCESS = ""
TEMP_FILES_ADDRESS = "temp1.cfg"
END_OF_FILE = False
def __init__(self, *args):
if len(args) > 0:
self.path = args[0]
self.EPOCH_SIZE = args[1]
self.POINTER_FILE = args[2]
self.POINTER_EPOCH = args[3]
self.FILES_COMPLETED = args[4]
self.TEMP_FILES_ADDRESS = args[5]
self.path_file_collector()
self.currentFileInProcessName = ''
self.logFile = open(self.TEMP_FILES_ADDRESS, 'a')
def log_decorator(func):
def wrapper(self, *args):
# before starts
self.logFile.writelines(
["File in process : " + str(self.FILE_IN_PROCESS) + " epoch point : " + str(self.POINTER_EPOCH) +
" Files completed : " + str(self.FILES_COMPLETED) + "\n"])
# / before starts
try:
return func(self, args)
except AssertionError as a:
self.error_handler(1)
except IndexError as i:
self.error_handler(2)
except TypeError as t:
self.error_handler(3)
except ValueError as v:
self.error_handler(4)
except NameError as n:
self.error_handler(5)
# after finished
return wrapper
@staticmethod
def error_handler(error_number):
def assertion_error():
print('Assertion Exception Raised')
exit(-1)
def index_error():
print('Index Exception Raised')
exit(-1)
def type_error():
print('Caught TypeError Exception')
exit(-1)
def value_error():
print('Caught ValueError Exception')
exit(-1)
def name_error():
print('Caught ValueError Exception')
exit(-1)
switcher = {
1: assertion_error,
2: index_error,
3: type_error,
4: value_error,
5: name_error
}
switcher.get(error_number)()
fileSetForReading = False
@log_decorator
def read_line(self, *args):
data = []
if not self.fileSetForReading:
self.start_new_file()
for i in range(self.EPOCH_SIZE):
current_data = self.currentFileInProcess.readline()
if current_data != '':
data.append(current_data)
else:
self.fileSetForReading = False
self.start_new_file()
break
self.POINTER_EPOCH += 1
return data
@log_decorator
def get_new_file(self, file):
# Change current file in process
self.FILE_IN_PROCESS = file
self.POINTER_EPOCH = 0
def path_file_collector(self):
self.pathFiles = [f for f in glob.glob(self.path + "**/*.csv", recursive=True)]
def start_new_file(self):
self.FILES_COMPLETED.append(self.currentFileInProcessName)
for file in self.pathFiles:
if file not in self.FILES_COMPLETED:
self.currentFileInProcess = open(file, "r")
self.currentFileInProcessName = file
self.fileSetForReading = True
return 1
self.logFile.writelines(
["File in process : " + str(self.FILE_IN_PROCESS) + " epoch point : " + str(self.POINTER_EPOCH) +
" Files completed : " + str(self.FILES_COMPLETED) + "\n"])
# exit(Exception("All path files finished"))
self.END_OF_FILE = True
def list_files(self):
# read all files in path and return their names in only_files variable as a list
try:
only_files = [f for f in listdir(self.path) if isfile(join(self.path, f))]
return only_files
except NotADirectoryError:
print('Error at FileOperation.list_files: Expected directory but a file was given')
return []