Skip to content

Commit

Permalink
Merge pull request #37 from 4Catalyzer/recursive-file-list-keys
Browse files Browse the repository at this point in the history
list nested keys in file's list_keys
  • Loading branch information
itajaja authored Jun 22, 2017
2 parents cf3b878 + 4143813 commit 8b8d529
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
16 changes: 16 additions & 0 deletions flask_annex/compat.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import fnmatch
import os
import sys

# -----------------------------------------------------------------------------
Expand All @@ -10,3 +12,17 @@
string_types = (str, unicode) # flake8: noqa
else:
string_types = (str,)


def recursive_glob(rootdir, pattern='*'):
"""Search recursively for files matching a specified pattern.
Adapted from http://stackoverflow.com/questions/2186525/use-a-glob-to-find-files-recursively-in-python
"""

matches = []
for root, dirnames, filenames in os.walk(rootdir):
for filename in fnmatch.filter(filenames, pattern):
matches.append(os.path.join(root, filename))

return matches
7 changes: 3 additions & 4 deletions flask_annex/file.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import errno
import glob
import os
import shutil

import flask

from .base import AnnexBase
from .compat import string_types
from .compat import recursive_glob, string_types

# -----------------------------------------------------------------------------

Expand Down Expand Up @@ -57,8 +56,8 @@ def get_file(self, key, out_file):
shutil.copyfileobj(in_fp, out_file)

def list_keys(self, prefix):
pattern = self._get_filename('{}*'.format(prefix))
filenames = glob.glob(pattern)
root = self._get_filename(prefix)
filenames = [root] if os.path.isfile(root) else recursive_glob(root)

return [
os.path.relpath(filename, self._root_path)
Expand Down
12 changes: 12 additions & 0 deletions tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,18 @@ def test_get_filename(self, tmpdir, annex):
assert open(out_filename).read() == '1\n'

def test_list_keys(self, annex):
annex.save_file('foo/qux.txt', BytesIO(b'3\n'))
assert sorted(annex.list_keys('foo/')) == [
'foo/bar.txt',
'foo/baz.json',
'foo/qux.txt',
]

# check that dangling '/' is not relevant
assert sorted(annex.list_keys('foo/')) == \
sorted(annex.list_keys('foo'))

def test_list_keys_nested(self, annex):
assert sorted(annex.list_keys('foo/')) == [
'foo/bar.txt',
'foo/baz.json',
Expand Down

0 comments on commit 8b8d529

Please sign in to comment.