forked from joernhees/sublime-compressor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Compressor.py
294 lines (257 loc) · 9.17 KB
/
Compressor.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# encoding: utf-8
'''
Sublime text de-Compressor
View compressed files ( gzip, bzip2 ) content in sublime text
Support verified for
- gzip (Sublime Text 2)
- bzip (Sublime Text 3)
- lzma (Sublime Text 4)
'''
from os.path import basename, join, dirname, exists, rename
import sys
import threading
import time
from tempfile import mkdtemp
import sublime
import sublime_plugin
'''
# header references
- [gzip](https://tools.ietf.org/html/rfc1952)
- [bzip2](https://en.wikipedia.org/wiki/Bzip2#File_format)
- [xz](https://tukaani.org/xz/format.html)
- [brotli (candidate for frame format)](https://github.com/google/brotli/issues/727)
'''
COMPRESSION_MODULES = {
'gzip': {'extension': '.gz', 'header': [0x1F, 0x8B]},
# since build 3114, Use dependency with older version
'bz2': {'handler': 'BZ2File', 'extension': '.bz2', 'header': [0x42, 0x5A]},
# future proof 20171031
'backports_lzma': {'handler': 'LZMAFile', 'extension': '.xz', 'header': [0xFD, 0x37, 0x7A, 0x58, 0x5A, 0x00]},
'lzma': {'handler': 'LZMAFile', 'extension': '.xz', 'header': [0xFD, 0x37, 0x7A, 0x58, 0x5A, 0x00]}
# more future proof 20190307
# brotli framing format is not fixed yet, that mean no magic for now
# candidate framing:
# 'brotli': {'extension': '.br', 'header': [0xCE, 0xB2, 0xCF, 0x81]}
# 'brotli': {'extension': '.br'}
# the official brotli module do not define any file-like interface might need to write a wrapper
}
def load_module(module, compression_module):
'''
Load one compression module
Parameters
----------
module : str module name
compression_module
'''
try:
if module not in sys.modules:
return False
open_attr = 'open'
# module override
if 'handler' in compression_module:
open_attr = compression_module['handler']
decompressor = __import__(module)
path = module.split('.')
if len(path) > 1:
for element in path[1:]:
decompressor = getattr(decompressor, element)
compression_module['open'] = getattr(decompressor, open_attr)
return True
except Exception as e:
print(e)
return False
def get_decompressor_by_header(filename):
'''
Attempt to detect the file compression format
Parameters
----------
filename : str
input file path to read the magic bytes from
Returns
-------
suffix : str or None
file extension to remove to have the original filename
decompressor : func on None
callable to create a file-like object to read decompressed data from
'''
read_header = []
'''
We cannot reliably get character from the buffer to figure out the header
although it would have been nice to do it with View.substr(point)
Reading a binary file open an hexdump view
depending on the `enable_hexadecimal_encoding` setting
Investigation :
sublime.load_settings("Preferences.sublime-settings").get("enable_hexadecimal_encoding")
to get current view
and have head guess depending on the view
probably too much work for our current needs
'''
file_size = stat(filename).st_size
if file_size == 0:
return None, None
with open(filename, "rb") as f_input:
for module in COMPRESSION_MODULES:
compression_module = COMPRESSION_MODULES[module]
if 'loaded' not in compression_module:
# Lazy loading modules
compression_module['loaded'] = load_module(module, compression_module)
suffix = compression_module['extension']
if not compression_module['loaded']:
continue
if 'open' not in compression_module:
continue
if 'header' not in compression_module:
continue
header = compression_module['header']
len_read = len(read_header)
len_header = len(header)
min_len = min(len_header, len_read)
if file_size <= len_header:
continue
if (min_len > 0) and (read_header[0: min_len] != header[0: min_len]):
continue
while len(read_header) < len_header:
read_header.append(ord(f_input.read(1)))
if read_header[0: len_header] == header:
decompressor = compression_module['open']
return suffix, decompressor
# headerless cases, we rely on file extension alone
for module in COMPRESSION_MODULES:
compression_module = COMPRESSION_MODULES[module]
if 'open' not in compression_module:
continue
if 'header' in compression_module:
continue
suffix = compression_module['extension']
if filename.endswith(suffix):
return suffix, compression_module['open']
return None, None
def copy_file(f_input, f_output, bytes_total):
'''
Copy file while attempting to report progress
Parameters
----------
f_input : file
input file to read from
f_output : file
output file to write to
bytes_total : array of int
container to hold a reference to the total bytes decompressed
'''
bytes_total[0] = 0
start_time = time.time()
while True:
read_buffer = f_input.read(4096)
bytes_read = len(read_buffer)
if bytes_read == 0:
break
f_output.write(read_buffer)
bytes_total[0] += bytes_read
print("%f seconds spent decompressing" % (time.time() - start_time))
def decompress(source, target):
suffix, decompressor = get_decompressor_by_header(source)
if not (suffix and decompressor):
return None
sublime.status_message("opening compressed file: %s" % source)
print("opening compressed file: " + source)
print("decompress into: " + target)
# some compressor don't support the `with` statement
f_input = decompressor(source, 'rb')
with open(target, "wb") as f_output:
bytes_total = [0]
thread = threading.Thread(target=copy_file, args=[f_input, f_output, bytes_total])
thread.start()
while thread.is_alive():
time.sleep(.1)
message = "opening compressed file: %s, %i bytes decompressed" % (source, bytes_total[0])
sublime.status_message(message)
thread.join()
f_input.close()
return suffix
def load_decompress(view):
'''
Decompress the view if the file is compressed in an acceptable format
Parameters
----------
view : sublime.View
view that contains the file to be decompressed
'''
if view.get_status('decompressed'):
return
'''
Execute work for both version
'''
filepath = view.file_name()
window = view.window()
for item in window.views():
if item.get_status('decompressed') == filepath:
window.run_command('close_file')
window.focus_view(item)
return
# file_basename = basename(filepath)[:-len(suffix)]
file_basename = basename(filepath)
file_temp = join(mkdtemp(), file_basename)
suffix = decompress(filepath, file_temp)
if suffix is None:
return
if file_temp.endswith(suffix):
old_name = file_temp
file_temp = file_temp[:-len(suffix)]
rename(old_name, file_temp)
'''
https://stackoverflow.com/a/25631071
you apparently cannot close a view outside of a command
using `view.close` would throw:
AttributeError: 'View' object has no attribute 'close'
'''
window.run_command('close_file')
decomp_view = window.open_file(file_temp)
decomp_view.set_status('decompressed', filepath)
decomp_view.set_status('decompressed_mtime', str(stat(filepath).st_mtime))
print(decomp_view.get_status('decompressed_mtime'))
decomp_view.set_read_only(True)
def update_decompressed(view):
if not view.get_status('decompressed'):
return
origin = view.get_status('decompressed')
if not exists(origin):
return
mtime = float(view.get_status('decompressed_mtime'))
current = stat(origin).st_mtime
if current <= mtime:
return
output = view.file_name()
if decompress(origin, output) is None:
return
view.set_status('decompressed_mtime', str(stat(origin).st_mtime))
class OpenCompressedFile3(sublime_plugin.EventListener):
'''
Sublime Text Event for the compressor plugin
'''
if hasattr(sublime_plugin.EventListener, 'on_load_async'):
def on_load_async(self, view):
'''
Sublime text 3 async event listener
'''
load_decompress(view)
else:
def on_load(self, view):
'''
Fallback event listener
'''
load_decompress(view)
if hasattr(sublime_plugin.EventListener, 'on_activated_async'):
def on_activated_async(self, view):
update_decompressed(view)
else:
def on_activated(self, view):
update_decompressed(view)
def on_close(self, view):
'''
Cleanup
'''
if view.get_status('decompressed'):
filepath = view.file_name()
remove(filepath)
# Should be empty by now
rmdir(dirname(filepath))