Skip to content

Commit

Permalink
Use the standard pathlib instead of pathtools
Browse files Browse the repository at this point in the history
  • Loading branch information
bstaletic committed Nov 26, 2020
1 parent 432c31f commit 35feb63
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 13 deletions.
2 changes: 0 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,6 @@ appropriate observer like in the example above, do::
Dependencies
------------
1. Python 3.4 or above.
2. pathtools_
3. XCode_ (only on Mac OS X)
4. PyYAML_ (only for ``watchmedo`` script)
5. argh_ (only for ``watchmedo`` script)
Expand Down Expand Up @@ -277,7 +276,6 @@ to do:
.. _PyYAML: http://www.pyyaml.org/
.. _XCode: http://developer.apple.com/technologies/tools/xcode.html
.. _LibYAML: http://pyyaml.org/wiki/LibYAML
.. _pathtools: http://github.com/gorakhargosh/pathtools

.. _pnotify: http://mark.heily.com/pnotify
.. _unison fsmonitor: https://webdav.seas.upenn.edu/viewvc/unison/trunk/src/fsmonitor.py?view=markup&pathrev=471
Expand Down
2 changes: 1 addition & 1 deletion docs/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pathtools>=0.1.2

This comment has been minimized.

Copy link
@BoboTiG

BoboTiG Nov 27, 2020

You could completely remove the file then :)

1 change: 0 additions & 1 deletion docs/source/global.rst.inc
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,3 @@
.. _unison fsmonitor: https://webdav.seas.upenn.edu/viewvc/unison/trunk/src/fsmonitor.py?view=markup&pathrev=471
.. _XCode: http://developer.apple.com/technologies/tools/xcode.html
.. _zc.buildout: http://www.buildout.org/
.. _pathtools: http://github.com/gorakhargosh/pathtools
2 changes: 0 additions & 2 deletions docs/source/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ using.
+=====================+=============+=============+=============+=============+
| XCode_ | | | Yes | |
+---------------------+-------------+-------------+-------------+-------------+
| pathtools_ | Yes | Yes | Yes | Yes |
+---------------------+-------------+-------------+-------------+-------------+

The following is a list of dependencies you need based on the operating system you are
using the ``watchmedo`` utility.
Expand Down
4 changes: 1 addition & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,7 @@
),
]

install_requires = [
"pathtools>=0.1.1",
]
install_requires = []

This comment has been minimized.

Copy link
@BoboTiG

BoboTiG Nov 27, 2020

You could completely remove it then :)

extras_require = {
'watchmedo': ['PyYAML>=3.10', 'argh>=0.24.1'],
}
Expand Down
3 changes: 1 addition & 2 deletions src/watchdog/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@
import os.path
import logging
import re
from pathtools.patterns import match_any_paths
from watchdog.utils import has_attribute
from watchdog.utils import has_attribute, match_any_paths


EVENT_TYPE_MOVED = 'moved'
Expand Down
4 changes: 3 additions & 1 deletion src/watchdog/observers/kqueue.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@
import os.path
import select

from pathtools.path import absolute_path
from pathlib import Path
def absolute_path(path):
return Path(path).resolve()

from watchdog.observers.api import (
BaseObserver,
Expand Down
34 changes: 34 additions & 0 deletions src/watchdog/utils/patterns.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# echo.py: Tracing function calls using Python decorators.
#
# Written by Boris Staletic <boris.staletic@gmail.com>

from pathlib import PureWindowsPath, PurePosixPath

This comment has been minimized.

Copy link
@BoboTiG

BoboTiG Nov 27, 2020

Why not using simply from pathlib import Path and Path everywhere?


def _match_path(path, included_patterns, excluded_patterns):
common_patterns = included_patterns & excluded_patterns
if common_patterns:
raise ValueError(f'conflicting patterns `{common_patterns}` included and excluded')
return (any(path.match(p) for p in included_patterns) and
not any(path.match(p) for p in excluded_patterns))


def filter_paths(paths, included_patterns = None, excluded_patterns = None, case_sensitive = True):
included = ["*"] if included_patterns is None else included_patterns
excluded = [] if excluded_patterns is None else excluded_patterns

for path in paths:
p = PurePosixPath(path) if case_sensitive else PureWindowsPath(path)
if _match_path(p, set(included), set(excluded), case_sensitive):
yield p


def match_any_paths(paths, included_patterns = None, excluded_patterns = None, case_sensitive = True):
included = ["*"] if included_patterns is None else included_patterns
excluded = [] if excluded_patterns is None else excluded_patterns

for path in paths:
p = PurePosixPath(path) if case_sensitive else PureWindowsPath(path)
if _match_path(p, set(included), set(excluded), case_sensitive):
yield p
2 changes: 1 addition & 1 deletion tests/test_pattern_matching_event_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from pathtools.patterns import filter_paths
from watchdog.events import (
FileDeletedEvent,
FileModifiedEvent,
Expand All @@ -33,6 +32,7 @@
EVENT_TYPE_MOVED,
)
from watchdog.utils import has_attribute
from watchdog.utils.patterns import filter_paths

path_1 = '/path/xyz'
path_2 = '/path/abc'
Expand Down

0 comments on commit 35feb63

Please sign in to comment.