Skip to content

Commit 6ebfbd5

Browse files
committed
Merge branch 'watch-files' of https://github.com/bpython/bpython into cleaning
2 parents ff8a252 + 87b0a9c commit 6ebfbd5

File tree

4 files changed

+88
-1
lines changed

4 files changed

+88
-1
lines changed

bpython/curtsies.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
import sys
44
import code
55
import logging
6+
from subprocess import Popen, PIPE
67
from optparse import Option
78
from itertools import izip
9+
from functools import wraps
810

911
import curtsies
1012
import curtsies.window
@@ -17,6 +19,8 @@
1719
from bpython.translations import _
1820
from bpython.importcompletion import find_iterator
1921

22+
import wdtest
23+
2024
repl = None # global for `from bpython.curtsies import repl`
2125
#WARNING Will be a problem if more than one repl is ever instantiated this way
2226

@@ -71,15 +75,38 @@ def mainloop(config, locals_, banner, interp=None, paste=None, interactive=True)
7175
hide_cursor=False,
7276
extra_bytes_callback=input_generator.unget_bytes) as window:
7377

78+
reload_requests = []
79+
def request_reload():
80+
reload_requests.append('reload!')
7481
refresh_requests = []
7582
def request_refresh():
7683
refresh_requests.append(curtsies.events.RefreshRequestEvent())
84+
85+
watcher = wdtest.ModuleChangedEventHandler([], request_reload)
86+
87+
orig_import = __builtins__['__import__']
88+
@wraps(orig_import)
89+
def new_import(name, globals={}, locals={}, fromlist=[], level=-1):
90+
m = orig_import(name, globals=globals, locals=locals, fromlist=fromlist)
91+
watcher.add_module(m.__file__)
92+
return m
93+
__builtins__['__import__'] = new_import
94+
7795
def event_or_refresh(timeout=None):
7896
while True:
7997
if refresh_requests:
8098
yield refresh_requests.pop()
8199
else:
82-
yield input_generator.send(timeout)
100+
while True:
101+
if reload_requests:
102+
del reload_requests[:]
103+
e = '<F6>'
104+
print 'asdf'
105+
else:
106+
e = input_generator.send(.2)
107+
if e is not None:
108+
break
109+
yield e
83110

84111
global repl # global for easy introspection `from bpython.curtsies import repl`
85112
with Repl(config=config,

bpython/curtsiesfrontend/beeper.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import time
2+
import sys
3+
4+
if __name__ == '__main__':
5+
while True:
6+
sys.stdout.write('beep\n')
7+
sys.stdout.flush()
8+
time.sleep(5)
9+

bpython/frontends/curtsies/repl.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import sys
1111
import tempfile
1212
import threading
13+
import time
1314
import unicodedata
1415

1516
from bpython import autocomplete
@@ -425,6 +426,7 @@ def process_event(self, e):
425426
elif e in key_dispatch[self.config.reimport_key]:
426427
self.clear_modules_and_reevaluate()
427428
self.update_completion()
429+
self.status_bar.message(_('Reloaded at ')+time.strftime('%H:%M:%S'))
428430
elif e in key_dispatch[self.config.undo_key]: #ctrl-r for undo
429431
self.undo()
430432
self.update_completion()

wdtest.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import time
2+
import os
3+
from collections import defaultdict
4+
5+
from bpython import importcompletion
6+
7+
from watchdog.observers import Observer
8+
from watchdog.events import FileSystemEventHandler
9+
10+
class ModuleChangedEventHandler(FileSystemEventHandler):
11+
def __init__(self, paths, on_change):
12+
self.dirs = defaultdict(list)
13+
self.on_change = on_change
14+
self.observer = Observer()
15+
for path in paths:
16+
self.add_module(path)
17+
self.observer.start()
18+
19+
def add_module(self, path):
20+
"""Add a python module to track changes to"""
21+
path = os.path.abspath(path)
22+
for suff in importcompletion.SUFFIXES:
23+
if path.endswith(suff):
24+
path = path[:-len(suff)]
25+
break
26+
dirname = os.path.dirname(path)
27+
if dirname not in self.dirs:
28+
self.observer.schedule(self, dirname, recursive=False)
29+
self.dirs[os.path.dirname(path)].append(path)
30+
31+
def on_any_event(self, event):
32+
dirpath = os.path.dirname(event.src_path)
33+
paths = [path + suffix for suffix in importcompletion.SUFFIXES
34+
for path in self.dirs[dirpath]]
35+
if event.src_path in paths:
36+
pass
37+
#self.on_change()
38+
self.on_change()
39+
40+
if __name__ == '__main__':
41+
m = ModuleChangedEventHandler([])
42+
m.add_module('./wdtest.py')
43+
try:
44+
while True:
45+
time.sleep(1)
46+
except KeyboardInterrupt:
47+
m.observer.stop()
48+
m.observer.join()
49+

0 commit comments

Comments
 (0)