-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread_compressed.py
More file actions
65 lines (55 loc) · 2.63 KB
/
read_compressed.py
File metadata and controls
65 lines (55 loc) · 2.63 KB
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
#!/usr/bin/env python
import os
import sys
import argparse
from compression_handler import CompressionHandler
from data_extractor import DataExtractor
from data_restructurer import DataRestructurer
from error_handler import ErrorHandler
def main():
parser = argparse.ArgumentParser(description="Read compressed files and restructure them.")
parser = argparse.ArgumentParser(description="Read compressed files and restructure them.")
parser.add_argument("target_dir", help="The directory containing the compressed files.")
args = parser.parse_args()
target_dir = args.target_dir
if not os.path.isdir(target_dir):
print(f"Error: Directory '{target_dir}' does not exist.")
sys.exit(1)
compression_handler = CompressionHandler()
data_extractor = DataExtractor()
data_restructurer = DataRestructurer()
error_handler = ErrorHandler()
# Find compressed files
compressed_files = []
for root, _, files in os.walk(target_dir):
for file in files:
file_path = os.path.join(root, file)
if file_path != "/Users/Elnaz1/Documents/data from the servers /miniscope /A0600/A0634/2020_11_22.zip":
continue
if compression_handler.is_compressed(file_path):
compressed_files.append(file_path)
# Process each compressed file
for compressed_file in compressed_files:
print(f"Processing: {compressed_file}")
extract_path = os.path.splitext(compressed_file)[0] # Extract to a directory with the same name as the file
if compression_handler.extract_file(compressed_file, extract_path):
print(f"Extracted to: {extract_path}")
# Find extracted files
extracted_files = []
for root, _, files in os.walk(extract_path):
for file in files:
file_path = os.path.join(root, file)
extracted_files.append(file_path)
# Process each extracted file
for extracted_file in extracted_files:
data = data_extractor.extract_data(extracted_file)
if data is None:
error_handler.log_error(f"Failed to extract data from {extracted_file}")
continue
output_dir = os.path.join(target_dir, "output") # Output directory
if not data_restructurer.restructure_data(data["file_path"], extracted_file, output_dir):
error_handler.log_error(f"Failed to restructure data from {extracted_file}")
else:
error_handler.log_error(f"Failed to extract {compressed_file}")
if __name__ == "__main__":
main()