-
Notifications
You must be signed in to change notification settings - Fork 0
/
filehandler.py
255 lines (194 loc) · 6.99 KB
/
filehandler.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# !/usr/bin/env python3
# -*- coding: utf-8 -*-
""" A class to handle files. """
# Imports
import os # Miscellaneous operating system interfaces
import re # Regular expression operations
import sys # System-specific parameters and functions
# A class to handle files
class FileHandler(object):
# Initialization
def __init__(self, filetypes):
# Declare class variables
self.error = "" # String for errors
self.filepath = None # File path
self.filetype = None # File type
self.filetypes = filetypes # Accepted file types
self.data = {} # Data-dictionary
# Close
def close(self):
self.error = ""
self.filepath = None
self.filetype = None
self.data = {}
# Return the number of lines
def count(self):
# Clear error message
self.error = ""
# Check for file
if not self.filepath:
self.error = "No file open."
return False
return len(self.data)
# Return only data we want to process
def get_editdata(self):
# Clear error message
self.error = ""
# Check for file
if not self.filepath:
self.error = "No file open."
return False
# If the file is an .srt-file
if self.filetype == "srt":
# Dictionary
filedata = {}
# Regular expression to match timecodes
timecode = "^\d{2}:\d{2}:\d{2},\d{3} --> \d{2}:\d{2}:\d{2},\d{3}\Z"
# Loop through filedata-dictionary and discard unwanted lines
for key in self.data:
# Line variable to make this easier
line = self.data[key]
# Separate actual subtitle lines
if line == "": # Empty line
continue
elif line.isdigit(): # Subtitle line number
continue
elif re.match(timecode, line): # Subtitle timecode
continue
else: # Add line
filedata[key] = line
# Return dictionary
return filedata
# If the file is a .txt-file
elif self.filetype == "txt":
# Dictionary
filedata = {}
# Loop through filedata-dictionary and discard unwanted lines
for key in self.data:
# Line variable to make this easier
line = self.data[key]
# Separate actual lines
if line == "": # Empty line
continue
else: # Add line
filedata[key] = line
# Return dictionary
return filedata
# Return the whole file
def get_filedata(self):
# Clear error message
self.error = ""
# Check for file
if not self.filepath:
self.error = "No file open."
return False
# Return dictionary
return self.data
# Return specific line from file
def get_line(self, key):
# Clear error message
self.error = ""
# Check for file
if not self.filepath:
self.error = "No file open."
return False
return self.data[key]
# Replace specific line to file
def set_line(self, key, line):
# Clear error message
self.error = ""
# Check for file
if not self.filepath:
self.error = "No file open."
return False
# Only apply if the line has changed
if self.data[key] != line:
self.data[key] = line
return True
# Open file
def open(self, file, enc):
# Clear error message
self.error = ""
# Close any previous file
self.close()
# Check file path
if file == "":
self.error = "Filename is empty."
return False
# Check if file exists
if not os.path.exists(file):
self.error = "File does not exist: " + file
return False
# File path should be an existing regular file
if not os.path.isfile(file):
self.error = "Not a file: " + file
return False
# File should not be empty
if not os.path.getsize(file) > 0:
self.error = "File is empty: " + file
return False
# Check file extension
ext = os.path.splitext(file)[1][1:].lower()
if ext not in self.filetypes:
self.error = "File type is not valid: " + file
return False
# Loop through file and set file data into a dictionary
try:
filehandle = open(file, "r", encoding=enc)
i = 0
while True:
line = filehandle.readline()
if len(line) == 0: # Zero lenght indicates EOF
break
line = line.rstrip("\r\n")
self.data[i] = line
i += 1
filehandle.close()
except IOError as e:
self.error = "I/O error({0}): {1}".format(e.errno, e.strerror)
return False
except UnicodeDecodeError:
self.error = "Unicode Decode Error: " + file
return False
except UnicodeEncodeError:
self.error = "Unicode Encode Error: " + file
return False
except:
self.error = "Unexpected error: " + str(sys.exc_info()[1])
return False
else:
self.filepath = file
self.filetype = ext
return True
# Save file
def save(self, enc, emptylines):
# Clear error message
self.error = ""
# Check for file
if not self.filepath:
self.error = "No file open."
return False
# Remove empty lines from the end of the file
if emptylines:
while len(self.data) > 1 and self.data[len(self.data) - 1] == "":
del self.data[len(self.data) - 1]
# Loop dictionary and save to file
try:
filehandle = open(self.filepath, "w", encoding=enc)
for key, value in sorted(self.get_filedata().items()):
filehandle.write(value + "\n")
filehandle.close()
except IOError as e:
self.error = "I/O error({0}): {1}".format(e.errno, e.strerror)
return False
except UnicodeDecodeError:
self.error = "Unicode Decode Error: " + self.filepath
return False
except UnicodeEncodeError:
self.error = "Unicode Encode Error: " + self.filepath
return False
except:
self.error = "Unexpected error: " + str(sys.exc_info()[1])
return False
else:
return True