-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Hot reload #362
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
Hot reload #362
Changes from all commits
335e038
5723533
5b10597
cc6c21c
493f99d
23882f5
ab53330
13202e5
94bf7c0
7c77bc8
8c993fa
631da7b
08bbdc1
410865e
46383d1
1e4eae6
e714af3
d74498f
494243e
1ae7dca
f22ea7d
353d79c
4654e5f
beadd63
407e24c
63033ef
fb26306
927d69b
d410c9c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import collections | ||
import os | ||
import re | ||
import time | ||
|
||
|
||
def watch(folders, on_change, pattern=None, sleep_time=0.1): | ||
pattern = re.compile(pattern) if pattern else None | ||
watched = collections.defaultdict(lambda: -1) | ||
|
||
def walk(): | ||
walked = [] | ||
for folder in folders: | ||
for current, _, files, in os.walk(folder): | ||
for f in files: | ||
if pattern and not pattern.search(f): | ||
continue | ||
path = os.path.join(current, f) | ||
|
||
info = os.stat(path) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. os.path.getmtime might be cleaner here. I thought there was platform dependence issue at first (since otherwise why would they even implement There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yea, some methods in that module seems to be wrapper around There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍, either way is good, just seemed a bit strange, seems to break There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the |
||
new_time = info.st_mtime | ||
|
||
if new_time > watched[path] > 0: | ||
on_change(path, new_time, False) | ||
|
||
watched[path] = new_time | ||
walked.append(path) | ||
|
||
# Look for deleted files | ||
for w in [x for x in watched.keys() if x not in walked]: | ||
del watched[w] | ||
on_change(w, -1, True) | ||
|
||
while True: | ||
walk() | ||
time.sleep(sleep_time) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Neat, didn't know you could do this.