Merge pull request #1233 into develop

This commit is contained in:
ZyX 2014-12-28 23:53:59 +03:00
commit e3ee4ef951
1 changed files with 29 additions and 9 deletions

View File

@ -18,15 +18,19 @@ class UvNotFound(NotImplementedError):
pyuv = None pyuv = None
pyuv_version_info = None
def import_pyuv(): def import_pyuv():
global pyuv global pyuv
global pyuv_version_info
if not pyuv: if not pyuv:
try: try:
pyuv = __import__('pyuv') pyuv = __import__('pyuv')
except ImportError: except ImportError:
raise UvNotFound raise UvNotFound
else:
pyuv_version_info = tuple((int(c) for c in pyuv.__version__.split('.')))
class UvThread(Thread): class UvThread(Thread):
@ -34,14 +38,18 @@ class UvThread(Thread):
def __init__(self, loop): def __init__(self, loop):
self.uv_loop = loop self.uv_loop = loop
self.async_handle = pyuv.Async(loop, self._async_cb)
super(UvThread, self).__init__() super(UvThread, self).__init__()
def _async_cb(self, handle):
self.uv_loop.stop()
self.async_handle.close()
def run(self): def run(self):
while True:
self.uv_loop.run() self.uv_loop.run()
def join(self): def join(self):
self.uv_loop.stop() self.async_handle.send()
return super(UvThread, self).join() return super(UvThread, self).join()
@ -72,18 +80,30 @@ class UvWatcher(object):
self.lock = RLock() self.lock = RLock()
self.loop = start_uv_thread() self.loop = start_uv_thread()
self.fenc = get_preferred_file_name_encoding() self.fenc = get_preferred_file_name_encoding()
if pyuv_version_info >= (1, 0):
self._start_watch = self._start_watch_1_x
else:
self._start_watch = self._start_watch_0_x
def watch(self, path): def _start_watch_1_x(self, path):
path = normpath(path, self.fenc) handle = pyuv.fs.FSEvent(self.loop)
with self.lock: handle.start(path, 0, partial(self._record_event, path))
if path not in self.watches: self.watches[path] = handle
try:
def _start_watch_0_x(self, path):
self.watches[path] = pyuv.fs.FSEvent( self.watches[path] = pyuv.fs.FSEvent(
self.loop, self.loop,
path, path,
partial(self._record_event, path), partial(self._record_event, path),
pyuv.fs.UV_CHANGE | pyuv.fs.UV_RENAME pyuv.fs.UV_CHANGE | pyuv.fs.UV_RENAME
) )
def watch(self, path):
path = normpath(path, self.fenc)
with self.lock:
if path not in self.watches:
try:
self._start_watch(path)
except pyuv.error.FSEventError as e: except pyuv.error.FSEventError as e:
code = e.args[0] code = e.args[0]
if code == pyuv.errno.UV_ENOENT: if code == pyuv.errno.UV_ENOENT: