Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix FileSystemRegexFinder to correctly find files in subdirectories #4

Merged
merged 1 commit into from
Nov 19, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions jstemplate/finders.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,16 @@ def find(self, name):
return matches.items()

def _update_matches(self, matches, directory, pattern):
for filename in os.listdir(directory):
if not os.path.isdir(filename):
match = pattern.match(filename)
# Walk recursively under directory and search for matches
for root, dirs, files in os.walk(directory):
# Build relative path to current directory, omit .
relative_root = os.path.relpath(root, directory).replace('.', '', 1)
for filename in files:
# Build relative path to file
relative_filepath = os.path.join(relative_root, filename)
match = pattern.match(relative_filepath)
if match is not None and match.group(1) not in matches:
matches[match.group(1)] = os.path.join(directory, filename)

matches[match.group(1)] = os.path.join(directory, relative_filepath)


def _get_app_template_dirs():
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>Mustache's template full of {{ foo }} and \.</p>
1 change: 1 addition & 0 deletions jstemplate/tests/templates/subdirectory/testtemplate.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>Mustache's template full of {{ foo }} and \.</p>
8 changes: 8 additions & 0 deletions jstemplate/tests/test_finders.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,14 @@ def test_find_many_files(self):
)


@override_settings(JSTEMPLATE_DIRS=[os.path.join(here, "templates")])
def test_find_files_in_subdirectories(self):
self.assertEqual(
list(self.finder.find('(subdirectory.*)')),
[('subdirectory/subdirectory2/testtemplate', os.path.join(here, 'templates', 'subdirectory', 'subdirectory2', 'testtemplate.html')),
('subdirectory/testtemplate', os.path.join(here, 'templates', 'subdirectory', 'testtemplate.html'))])


class AppFinderTest(TestCase):
@property
def finder(self):
Expand Down