38
38
from extractcode import patches
39
39
from extractcode import special_package
40
40
41
+ from extractcode import libarchive2
41
42
from extractcode import patch
42
43
from extractcode import sevenzip
43
- from extractcode import libarchive2
44
+
44
45
from extractcode .uncompress import uncompress_gzip
45
46
from extractcode .uncompress import uncompress_bzip2
46
47
79
80
- http://en.wikipedia.org/wiki/List_of_file_formats#Archive_and_compressed
80
81
"""
81
82
82
- # if strict, all hanlders criteria must be matched for it to be selected
83
- Handler = namedtuple ('Handler' , ['name' , 'filetypes' , 'mimetypes' , 'extensions' , 'kind' , 'extractors' , 'strict' ])
83
+ # if strict, all handlers criteria must be matched for a handler to be selected
84
+ Handler = namedtuple (
85
+ 'Handler' ,
86
+ [
87
+ 'name' ,
88
+ 'filetypes' ,
89
+ 'mimetypes' ,
90
+ 'extensions' ,
91
+ 'kind' ,
92
+ 'extractors' ,
93
+ 'strict' ,
94
+ ]
95
+ )
84
96
85
97
86
98
def can_extract (location ):
@@ -96,13 +108,17 @@ def can_extract(location):
96
108
97
109
def should_extract (location , kinds , ignore_pattern = ()):
98
110
"""
99
- Return True if this location should be extracted based on the provided
100
- kinds
111
+ Return True if this location should be extracted based on the provided kinds
101
112
"""
102
113
location = os .path .abspath (os .path .expanduser (location ))
103
114
ignore_pattern = {extension : 'User ignore: Supplied by --ignore' for extension in ignore_pattern }
104
115
should_ignore = is_ignored (location , ignore_pattern )
105
- if get_extractor (location , kinds ) and not should_ignore :
116
+ extractor = get_extractor (location , kinds = kinds )
117
+
118
+ if TRACE_DEEP :
119
+ logger .debug (f' should_extract: extractor: { extractor } , should_ignore: { should_ignore } ' )
120
+
121
+ if extractor and not should_ignore :
106
122
return True
107
123
108
124
@@ -113,15 +129,19 @@ def get_extractor(location, kinds=all_kinds):
113
129
"""
114
130
assert location
115
131
location = os .path .abspath (os .path .expanduser (location ))
116
- extractors = get_extractors (location , kinds )
132
+ extractors = get_extractors (location , kinds = kinds )
117
133
if not extractors :
134
+ if TRACE_DEEP :
135
+ logger .debug (f' get_extractor: not extractors: { extractors } ' )
118
136
return None
119
137
120
138
if len (extractors ) == 2 :
121
139
extractor1 , extractor2 = extractors
122
- nested_extractor = functional .partial (extract_twice ,
123
- extractor1 = extractor1 ,
124
- extractor2 = extractor2 )
140
+ nested_extractor = functional .partial (
141
+ extract_twice ,
142
+ extractor1 = extractor1 ,
143
+ extractor2 = extractor2 ,
144
+ )
125
145
return nested_extractor
126
146
elif len (extractors ) == 1 :
127
147
return extractors [0 ]
@@ -135,23 +155,38 @@ def get_extractors(location, kinds=all_kinds):
135
155
location or an empty list.
136
156
"""
137
157
handler = get_best_handler (location , kinds )
158
+ if TRACE_DEEP :
159
+ logger .debug (f' get_extractors: handler: { handler } ' )
160
+
138
161
return handler and handler .extractors or []
139
162
140
163
141
164
def get_best_handler (location , kinds = all_kinds ):
142
165
"""
143
- Return the best handler of None for the file at location.
166
+ Return the best handler for the file at ` location` or None .
144
167
"""
145
168
location = os .path .abspath (os .path .expanduser (location ))
146
169
if not filetype .is_file (location ):
147
170
return
171
+
148
172
handlers = list (get_handlers (location ))
149
173
if TRACE_DEEP :
150
- logger .debug ('get_best_handler: handlers: %(handlers)r ' % locals ())
174
+ logger .debug (f' get_best_handler: handlers: { handlers } ' )
175
+ if not handlers :
176
+ return
177
+
178
+ candidates = list (score_handlers (handlers ))
179
+ if TRACE_DEEP :
180
+ logger .debug (f' get_best_handler: candidates: { candidates } ' )
181
+ if not candidates :
182
+ if TRACE_DEEP :
183
+ logger .debug (f' get_best_handler: candidates: { candidates } ' )
184
+ return
151
185
152
- if handlers :
153
- candidates = score_handlers (handlers )
154
- return candidates and pick_best_handler (candidates , kinds )
186
+ picked = pick_best_handler (candidates , kinds = kinds )
187
+ if TRACE_DEEP :
188
+ logger .debug (f' get_best_handler: picked: { picked } ' )
189
+ return picked
155
190
156
191
157
192
def get_handlers (location ):
@@ -177,6 +212,8 @@ def get_handlers(location):
177
212
178
213
# default to False
179
214
type_matched = handler .filetypes and any (t in ftype for t in handler .filetypes )
215
+ if TRACE_DEEP :
216
+ logger .debug (f' get_handlers: handler.filetypes={ handler .filetypes } ' )
180
217
mime_matched = handler .mimetypes and any (m in mtype for m in handler .mimetypes )
181
218
exts = handler .extensions
182
219
if exts :
@@ -201,10 +238,18 @@ def score_handlers(handlers):
201
238
Score candidate handlers. Higher score is better.
202
239
"""
203
240
for handler , type_matched , mime_matched , extension_matched in handlers :
241
+ if TRACE_DEEP :
242
+ logger .debug (
243
+ f' score_handlers: handler={ handler } , '
244
+ f'type_matched={ type_matched } , '
245
+ f'mime_matched={ mime_matched } , '
246
+ f'extension_matched={ extension_matched } '
247
+ )
204
248
score = 0
205
249
# increment kind value: higher kinds numerical values are more
206
250
# specific by design
207
251
score += handler .kind
252
+ if TRACE_DEEP : logger .debug (f' score_handlers: score += handler.kind { score } ' )
208
253
209
254
# increment score based on matched criteria
210
255
if type_matched and mime_matched and extension_matched :
@@ -255,6 +300,10 @@ def pick_best_handler(candidates, kinds):
255
300
"""
256
301
# sort by increasing scores
257
302
scored = sorted (candidates , reverse = True )
303
+
304
+ if TRACE_DEEP :
305
+ logger .debug (f' pick_best_handler: scored: { scored } ' )
306
+
258
307
if not scored :
259
308
return
260
309
@@ -994,7 +1043,7 @@ def try_to_extract(location, target_dir, extractor):
994
1043
strict = False
995
1044
)
996
1045
997
- PatchHandler = Handler (
1046
+ ` PatchHandler = Handler (
998
1047
name = 'Patch' ,
999
1048
filetypes = ('diff' , 'patch' ,),
1000
1049
mimetypes = ('text/x-diff' ,),
0 commit comments