Readd support for 0.x

This commit is contained in:
ZyX 2014-12-28 23:50:45 +03:00
parent 3474c0dad5
commit eb9c14096b
1 changed files with 22 additions and 3 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):
@ -76,15 +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 _start_watch_1_x(self, path):
handle = pyuv.fs.FSEvent(self.loop)
self.watches[path] = handle
handle.start(path, 0, partial(self._record_event, path))
def _start_watch_0_x(self, path):
self.watches[path] = pyuv.fs.FSEvent(
self.loop,
path,
partial(self._record_event, path),
pyuv.fs.UV_CHANGE | pyuv.fs.UV_RENAME
)
def watch(self, path): def watch(self, path):
path = normpath(path, self.fenc) path = normpath(path, self.fenc)
with self.lock: with self.lock:
if path not in self.watches: if path not in self.watches:
try: try:
handle = pyuv.fs.FSEvent(self.loop) self._start_watch(path)
self.watches[path] = handle
handle.start(path, 0, partial(self._record_event, 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: