Skip to content

Commit

Permalink
standardize the monitor_factory api
Browse files Browse the repository at this point in the history
  • Loading branch information
mmerickel committed May 2, 2018
1 parent 874c8fd commit e9d41cd
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/hupper/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def trigger_reload(self):


class IFileMonitorFactory(with_metaclass(abc.ABCMeta)):
def __call__(self, callback):
def __call__(self, callback, **kw):
""" Return an :class:`.IFileMonitor` instance.
``callback`` is a callable to be invoked by the ``IFileMonitor``
Expand Down
4 changes: 2 additions & 2 deletions src/hupper/polling.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ class PollingFileMonitor(threading.Thread, IFileMonitor):
disk. Do not set this too low or it will eat your CPU and kill your drive.
"""
def __init__(self, callback, poll_interval=1):
def __init__(self, callback, interval=1, **kw):
super(PollingFileMonitor, self).__init__()
self.callback = callback
self.poll_interval = poll_interval
self.poll_interval = interval
self.paths = set()
self.mtimes = {}
self.lock = threading.Lock()
Expand Down
27 changes: 13 additions & 14 deletions src/hupper/reloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ class FileMonitorProxy(object):
when it should reload.
"""
def __init__(self, monitor_factory, verbose=1):
self.monitor = monitor_factory(self.file_changed)
monitor = None

def __init__(self, verbose=1):
self.verbose = verbose
self.change_event = threading.Event()
self.changed_paths = set()
Expand Down Expand Up @@ -199,7 +200,12 @@ def _wait_for_changes(self):
self.monitor.clear_changes()

def _start_monitor(self):
self.monitor = FileMonitorProxy(self.monitor_factory, self.verbose)
proxy = FileMonitorProxy(self.verbose)
proxy.monitor = self.monitor_factory(
proxy.file_changed,
interval=self.reload_interval,
)
self.monitor = proxy
self.monitor.start()

def _stop_monitor(self):
Expand All @@ -221,7 +227,7 @@ def _restore_signals(self):
signal.signal(signal.SIGHUP, signal.SIG_DFL)


def find_default_monitor_factory(verbose, reload_interval):
def find_default_monitor_factory(verbose):
spec = os.environ.get('HUPPER_DEFAULT_MONITOR')
if spec:
monitor_factory = resolve_spec(spec)
Expand All @@ -230,19 +236,13 @@ def find_default_monitor_factory(verbose, reload_interval):
print('File monitor backend: ' + spec)

elif is_watchdog_supported():
from .watchdog import WatchdogFileMonitor

def monitor_factory(callback):
return WatchdogFileMonitor(callback)
from .watchdog import WatchdogFileMonitor as monitor_factory

if verbose > 1:
print('File monitor backend: watchdog')

else:
from .polling import PollingFileMonitor

def monitor_factory(callback):
return PollingFileMonitor(callback, reload_interval)
from .polling import PollingFileMonitor as monitor_factory

if verbose > 1:
print('File monitor backend: polling')
Expand Down Expand Up @@ -295,8 +295,7 @@ def start_reloader(
return get_reloader()

if monitor_factory is None:
monitor_factory = find_default_monitor_factory(
verbose, reload_interval)
monitor_factory = find_default_monitor_factory(verbose)

reloader = Reloader(
worker_path=worker_path,
Expand Down
2 changes: 1 addition & 1 deletion src/hupper/watchdog.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class WatchdogFileMonitor(FileSystemEventHandler, Observer, IFileMonitor):
``callback`` is a callable that accepts a path to a changed file.
"""
def __init__(self, callback):
def __init__(self, callback, **kw):
super(WatchdogFileMonitor, self).__init__()
self.callback = callback
self.paths = set()
Expand Down
12 changes: 7 additions & 5 deletions tests/test_reloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@

here = os.path.abspath(os.path.dirname(__file__))

def make_proxy(*args, **kwargs):
def make_proxy(monitor_factory):
from hupper.reloader import FileMonitorProxy
return FileMonitorProxy(*args, **kwargs)
proxy = FileMonitorProxy()
proxy.monitor = monitor_factory(proxy.file_changed)
return proxy

def test_proxy_proxies():
class DummyMonitor(object):
started = stopped = joined = False

def __call__(self, cb):
def __call__(self, cb, **kw):
self.cb = cb
return self

Expand All @@ -34,7 +36,7 @@ def join(self):

def test_proxy_expands_paths(tmpdir):
class DummyMonitor(object):
def __call__(self, cb):
def __call__(self, cb, **kw):
self.cb = cb
self.paths = []
return self
Expand All @@ -59,7 +61,7 @@ def add_path(self, path):

def test_proxy_tracks_changes(capsys):
class DummyMonitor(object):
def __call__(self, cb):
def __call__(self, cb, **kw):
self.cb = cb
return self

Expand Down

0 comments on commit e9d41cd

Please sign in to comment.