-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmultifile.py
122 lines (99 loc) · 3.9 KB
/
multifile.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
#!/usr/bin/python
# Mark's Media Framework
# Copyright (C) 2011 Mark Pariente
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
import os.path
from mmf import vidparse
from mmf import errors
_READ_BUFFER_SIZE = 4 * 1024 # 4KB per buffer read iteration
class MultiFileInput:
"""Multiple input file implementation"""
def __init__(self, file_list):
self.parser = None
self._current_file = None
self._current_idx = -1
try:
self._validate_list(file_list)
except errors.MMFError:
raise
# Cache absolute paths of all files
self._file_list = []
for cur_file in file_list:
self._file_list.append(os.path.abspath(cur_file))
self._output_fd = None
def _validate_list(self, file_list):
"""Validate the input file list to make sure all files are similar"""
print "Validating multiple file compatibility..."
for cur_file in file_list:
if self.parser is None:
try:
self.parser = vidparse.VidParser(cur_file)
except errors.MMFError:
raise
else:
try:
cur_parser = vidparse.VidParser(cur_file)
except errors.MMFError:
raise
if cur_parser != self.parser:
raise errors.MMFError(
"Files '%s' and '%s' are incompatible:\n%s" %
(file_list[0], cur_file, cur_parser.diff_str))
# TODO: Validate that the file containers are concat-able
print "All files compatible!"
def _open_next(self):
"""Opens the next file in the list and sets current file appropriately"""
if self._current_file is not None:
self._current_file.close()
self._current_idx += 1
if self._current_idx < len(self._file_list):
self._current_file = open(self._file_list[self._current_idx], 'rb')
else:
self._current_file = None
def _read(self):
"""Reads data from the current file and returns it"""
buf = ""
if (self._current_idx == -1):
self._open_next()
if self._current_file is not None:
buf = self._current_file.read(_READ_BUFFER_SIZE)
if buf == "":
self._open_next()
if self._current_file is not None:
buf = self._current_file.read(_READ_BUFFER_SIZE)
return buf
def set_output(self, output_fd):
"""Set the output file descriptor for write_all() to write to"""
self._output_fd = output_fd
def write_all(self):
"""
Read all the files in the input list and write them to the output
set with set_output()
"""
if self._output_fd is None:
raise errors.MMFError(
"No output descriptor set for %s" % self)
buf = self._read()
while buf != "":
try:
self._output_fd.write(buf)
except IOError:
# Output file descriptor closed, reader process is gone...
raise
buf = self._read()
self._output_fd.close()
def rewind(self):
"""Rewind to the beginning of the input list"""
if self._current_file is not None:
self._current_file.close()
self._current_file = None
self._current_idx = -1