PiRSquared17 / py-unrar2

Automatically exported from code.google.com/p/py-unrar2
MIT License
0 stars 0 forks source link

Need a way to filter read_files for multiple file types, file extensions #2

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Define a string or list with multiple file types (e.g.
['.jpg','.jpeg','.gif','.png']
2. Pass string to RarFile.read_files()

What is the expected output? What do you see instead?
I expect to get a list of files back that match any of the given
extensions. Instead, I get no matches found.

What version of the product are you using? On what operating system?
0.96 on windows xp

Please provide any additional information below.

Original issue reported on code.google.com by tal...@gmail.com on 21 Jan 2010 at 9:38

GoogleCodeExporter commented 9 years ago
Instead of bloating the library, I've built in a flexible solution based on the
oncept of functional programming.

To perform a complex filtering you should use callbacks (predicates).
Predicate is a simple function that processes a given value (RarInfo record) and
returns boolean True (ok to extract the file) or False (skip it).

Your problem can be resolved in several ways.

# simpliest way, not very scalable
archive.extract(lambda x:x.filename.endswith('.py') or 
x.filename.endswith('.txt'))

# nice way if you care only about extensions (i.e. "file types")
import os.path

archive.extract(lambda x:os.path.splitext(x.filename)[1] in ['txt','py'])

# some powerful functional magic to aggregate several similar predicates
archive.extract(lambda x:any(x.filename.endswith(y) for y in ['.txt','.py']))

Original comment by yk4e...@gmail.com on 24 Jan 2010 at 9:15