Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Simplify diff
  • Loading branch information
barneygale committed Jun 4, 2024
commit 5ed1c09d2023e67d4fe684f6cfcb9879825eea70
23 changes: 15 additions & 8 deletions Lib/glob.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,27 +340,32 @@ def __init__(self, sep, case_sensitive, case_pedantic=False, recursive=False):

# Abstract methods

def lexists(self, path):
@staticmethod
def lexists(path):
"""Implements os.path.lexists().
"""
raise NotImplementedError

def scandir(self, path):
@staticmethod
def scandir(path):
"""Implements os.scandir().
"""
raise NotImplementedError

def add_slash(self, path):
@staticmethod
def add_slash(path):
"""Returns a path with a trailing slash added.
"""
raise NotImplementedError

def concat_path(self, path, text):
@staticmethod
def concat_path(path, text):
"""Implements path concatenation.
"""
raise NotImplementedError

def parse_entry(self, entry):
@staticmethod
def parse_entry(entry):
"""Returns the path of an entry yielded from scandir().
"""
raise NotImplementedError
Expand Down Expand Up @@ -523,7 +528,7 @@ def select_exists(self, path, exists=False):
yield path


class _Globber(_GlobberBase):
class _StringGlobber(_GlobberBase):
"""Provides shell-style pattern matching and globbing for string paths.
"""
lexists = staticmethod(os.path.lexists)
Expand All @@ -532,13 +537,15 @@ class _Globber(_GlobberBase):
concat_path = operator.add

if os.name == 'nt':
def add_slash(self, pathname):
@staticmethod
def add_slash(pathname):
tail = os.path.splitroot(pathname)[2]
if not tail or tail[-1] in '\\/':
return pathname
return f'{pathname}\\'
else:
def add_slash(self, pathname):
@staticmethod
def add_slash(pathname):
if not pathname or pathname[-1] == '/':
return pathname
return f'{pathname}/'
19 changes: 10 additions & 9 deletions Lib/pathlib/_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,29 +85,30 @@ def isabs(self, path):
raise UnsupportedOperation(self._unsupported_msg('isabs()'))


class Globber(_GlobberBase):
class PathGlobber(_GlobberBase):
"""
Class providing shell-style globbing for path objects.
"""

lexists = operator.methodcaller('exists', follow_symlinks=False)
add_slash = operator.methodcaller('joinpath', '')

def scandir(self, path):
@staticmethod
def scandir(path):
"""Emulates os.scandir(), which returns an object that can be used as
a context manager. This method is called by walk() and glob().
"""
import contextlib
return contextlib.nullcontext(path.iterdir())

def concat_path(self, path, text):
"""Appends text to the given path.
"""
@staticmethod
def concat_path(path, text):
"""Appends text to the given path."""
return path.with_segments(path._raw_path + text)

def parse_entry(self, entry):
"""Returns the path of an entry yielded from scandir().
"""
@staticmethod
def parse_entry(entry):
"""Returns the path of an entry yielded from scandir()."""
return entry


Expand All @@ -131,7 +132,7 @@ class PurePathBase:
'_resolving',
)
parser = ParserBase()
_globber = Globber
_globber = PathGlobber

def __init__(self, path, *paths):
self._raw_path = self.parser.join(path, *paths) if paths else path
Expand Down
4 changes: 2 additions & 2 deletions Lib/pathlib/_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import os
import posixpath
import sys
from glob import _Globber
from glob import _StringGlobber
from itertools import chain
from _collections_abc import Sequence

Expand Down Expand Up @@ -101,7 +101,7 @@ class PurePath(PurePathBase):
'_hash',
)
parser = os.path
_globber = _Globber
_globber = _StringGlobber

def __new__(cls, *args, **kwargs):
"""Construct a PurePath from one or several strings and or existing
Expand Down