Skip to content

Commit 2077aa6

Browse files
using watchdog to reload
1 parent 4c6b243 commit 2077aa6

File tree

2 files changed

+76
-15
lines changed

2 files changed

+76
-15
lines changed

bpython/curtsies.py

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
import sys
44
import code
55
import logging
6-
import select
76
from subprocess import Popen, PIPE
87
from optparse import Option
98
from itertools import izip
9+
from functools import wraps
1010

1111
import curtsies
1212
import curtsies.window
@@ -19,6 +19,8 @@
1919
from bpython.translations import _
2020
from bpython.importcompletion import find_iterator
2121

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

@@ -73,28 +75,38 @@ def mainloop(config, locals_, banner, interp=None, paste=None, interactive=True)
7375
hide_cursor=False,
7476
extra_bytes_callback=input_generator.unget_bytes) as window:
7577

76-
p = Popen(['python', '-m', 'bpython.curtsiesfrontend.beeper'], stdout=PIPE)
77-
78+
reload_requests = []
79+
def request_reload():
80+
reload_requests.append('reload!')
7881
refresh_requests = []
7982
def request_refresh():
8083
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+
8195
def event_or_refresh(timeout=None):
8296
while True:
8397
if refresh_requests:
8498
yield refresh_requests.pop()
8599
else:
86-
try:
87-
rs, ws, es = select.select([input_generator, p.stdout], [], [])
88-
except select.error as e:
89-
print e
90-
yield curtsies.events.SigIntEvent()
91-
else:
92-
for r in rs:
93-
if r is input_generator:
94-
yield input_generator.send(timeout)
95-
else:
96-
p.stdout.readline()
97-
yield u'a'
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
98110

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

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)