Skip to content

Commit

Permalink
Reloader: Improve should_handle()
Browse files Browse the repository at this point in the history
Use pathlib for easier path manipulation, and to make the
filename argument accept more types.

In addition, the check if the file was in searchpath is unneeded,
so remove it. By definition when we call easywatch.watch() on
searchpath, every callback must be for a file under search path.
In addition, in event_handler() we ASSUME that file is under
search path, since we call os.path.relpath(src_path, searchpath).
That call would explode if src_path wasn't under searchpath.
  • Loading branch information
NickCrews committed Feb 15, 2021
1 parent 714ea01 commit 2aafa70
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 8 deletions.
10 changes: 4 additions & 6 deletions staticjinja/reloader.py
@@ -1,4 +1,5 @@
import os
from pathlib import Path


class Reloader(object):
Expand All @@ -21,17 +22,14 @@ def searchpath(self):
def should_handle(self, event_type, filename):
"""Check if an event should be handled.
An event should be handled if a file in the searchpath was modified.
An event should be handled if a file was created or modified, and
still exists.
:param event_type: a string, representing the type of event
:param filename: the path to the file that triggered the event.
"""
return (
event_type in ("modified", "created")
and filename.startswith(self.searchpath)
and os.path.isfile(filename)
)
return event_type in ("modified", "created") and Path(filename).is_file()

def event_handler(self, event_type, src_path):
"""Re-render templates if they are modified.
Expand Down
2 changes: 0 additions & 2 deletions tests/test_staticjinja.py
Expand Up @@ -160,12 +160,10 @@ def fake_watch(self):
def test_should_handle(reloader, root_path, template_path):
exists = template_path / "template1.html"
DNE = template_path / "DNE.html"
outside_searchpath = root_path / "file.txt"
assert reloader.should_handle("created", str(exists))
assert reloader.should_handle("modified", str(exists))
assert not reloader.should_handle("deleted", str(exists))
assert not reloader.should_handle("modified", str(DNE))
assert not reloader.should_handle("modified", str(outside_searchpath))


def test_event_handler(monkeypatch, reloader, template_path):
Expand Down

0 comments on commit 2aafa70

Please sign in to comment.