5
from flexget.entry import Entry
6
from flexget.plugin import register_plugin
7
from flexget.utils.cached_input import cached
9
log = logging.getLogger('find')
12
class InputFind(object):
14
Uses local path content as an input, recurses through directories and creates entries for files that match mask.
16
You can specify either the mask key, in shell file matching format, (see python fnmatch module,) or regexp key.
21
path: /storage/movies/
30
regexp: .*\.(avi|mkv)$
34
from flexget import validator
35
root = validator.factory('dict')
36
root.accept('path', key='path', required=True)
37
root.accept('list', key='path').accept('path')
38
root.accept('text', key='mask')
39
root.accept('regexp', key='regexp')
40
root.accept('boolean', key='recursive')
43
def prepare_config(self, config):
44
from fnmatch import translate
45
# If only a single path is passed turn it into a 1 element list
46
if isinstance(config['path'], basestring):
47
config['path'] = [config['path']]
48
config.setdefault('recursive', False)
49
# If mask was specified, turn it in to a regexp
50
if config.get('mask'):
51
config['regexp'] = translate(config['mask'])
52
# If no mask or regexp specified, accept all files
53
if not config.get('regexp'):
54
config['regexp'] = '.'
57
def on_feed_input(self, feed, config):
58
self.prepare_config(config)
60
match = re.compile(config['regexp'], re.IGNORECASE).match
61
# Default to utf-8 if we get None from getfilesystemencoding()
62
fs_encoding = sys.getfilesystemencoding() or 'utf-8'
63
for path in config['path']:
64
log.debug('scanning %s' % path)
65
# unicode causes problems in here (#989)
66
path = path.encode(fs_encoding)
67
path = os.path.expanduser(path)
68
for item in os.walk(path):
69
log.debug('item: %s' % str(item))
71
# If mask fails continue
72
if match(name) is None:
76
# Convert back to unicode
77
e['title'] = name.decode(fs_encoding)
78
except UnicodeDecodeError:
79
log.warning('Filename `%r` in `%s` encoding broken?' %
80
(name.decode('utf-8', 'replace'), item[0]))
82
filepath = os.path.join(item[0], name).decode(fs_encoding)
83
e['location'] = filepath
84
# Windows paths need an extra / prepended to them for url
85
if not filepath.startswith('/'):
86
filepath = '/' + filepath
87
e['url'] = 'file://%s' % (filepath)
89
# If we are not searching recursively, break after first (base) directory
90
if not config['recursive']:
94
register_plugin(InputFind, 'find', api_ver=2)