From ac4b6fc7b51da9eadd965bdcc56bc92e90905763 Mon Sep 17 00:00:00 2001 From: croneter Date: Fri, 18 Dec 2020 18:08:39 +0100 Subject: [PATCH] Fixes to enable an import of the watchdog module --- resources/lib/watchdog/events.py | 2 +- resources/lib/watchdog/observers/__init__.py | 4 +- resources/lib/watchdog/observers/api.py | 4 +- resources/lib/watchdog/observers/fsevents.py | 4 +- resources/lib/watchdog/observers/fsevents2.py | 4 +- resources/lib/watchdog/observers/inotify.py | 4 +- .../lib/watchdog/observers/inotify_buffer.py | 6 +- resources/lib/watchdog/observers/inotify_c.py | 2 +- resources/lib/watchdog/observers/kqueue.py | 8 +-- resources/lib/watchdog/observers/polling.py | 6 +- .../observers/read_directory_changes.py | 6 +- resources/lib/watchdog/tricks/__init__.py | 4 +- resources/lib/watchdog/utils/bricks.py | 67 ++++++++++++++++++- resources/lib/watchdog/watchmedo.py | 36 +++++----- 14 files changed, 111 insertions(+), 46 deletions(-) diff --git a/resources/lib/watchdog/events.py b/resources/lib/watchdog/events.py index 73e82e8e..4ec2b5c4 100644 --- a/resources/lib/watchdog/events.py +++ b/resources/lib/watchdog/events.py @@ -88,7 +88,7 @@ Event Handler Classes import os.path import logging import re -from watchdog.utils.patterns import match_any_paths +from .utils.patterns import match_any_paths EVENT_TYPE_MOVED = 'moved' diff --git a/resources/lib/watchdog/observers/__init__.py b/resources/lib/watchdog/observers/__init__.py index f1d437a3..ac6af349 100644 --- a/resources/lib/watchdog/observers/__init__.py +++ b/resources/lib/watchdog/observers/__init__.py @@ -54,8 +54,8 @@ Class Platforms Note """ import warnings -from watchdog.utils import platform -from watchdog.utils import UnsupportedLibc +from ..utils import platform +from ..utils import UnsupportedLibc if platform.is_linux(): try: diff --git a/resources/lib/watchdog/observers/api.py b/resources/lib/watchdog/observers/api.py index 6a30903d..06e140a9 100644 --- a/resources/lib/watchdog/observers/api.py +++ b/resources/lib/watchdog/observers/api.py @@ -19,8 +19,8 @@ import queue import threading from pathlib import Path -from watchdog.utils import BaseThread -from watchdog.utils.bricks import SkipRepeatsQueue +from ..utils import BaseThread +from ..utils.bricks import SkipRepeatsQueue DEFAULT_EMITTER_TIMEOUT = 1 # in seconds. DEFAULT_OBSERVER_TIMEOUT = 1 # in seconds. diff --git a/resources/lib/watchdog/observers/fsevents.py b/resources/lib/watchdog/observers/fsevents.py index ecfdcee4..9cda4480 100644 --- a/resources/lib/watchdog/observers/fsevents.py +++ b/resources/lib/watchdog/observers/fsevents.py @@ -28,7 +28,7 @@ import threading import unicodedata import _watchdog_fsevents as _fsevents -from watchdog.events import ( +from ..events import ( FileDeletedEvent, FileModifiedEvent, FileCreatedEvent, @@ -39,7 +39,7 @@ from watchdog.events import ( DirMovedEvent ) -from watchdog.observers.api import ( +from .api import ( BaseObserver, EventEmitter, DEFAULT_EMITTER_TIMEOUT, diff --git a/resources/lib/watchdog/observers/fsevents2.py b/resources/lib/watchdog/observers/fsevents2.py index f0b4750d..6fd02f4d 100644 --- a/resources/lib/watchdog/observers/fsevents2.py +++ b/resources/lib/watchdog/observers/fsevents2.py @@ -26,7 +26,7 @@ import queue import unicodedata from threading import Thread -from watchdog.events import ( +from ..events import ( FileDeletedEvent, FileModifiedEvent, FileCreatedEvent, @@ -36,7 +36,7 @@ from watchdog.events import ( DirCreatedEvent, DirMovedEvent ) -from watchdog.observers.api import ( +from .api import ( BaseObserver, EventEmitter, DEFAULT_EMITTER_TIMEOUT, diff --git a/resources/lib/watchdog/observers/inotify.py b/resources/lib/watchdog/observers/inotify.py index 5cb8c5dd..e938dd24 100644 --- a/resources/lib/watchdog/observers/inotify.py +++ b/resources/lib/watchdog/observers/inotify.py @@ -70,14 +70,14 @@ import os import threading from .inotify_buffer import InotifyBuffer -from watchdog.observers.api import ( +from .api import ( EventEmitter, BaseObserver, DEFAULT_EMITTER_TIMEOUT, DEFAULT_OBSERVER_TIMEOUT ) -from watchdog.events import ( +from ..events import ( DirDeletedEvent, DirModifiedEvent, DirMovedEvent, diff --git a/resources/lib/watchdog/observers/inotify_buffer.py b/resources/lib/watchdog/observers/inotify_buffer.py index e5ea49df..d5f1f45a 100644 --- a/resources/lib/watchdog/observers/inotify_buffer.py +++ b/resources/lib/watchdog/observers/inotify_buffer.py @@ -15,9 +15,9 @@ # limitations under the License. import logging -from watchdog.utils import BaseThread -from watchdog.utils.delayed_queue import DelayedQueue -from watchdog.observers.inotify_c import Inotify +from ..utils import BaseThread +from ..utils.delayed_queue import DelayedQueue +from .inotify_c import Inotify logger = logging.getLogger(__name__) diff --git a/resources/lib/watchdog/observers/inotify_c.py b/resources/lib/watchdog/observers/inotify_c.py index 61452ddc..bd1dadbe 100644 --- a/resources/lib/watchdog/observers/inotify_c.py +++ b/resources/lib/watchdog/observers/inotify_c.py @@ -23,7 +23,7 @@ import ctypes import ctypes.util from functools import reduce from ctypes import c_int, c_char_p, c_uint32 -from watchdog.utils import UnsupportedLibc +from ..utils import UnsupportedLibc def _load_libc(): diff --git a/resources/lib/watchdog/observers/kqueue.py b/resources/lib/watchdog/observers/kqueue.py index bd1f55e1..c35f3e8e 100644 --- a/resources/lib/watchdog/observers/kqueue.py +++ b/resources/lib/watchdog/observers/kqueue.py @@ -67,7 +67,7 @@ Collections and Utility Classes """ -from watchdog.utils import platform +from ..utils import platform import threading import errno @@ -78,16 +78,16 @@ import select from pathlib import Path -from watchdog.observers.api import ( +from .api import ( BaseObserver, EventEmitter, DEFAULT_OBSERVER_TIMEOUT, DEFAULT_EMITTER_TIMEOUT ) -from watchdog.utils.dirsnapshot import DirectorySnapshot +from ..utils.dirsnapshot import DirectorySnapshot -from watchdog.events import ( +from ..events import ( DirMovedEvent, DirDeletedEvent, DirCreatedEvent, diff --git a/resources/lib/watchdog/observers/polling.py b/resources/lib/watchdog/observers/polling.py index 73b72736..e03be06f 100644 --- a/resources/lib/watchdog/observers/polling.py +++ b/resources/lib/watchdog/observers/polling.py @@ -38,15 +38,15 @@ import os import threading from functools import partial -from watchdog.utils.dirsnapshot import DirectorySnapshot, DirectorySnapshotDiff -from watchdog.observers.api import ( +from ..utils.dirsnapshot import DirectorySnapshot, DirectorySnapshotDiff +from .api import ( EventEmitter, BaseObserver, DEFAULT_OBSERVER_TIMEOUT, DEFAULT_EMITTER_TIMEOUT ) -from watchdog.events import ( +from ..events import ( DirMovedEvent, DirDeletedEvent, DirCreatedEvent, diff --git a/resources/lib/watchdog/observers/read_directory_changes.py b/resources/lib/watchdog/observers/read_directory_changes.py index a0e088eb..85b69750 100644 --- a/resources/lib/watchdog/observers/read_directory_changes.py +++ b/resources/lib/watchdog/observers/read_directory_changes.py @@ -20,7 +20,7 @@ import threading import os.path import time -from watchdog.events import ( +from ..events import ( DirCreatedEvent, DirDeletedEvent, DirMovedEvent, @@ -33,14 +33,14 @@ from watchdog.events import ( generate_sub_created_events, ) -from watchdog.observers.api import ( +from api import ( EventEmitter, BaseObserver, DEFAULT_OBSERVER_TIMEOUT, DEFAULT_EMITTER_TIMEOUT ) -from watchdog.observers.winapi import ( +from winapi import ( read_events, get_directory_handle, close_directory_handle, diff --git a/resources/lib/watchdog/tricks/__init__.py b/resources/lib/watchdog/tricks/__init__.py index 5c3f2ab8..2bcbf12f 100644 --- a/resources/lib/watchdog/tricks/__init__.py +++ b/resources/lib/watchdog/tricks/__init__.py @@ -46,8 +46,8 @@ import signal import subprocess import time -from watchdog.utils import echo -from watchdog.events import PatternMatchingEventHandler +from ..utils import echo +from ..events import PatternMatchingEventHandler class Trick(PatternMatchingEventHandler): diff --git a/resources/lib/watchdog/utils/bricks.py b/resources/lib/watchdog/utils/bricks.py index 35426b7c..5bbe300f 100644 --- a/resources/lib/watchdog/utils/bricks.py +++ b/resources/lib/watchdog/utils/bricks.py @@ -35,7 +35,6 @@ Classes .. autoclass:: OrderedSet """ - import queue @@ -100,3 +99,69 @@ class SkipRepeatsQueue(queue.Queue): if item is self._last_item: self._last_item = None return item + + +class OrderedSetQueue(queue.Queue): + + """Thread-safe implementation of an ordered set queue. + + Disallows adding a duplicate item while maintaining the + order of items in the queue. The implementation leverages + locking already implemented in the base class + redefining only the primitives. Since the internal queue + is not replaced, the order is maintained. The set is used + merely to check for the existence of an item. + + Queued items must be immutable and hashable so that they can be used + as dictionary keys. You must implement **only read-only properties** and + the :meth:`Item.__hash__()`, :meth:`Item.__eq__()`, and + :meth:`Item.__ne__()` methods for items to be hashable. + + An example implementation follows:: + + class Item(object): + def __init__(self, a, b): + self._a = a + self._b = b + + @property + def a(self): + return self._a + + @property + def b(self): + return self._b + + def _key(self): + return (self._a, self._b) + + def __eq__(self, item): + return self._key() == item._key() + + def __ne__(self, item): + return self._key() != item._key() + + def __hash__(self): + return hash(self._key()) + + :author: lalinsky@gmail.com (Lukáš Lalinský) + :url: http://stackoverflow.com/questions/1581895/how-check-if-a-task-is-already-in-python-queue + """ + + def _init(self, maxsize): + queue.Queue._init(self, maxsize) + self._set_of_items = set() + + def _put(self, item): + if item not in self._set_of_items: + queue.Queue._put(self, item) + self._set_of_items.add(item) + else: + # `put` increments `unfinished_tasks` even if we did not put + # anything into the queue here + self.unfinished_tasks -= 1 + + def _get(self): + item = queue.Queue._get(self) + self._set_of_items.remove(item) + return item diff --git a/resources/lib/watchdog/watchmedo.py b/resources/lib/watchdog/watchmedo.py index d8a1bca1..69f44580 100644 --- a/resources/lib/watchdog/watchmedo.py +++ b/resources/lib/watchdog/watchmedo.py @@ -32,8 +32,8 @@ import logging from io import StringIO from argh import arg, aliases, ArghParser, expects_obj -from watchdog.version import VERSION_STRING -from watchdog.utils import WatchdogShutdown, load_class +from .version import VERSION_STRING +from .utils import WatchdogShutdown, load_class logging.basicConfig(level=logging.INFO) @@ -162,7 +162,7 @@ def tricks_from(args): :param args: Command line argument options. """ - from watchdog.observers import Observer + from .observers import Observer add_to_sys_path(path_split(args.python_path)) observers = [] @@ -312,8 +312,8 @@ def log(args): :param args: Command line argument options. """ - from watchdog.utils import echo - from watchdog.tricks import LoggerTrick + from .utils import echo + from .tricks import LoggerTrick if args.trace: echo.echo_class(LoggerTrick) @@ -324,23 +324,23 @@ def log(args): ignore_patterns=ignore_patterns, ignore_directories=args.ignore_directories) if args.debug_force_polling: - from watchdog.observers.polling import PollingObserver as Observer + from .observers.polling import PollingObserver as Observer elif args.debug_force_kqueue: - from watchdog.observers.kqueue import KqueueObserver as Observer + from .observers.kqueue import KqueueObserver as Observer elif args.debug_force_winapi_async: - from watchdog.observers.read_directory_changes_async import\ + from .observers.read_directory_changes_async import\ WindowsApiAsyncObserver as Observer elif args.debug_force_winapi: - from watchdog.observers.read_directory_changes import\ + from .observers.read_directory_changes import\ WindowsApiObserver as Observer elif args.debug_force_inotify: - from watchdog.observers.inotify import InotifyObserver as Observer + from .observers.inotify import InotifyObserver as Observer elif args.debug_force_fsevents: - from watchdog.observers.fsevents import FSEventsObserver as Observer + from .observers.fsevents import FSEventsObserver as Observer else: # Automatically picks the most appropriate observer for the platform # on which it is running. - from watchdog.observers import Observer + from .observers import Observer observer = Observer(timeout=args.timeout) observe_with(observer, handler, args.directories, args.recursive) @@ -420,15 +420,15 @@ def shell_command(args): :param args: Command line argument options. """ - from watchdog.tricks import ShellCommandTrick + from .tricks import ShellCommandTrick if not args.command: args.command = None if args.debug_force_polling: - from watchdog.observers.polling import PollingObserver as Observer + from .observers.polling import PollingObserver as Observer else: - from watchdog.observers import Observer + from .observers import Observer patterns, ignore_patterns = parse_patterns(args.patterns, args.ignore_patterns) @@ -510,11 +510,11 @@ def auto_restart(args): """ if args.debug_force_polling: - from watchdog.observers.polling import PollingObserver as Observer + from .observers.polling import PollingObserver as Observer else: - from watchdog.observers import Observer + from .observers import Observer - from watchdog.tricks import AutoRestartTrick + from .tricks import AutoRestartTrick import signal if not args.directories: