3838from extractcode import patches
3939from extractcode import special_package
4040
41+ from extractcode import libarchive2
4142from extractcode import patch
4243from extractcode import sevenzip
43- from extractcode import libarchive2
44+
4445from extractcode .uncompress import uncompress_gzip
4546from extractcode .uncompress import uncompress_bzip2
4647
7980 - http://en.wikipedia.org/wiki/List_of_file_formats#Archive_and_compressed
8081"""
8182
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+ )
8496
8597
8698def can_extract (location ):
@@ -96,13 +108,17 @@ def can_extract(location):
96108
97109def should_extract (location , kinds , ignore_pattern = ()):
98110 """
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
101112 """
102113 location = os .path .abspath (os .path .expanduser (location ))
103114 ignore_pattern = {extension : 'User ignore: Supplied by --ignore' for extension in ignore_pattern }
104115 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 :
106122 return True
107123
108124
@@ -113,15 +129,19 @@ def get_extractor(location, kinds=all_kinds):
113129 """
114130 assert location
115131 location = os .path .abspath (os .path .expanduser (location ))
116- extractors = get_extractors (location , kinds )
132+ extractors = get_extractors (location , kinds = kinds )
117133 if not extractors :
134+ if TRACE_DEEP :
135+ logger .debug (f' get_extractor: not extractors: { extractors } ' )
118136 return None
119137
120138 if len (extractors ) == 2 :
121139 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+ )
125145 return nested_extractor
126146 elif len (extractors ) == 1 :
127147 return extractors [0 ]
@@ -135,23 +155,38 @@ def get_extractors(location, kinds=all_kinds):
135155 location or an empty list.
136156 """
137157 handler = get_best_handler (location , kinds )
158+ if TRACE_DEEP :
159+ logger .debug (f' get_extractors: handler: { handler } ' )
160+
138161 return handler and handler .extractors or []
139162
140163
141164def get_best_handler (location , kinds = all_kinds ):
142165 """
143- Return the best handler of None for the file at location.
166+ Return the best handler for the file at ` location` or None .
144167 """
145168 location = os .path .abspath (os .path .expanduser (location ))
146169 if not filetype .is_file (location ):
147170 return
171+
148172 handlers = list (get_handlers (location ))
149173 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
151185
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
155190
156191
157192def get_handlers (location ):
@@ -177,6 +212,8 @@ def get_handlers(location):
177212
178213 # default to False
179214 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 } ' )
180217 mime_matched = handler .mimetypes and any (m in mtype for m in handler .mimetypes )
181218 exts = handler .extensions
182219 if exts :
@@ -201,10 +238,18 @@ def score_handlers(handlers):
201238 Score candidate handlers. Higher score is better.
202239 """
203240 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+ )
204248 score = 0
205249 # increment kind value: higher kinds numerical values are more
206250 # specific by design
207251 score += handler .kind
252+ if TRACE_DEEP : logger .debug (f' score_handlers: score += handler.kind { score } ' )
208253
209254 # increment score based on matched criteria
210255 if type_matched and mime_matched and extension_matched :
@@ -255,6 +300,10 @@ def pick_best_handler(candidates, kinds):
255300 """
256301 # sort by increasing scores
257302 scored = sorted (candidates , reverse = True )
303+
304+ if TRACE_DEEP :
305+ logger .debug (f' pick_best_handler: scored: { scored } ' )
306+
258307 if not scored :
259308 return
260309
@@ -994,7 +1043,7 @@ def try_to_extract(location, target_dir, extractor):
9941043 strict = False
9951044)
9961045
997- PatchHandler = Handler (
1046+ ` PatchHandler = Handler (
9981047 name = 'Patch' ,
9991048 filetypes = ('diff' , 'patch' ,),
10001049 mimetypes = ('text/x-diff' ,),
0 commit comments