-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquick_extract.py
52 lines (41 loc) · 1.58 KB
/
quick_extract.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
import zipfile
import os
import json
import shutil
from alfred import alfred_error_message, outcome_JSON, selection_error_message
def temp_extract_and_quicklook(zip_file, to_extract, next_directory):
# Extracts a file in a temporary folder and takes a Quicklook at it
cache_folder = os.getenv('alfred_workflow_cache')
temp_dir = os.path.join(cache_folder, 'quicklook_files')
if not os.path.isdir(temp_dir):
try:
os.mkdir(temp_dir)
except Exception as e:
comment = f"Error creating quicklook files folder"
subcomment = str(e)
selection_error_message(comment, subcomment)
sys.exit()
# Extraction
with zipfile.ZipFile(zip_file, 'r') as zip_ref:
try:
zip_ref.extract(to_extract, temp_dir)
except Exception as e:
comment = "Error: " + str(e)
alfred_error_message(comment, next_directory, zip_file)
return
# Quicklook with ql if possible, with qlmanage -p otherwise
temp_file = os.path.join(temp_dir, to_extract)
# Pass variables to the External Call that reruns the workflow
comment = ""
ResultJSON = outcome_JSON(next_directory, comment, zip_file, to_reveal=temp_file)
print(json.dumps(ResultJSON))
def main():
zip_file = os.getenv('zip_file')
to_extract = os.getenv('file_to_extract')
next_directory = os.getenv('next_directory')
temp_extract_and_quicklook(zip_file, to_extract, next_directory)
if __name__ == "__main__":
main()