File: util.py

package info (click to toggle)
python-pathspec 1.0.4-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,140 kB
  • sloc: python: 9,685; makefile: 14
file content (847 lines) | stat: -rw-r--r-- 24,728 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
"""
This module provides utility methods for dealing with path-specs.
"""

import os
import os.path
import pathlib
import posixpath
import stat
from collections.abc import (
	Collection,
	Iterable,
	Iterator,
	Sequence)
from dataclasses import (
	dataclass)
from typing import (
	Any,
	Callable,  # Replaced by `collections.abc.Callable` in 3.9.2.
	Generic,
	Optional,  # Replaced by `X | None` in 3.10.
	TypeVar,
	Union)  # Replaced by `X | Y` in 3.10.

from .pattern import (
	Pattern)
from ._typing import (
	AnyStr,  # Removed in 3.18.
	deprecated)  # Added in 3.13.

StrPath = Union[str, os.PathLike[str]]

TStrPath = TypeVar("TStrPath", bound=StrPath)
"""
Type variable for :class:`str` or :class:`os.PathLike`.
"""

NORMALIZE_PATH_SEPS = [
	__sep
	for __sep in [os.sep, os.altsep]
	if __sep and __sep != posixpath.sep
]
"""
*NORMALIZE_PATH_SEPS* (:class:`list` of :class:`str`) contains the path
separators that need to be normalized to the POSIX separator for the current
operating system. The separators are determined by examining :data:`os.sep` and
:data:`os.altsep`.
"""

_registered_patterns = {}
"""
*_registered_patterns* (:class:`dict`) maps a name (:class:`str`) to the
registered pattern factory (:class:`~collections.abc.Callable`).
"""


def append_dir_sep(path: pathlib.Path) -> str:
	"""
	Appends the path separator to the path if the path is a directory. This can be
	used to aid in distinguishing between directories and files on the file-system
	by relying on the presence of a trailing path separator.

	*path* (:class:`pathlib.Path`) is the path to use.

	Returns the path (:class:`str`).
	"""
	str_path = str(path)
	if path.is_dir():
		str_path += os.sep

	return str_path


def check_match_file(
	patterns: Iterable[tuple[int, Pattern]],
	file: str,
	is_reversed: Optional[bool] = None,
) -> tuple[Optional[bool], Optional[int]]:
	"""
	Check the file against the patterns.

	*patterns* (:class:`~collections.abc.Iterable`) yields each indexed pattern
	(:class:`tuple`) which contains the pattern index (:class:`int`) and actua
	pattern (:class:`.Pattern`).

	*file* (:class:`str`) is the normalized file path to be matched against
	*patterns*.

	*is_reversed* (:class:`bool` or :data:`None`) is whether the order of the
	patterns has been reversed. Default is :data:`None` for :data:`False`.
	Reversing the order of the patterns is an optimization.

	Returns a :class:`tuple` containing whether to include *file* (:class:`bool`
	or :data:`None`), and the index of the last matched pattern (:class:`int` or
	:data:`None`).
	"""
	if is_reversed:
		# Check patterns in reverse order. The first pattern that matches takes
		# precedence.
		for index, pattern in patterns:
			if pattern.include is not None and pattern.match_file(file) is not None:
				return pattern.include, index

		return None, None

	else:
		# Check all patterns. The last pattern that matches takes precedence.
		out_include: Optional[bool] = None
		out_index: Optional[int] = None
		for index, pattern in patterns:
			if pattern.include is not None and pattern.match_file(file) is not None:
				out_include = pattern.include
				out_index = index

		return out_include, out_index


def detailed_match_files(
	patterns: Iterable[Pattern],
	files: Iterable[str],
	all_matches: Optional[bool] = None,
) -> dict[str, 'MatchDetail']:
	"""
	Matches the files to the patterns, and returns which patterns matched the
	files.

	*patterns* (:class:`~collections.abc.Iterable` of :class:`.Pattern`) contains
	the patterns to use.

	*files* (:class:`~collections.abc.Iterable` of :class:`str`) contains the
	normalized file paths to be matched against *patterns*.

	*all_matches* (:class:`bool` or :data:`None`) is whether to return all matches
	patterns (:data:`True`), or only the last matched pattern (:data:`False`).
	Default is :data:`None` for :data:`False`.

	Returns the matched files (:class:`dict`) which maps each matched file
	(:class:`str`) to the patterns that matched in order (:class:`.MatchDetail`).
	"""
	all_files = files if isinstance(files, Collection) else list(files)
	return_files = {}
	for pattern in patterns:
		if pattern.include is not None:
			result_files = pattern.match(all_files)  # TODO: Replace with `.match_file()`.
			if pattern.include:
				# Add files and record pattern.
				for result_file in result_files:
					if result_file in return_files:
						if all_matches:
							return_files[result_file].patterns.append(pattern)
						else:
							return_files[result_file].patterns[0] = pattern
					else:
						return_files[result_file] = MatchDetail([pattern])

			else:
				# Remove files.
				for file in result_files:
					del return_files[file]

	return return_files


def _filter_check_patterns(
	patterns: Iterable[Pattern],
) -> list[tuple[int, Pattern]]:
	"""
	Filters out null-patterns.

	*patterns* (:class:`~collections.abc.Iterable` of :class:`.Pattern`) contains
	the patterns.

	Returns a :class:`list` containing each indexed pattern (:class:`tuple`) which
	contains the pattern index (:class:`int`) and the actual pattern
	(:class:`.Pattern`).
	"""
	return [
		(__index, __pat)
		for __index, __pat in enumerate(patterns)
		if __pat.include is not None
	]


def _is_iterable(value: Any) -> bool:
	"""
	Check whether the value is an iterable (excludes strings).

	*value* is the value to check,

	Returns whether *value* is an iterable (:class:`bool`).
	"""
	return isinstance(value, Iterable) and not isinstance(value, (str, bytes))


@deprecated((
	"pathspec.util.iter_tree() is deprecated. Use iter_tree_files() instead."
))
def iter_tree(root, on_error=None, follow_links=None):
	"""
	.. version-deprecated:: 0.10.0
		This is an alias for the :func:`.iter_tree_files` function.
	"""
	return iter_tree_files(root, on_error=on_error, follow_links=follow_links)


def iter_tree_entries(
	root: StrPath,
	on_error: Optional[Callable[[OSError], None]] = None,
	follow_links: Optional[bool] = None,
) -> Iterator['TreeEntry']:
	"""
	Walks the specified directory for all files and directories.

	*root* (:class:`str` or :class:`os.PathLike`) is the root directory to search.

	*on_error* (:class:`~collections.abc.Callable` or :data:`None`) optionally is
	the error handler for file-system exceptions. It will be called with the
	exception (:exc:`OSError`). Reraise the exception to abort the walk. Default
	is :data:`None` to ignore file-system exceptions.

	*follow_links* (:class:`bool` or :data:`None`) optionally is whether to walk
	symbolic links that resolve to directories. Default is :data:`None` for
	:data:`True`.

	Raises :exc:`.RecursionError` if recursion is detected.

	Returns an :class:`~collections.abc.Iterator` yielding each file or directory
	entry (:class:`.TreeEntry`) relative to *root*.
	"""
	if on_error is not None and not callable(on_error):
		raise TypeError(f"on_error:{on_error!r} is not callable.")

	if follow_links is None:
		follow_links = True

	yield from _iter_tree_entries_next(os.path.abspath(root), '', {}, on_error, follow_links)


def _iter_tree_entries_next(
	root_full: str,
	dir_rel: str,
	memo: dict[str, str],
	on_error: Callable[[OSError], None],
	follow_links: bool,
) -> Iterator['TreeEntry']:
	"""
	Scan the directory for all descendant files.

	*root_full* (:class:`str`) the absolute path to the root directory.

	*dir_rel* (:class:`str`) the path to the directory to scan relative to
	*root_full*.

	*memo* (:class:`dict`) keeps track of ancestor directories encountered. Maps
	each ancestor real path (:class:`str`) to relative path (:class:`str`).

	*on_error* (:class:`~collections.abc.Callable` or :data:`None`) optionally is
	the error handler for file-system exceptions.

	*follow_links* (:class:`bool`) is whether to walk symbolic links that resolve
	to directories.

	Yields each entry (:class:`.TreeEntry`).
	"""
	dir_full = os.path.join(root_full, dir_rel)
	dir_real = os.path.realpath(dir_full)

	# Remember each encountered ancestor directory and its canonical (real) path.
	# If a canonical path is encountered more than once, recursion has occurred.
	if dir_real not in memo:
		memo[dir_real] = dir_rel
	else:
		raise RecursionError(real_path=dir_real, first_path=memo[dir_real], second_path=dir_rel)

	with os.scandir(dir_full) as scan_iter:
		node_ent: os.DirEntry
		for node_ent in scan_iter:
			node_rel = os.path.join(dir_rel, node_ent.name)

			# Inspect child node.
			try:
				node_lstat = node_ent.stat(follow_symlinks=False)
			except OSError as e:
				if on_error is not None:
					on_error(e)
				continue

			if node_ent.is_symlink():
				# Child node is a link, inspect the target node.
				try:
					node_stat = node_ent.stat()
				except OSError as e:
					if on_error is not None:
						on_error(e)
					continue
			else:
				node_stat = node_lstat

			if node_ent.is_dir(follow_symlinks=follow_links):
				# Child node is a directory, recurse into it and yield its descendant
				# files.
				yield TreeEntry(node_ent.name, node_rel, node_lstat, node_stat)

				yield from _iter_tree_entries_next(root_full, node_rel, memo, on_error, follow_links)

			elif node_ent.is_file() or node_ent.is_symlink():
				# Child node is either a file or an unfollowed link, yield it.
				yield TreeEntry(node_ent.name, node_rel, node_lstat, node_stat)

	# NOTE: Make sure to remove the canonical (real) path of the directory from
	# the ancestors memo once we are done with it. This allows the same directory
	# to appear multiple times. If this is not done, the second occurrence of the
	# directory will be incorrectly interpreted as a recursion. See
	# <https://github.com/cpburnz/python-path-specification/pull/7>.
	del memo[dir_real]


def iter_tree_files(
	root: StrPath,
	on_error: Optional[Callable[[OSError], None]] = None,
	follow_links: Optional[bool] = None,
) -> Iterator[str]:
	"""
	Walks the specified directory for all files.

	*root* (:class:`str` or :class:`os.PathLike`) is the root directory to search
	for files.

	*on_error* (:class:`~collections.abc.Callable` or :data:`None`) optionally is
	the error handler for file-system exceptions. It will be called with the
	exception (:exc:`OSError`). Reraise the exception to abort the walk. Default
	is :data:`None` to ignore file-system exceptions.

	*follow_links* (:class:`bool` or :data:`None`) optionally is whether to walk
	symbolic links that resolve to directories. Default is :data:`None` for
	:data:`True`.

	Raises :exc:`.RecursionError` if recursion is detected.

	Returns an :class:`~collections.abc.Iterator` yielding the path to each file
	(:class:`str`) relative to *root*.
	"""
	if on_error is not None and not callable(on_error):
		raise TypeError(f"on_error:{on_error!r} is not callable.")

	if follow_links is None:
		follow_links = True

	yield from _iter_tree_files_next(os.path.abspath(root), '', {}, on_error, follow_links)


def _iter_tree_files_next(
	root_full: str,
	dir_rel: str,
	memo: dict[str, str],
	on_error: Callable[[OSError], None],
	follow_links: bool,
) -> Iterator[str]:
	"""
	Scan the directory for all descendant files.

	*root_full* (:class:`str`) the absolute path to the root directory.

	*dir_rel* (:class:`str`) the path to the directory to scan relative to
	*root_full*.

	*memo* (:class:`dict`) keeps track of ancestor directories encountered. Maps
	each ancestor real path (:class:`str`) to relative path (:class:`str`).

	*on_error* (:class:`~collections.abc.Callable` or :data:`None`) optionally is
	the error handler for file-system exceptions.

	*follow_links* (:class:`bool`) is whether to walk symbolic links that resolve
	to directories.

	Yields each file path (:class:`str`).
	"""
	dir_full = os.path.join(root_full, dir_rel)
	dir_real = os.path.realpath(dir_full)

	# Remember each encountered ancestor directory and its canonical (real) path.
	# If a canonical path is encountered more than once, recursion has occurred.
	if dir_real not in memo:
		memo[dir_real] = dir_rel
	else:
		raise RecursionError(real_path=dir_real, first_path=memo[dir_real], second_path=dir_rel)

	with os.scandir(dir_full) as scan_iter:
		node_ent: os.DirEntry
		for node_ent in scan_iter:
			node_rel = os.path.join(dir_rel, node_ent.name)

			if node_ent.is_dir(follow_symlinks=follow_links):
				# Child node is a directory, recurse into it and yield its descendant
				# files.
				yield from _iter_tree_files_next(root_full, node_rel, memo, on_error, follow_links)

			elif node_ent.is_file():
				# Child node is a file, yield it.
				yield node_rel

			elif not follow_links and node_ent.is_symlink():
				# Child node is an unfollowed link, yield it.
				yield node_rel

	# NOTE: Make sure to remove the canonical (real) path of the directory from
	# the ancestors memo once we are done with it. This allows the same directory
	# to appear multiple times. If this is not done, the second occurrence of the
	# directory will be incorrectly interpreted as a recursion. See
	# <https://github.com/cpburnz/python-path-specification/pull/7>.
	del memo[dir_real]


def lookup_pattern(name: str) -> Callable[[AnyStr], Pattern]:
	"""
	Lookups a registered pattern factory by name.

	*name* (:class:`str`) is the name of the pattern factory.

	Returns the registered pattern factory (:class:`~collections.abc.Callable`).
	If no pattern factory is registered, raises :exc:`KeyError`.
	"""
	return _registered_patterns[name]


def match_file(patterns: Iterable[Pattern], file: str) -> bool:
	"""
	Matches the file to the patterns.

	*patterns* (:class:`~collections.abc.Iterable` of :class:`.Pattern`) contains
	the patterns to use.

	*file* (:class:`str`) is the normalized file path to be matched against
	*patterns*.

	Returns :data:`True` if *file* matched; otherwise, :data:`False`.
	"""
	matched = False
	for pattern in patterns:
		if pattern.include is not None and pattern.match_file(file) is not None:
			matched = pattern.include

	return matched


@deprecated((
	"pathspec.util.match_files() is deprecated. Use match_file() with a loop for "
	"better results."
))
def match_files(
	patterns: Iterable[Pattern],
	files: Iterable[str],
) -> set[str]:
	"""
	.. version-deprecated:: 0.10.0
		This function is no longer used. Use the :func:`.match_file` function with a
		loop for better results.

	Matches the files to the patterns.

	*patterns* (:class:`~collections.abc.Iterable` of :class:`.Pattern`) contains
	the patterns to use.

	*files* (:class:`~collections.abc.Iterable` of :class:`str`) contains the
	normalized file paths to be matched against *patterns*.

	Returns the matched files (:class:`set` of :class:`str`).
	"""
	use_patterns = [__pat for __pat in patterns if __pat.include is not None]

	return_files = set()
	for file in files:
		if match_file(use_patterns, file):
			return_files.add(file)

	return return_files


def normalize_file(
	file: StrPath,
	separators: Optional[Collection[str]] = None,
) -> str:
	"""
	Normalizes the file path to use the POSIX path separator (i.e., ``"/"``), and
	make the paths relative (remove leading ``"/"``).

	*file* (:class:`str` or :class:`os.PathLike`) is the file path.

	*separators* (:class:`~collections.abc.Collection` of :class:`str`; or
	:data:`None`) optionally contains the path separators to normalize. This does
	not need to include the POSIX path separator (``"/"``), but including it will
	not affect the results. Default is ``None`` for :data:`.NORMALIZE_PATH_SEPS`.
	To prevent normalization, pass an empty container (e.g., an empty tuple
	``()``).

	Returns the normalized file path (:class:`str`).
	"""
	# Normalize path separators.
	if separators is None:
		separators = NORMALIZE_PATH_SEPS

	# Convert path object to string.
	norm_file: str = os.fspath(file)

	for sep in separators:
		norm_file = norm_file.replace(sep, posixpath.sep)

	if norm_file.startswith('/'):
		# Make path relative.
		norm_file = norm_file[1:]

	elif norm_file.startswith('./'):
		# Remove current directory prefix.
		norm_file = norm_file[2:]

	return norm_file


@deprecated((
	"pathspec.util.normalize_files() is deprecated. Use normalize_file() with a "
	"loop for better results."
))
def normalize_files(
	files: Iterable[StrPath],
	separators: Optional[Collection[str]] = None,
) -> dict[str, list[StrPath]]:
	"""
	.. version-deprecated:: 0.10.0
		This function is no longer used. Use the :func:`.normalize_file` function
		with a loop for better results.

	Normalizes the file paths to use the POSIX path separator.

	*files* (:class:`~collections.abc.Iterable` of :class:`str` or
	:class:`os.PathLike`) contains the file paths to be normalized.

	*separators* (:class:`~collections.abc.Collection` of :class:`str`; or
	:data:`None`) optionally contains the path separators to normalize. See
	:func:`.normalize_file` for more information.

	Returns a :class:`dict` mapping each normalized file path (:class:`str`) to
	the original file paths (:class:`list` of :class:`str` or
	:class:`os.PathLike`).
	"""
	norm_files = {}
	for path in files:
		norm_file = normalize_file(path, separators=separators)
		if norm_file in norm_files:
			norm_files[norm_file].append(path)
		else:
			norm_files[norm_file] = [path]

	return norm_files


def register_pattern(
	name: str,
	pattern_factory: Callable[[AnyStr], Pattern],
	override: Optional[bool] = None,
) -> None:
	"""
	Registers the specified pattern factory.

	*name* (:class:`str`) is the name to register the pattern factory under.

	*pattern_factory* (:class:`~collections.abc.Callable`) is used to compile
	patterns. It must accept an uncompiled pattern (:class:`str`) and return the
	compiled pattern (:class:`.Pattern`).

	*override* (:class:`bool` or :data:`None`) optionally is whether to allow
	overriding an already registered pattern under the same name (:data:`True`),
	instead of raising an :exc:`.AlreadyRegisteredError` (:data:`False`). Default
	is :data:`None` for :data:`False`.
	"""
	if not isinstance(name, str):
		raise TypeError(f"name:{name!r} is not a string.")

	if not callable(pattern_factory):
		raise TypeError(f"pattern_factory:{pattern_factory!r} is not callable.")

	if name in _registered_patterns and not override:
		raise AlreadyRegisteredError(name, _registered_patterns[name])

	_registered_patterns[name] = pattern_factory


class AlreadyRegisteredError(Exception):
	"""
	The :exc:`AlreadyRegisteredError` exception is raised when a pattern factory
	is registered under a name already in use.
	"""

	def __init__(
		self,
		name: str,
		pattern_factory: Callable[[AnyStr], Pattern],
	) -> None:
		"""
		Initializes the :exc:`AlreadyRegisteredError` instance.

		*name* (:class:`str`) is the name of the registered pattern.

		*pattern_factory* (:class:`~collections.abc.Callable`) is the registered
		pattern factory.
		"""
		super().__init__(name, pattern_factory)

	@property
	def message(self) -> str:
		"""
		*message* (:class:`str`) is the error message.
		"""
		return (
			f"{self.name!r} is already registered for pattern factory="
			f"{self.pattern_factory!r}."
		)

	@property
	def name(self) -> str:
		"""
		*name* (:class:`str`) is the name of the registered pattern.
		"""
		return self.args[0]

	@property
	def pattern_factory(self) -> Callable[[AnyStr], Pattern]:
		"""
		*pattern_factory* (:class:`~collections.abc.Callable`) is the registered
		pattern factory.
		"""
		return self.args[1]


class RecursionError(Exception):
	"""
	The :exc:`RecursionError` exception is raised when recursion is detected.
	"""

	def __init__(
		self,
		real_path: str,
		first_path: str,
		second_path: str,
	) -> None:
		"""
		Initializes the :exc:`RecursionError` instance.

		*real_path* (:class:`str`) is the real path that recursion was encountered
		on.

		*first_path* (:class:`str`) is the first path encountered for *real_path*.

		*second_path* (:class:`str`) is the second path encountered for *real_path*.
		"""
		super().__init__(real_path, first_path, second_path)

	@property
	def first_path(self) -> str:
		"""
		*first_path* (:class:`str`) is the first path encountered for
		:attr:`self.real_path <RecursionError.real_path>`.
		"""
		return self.args[1]

	@property
	def message(self) -> str:
		"""
		*message* (:class:`str`) is the error message.
		"""
		return (
			f"Real path {self.real_path!r} was encountered at {self.first_path!r} "
			f"and then {self.second_path!r}."
		)

	@property
	def real_path(self) -> str:
		"""
		*real_path* (:class:`str`) is the real path that recursion was
		encountered on.
		"""
		return self.args[0]

	@property
	def second_path(self) -> str:
		"""
		*second_path* (:class:`str`) is the second path encountered for
		:attr:`self.real_path <RecursionError.real_path>`.
		"""
		return self.args[2]


@dataclass(frozen=True)
class CheckResult(Generic[TStrPath]):
	"""
	The :class:`CheckResult` class contains information about the file and which
	pattern matched it.
	"""

	# Make the class dict-less.
	__slots__ = (
		'file',
		'include',
		'index',
	)

	file: TStrPath
	"""
	*file* (:class:`str` or :class:`os.PathLike`) is the file path.
	"""

	include: Optional[bool]
	"""
	*include* (:class:`bool` or :data:`None`) is whether to include or exclude the
	file. If :data:`None`, no pattern matched.
	"""

	index: Optional[int]
	"""
	*index* (:class:`int` or :data:`None`) is the index of the last pattern that
	matched. If :data:`None`, no pattern matched.
	"""


class MatchDetail(object):
	"""
	The :class:`.MatchDetail` class contains information about
	"""

	# Make the class dict-less.
	__slots__ = ('patterns',)

	def __init__(self, patterns: Sequence[Pattern]) -> None:
		"""
		Initialize the :class:`.MatchDetail` instance.

		*patterns* (:class:`~collections.abc.Sequence` of :class:`.Pattern`)
		contains the patterns that matched the file in the order they were encountered.
		"""

		self.patterns = patterns
		"""
		*patterns* (:class:`~collections.abc.Sequence` of :class:`.Pattern`)
		contains the patterns that matched the file in the order they were
		encountered.
		"""


class TreeEntry(object):
	"""
	The :class:`TreeEntry` class contains information about a file-system entry.
	"""

	# Make the class dict-less.
	__slots__ = ('_lstat', 'name', 'path', '_stat')

	def __init__(
		self,
		name: str,
		path: str,
		lstat: os.stat_result,
		stat: os.stat_result,
	) -> None:
		"""
		Initialize the :class:`TreeEntry` instance.

		*name* (:class:`str`) is the base name of the entry.

		*path* (:class:`str`) is the relative path of the entry.

		*lstat* (:class:`os.stat_result`) is the stat result of the direct entry.

		*stat* (:class:`os.stat_result`) is the stat result of the entry,
		potentially linked.
		"""

		self._lstat: os.stat_result = lstat
		"""
		*_lstat* (:class:`os.stat_result`) is the stat result of the direct entry.
		"""

		self.name: str = name
		"""
		*name* (:class:`str`) is the base name of the entry.
		"""

		self.path: str = path
		"""
		*path* (:class:`str`) is the path of the entry.
		"""

		self._stat: os.stat_result = stat
		"""
		*_stat* (:class:`os.stat_result`) is the stat result of the linked entry.
		"""

	def is_dir(self, follow_links: Optional[bool] = None) -> bool:
		"""
		Get whether the entry is a directory.

		*follow_links* (:class:`bool` or :data:`None`) is whether to follow symbolic
		links. If this is :data:`True`, a symlink to a directory will result in
		:data:`True`. Default is :data:`None` for :data:`True`.

		Returns whether the entry is a directory (:class:`bool`).
		"""
		if follow_links is None:
			follow_links = True

		node_stat = self._stat if follow_links else self._lstat
		return stat.S_ISDIR(node_stat.st_mode)

	def is_file(self, follow_links: Optional[bool] = None) -> bool:
		"""
		Get whether the entry is a regular file.

		*follow_links* (:class:`bool` or :data:`None`) is whether to follow symbolic
		links. If this is :data:`True`, a symlink to a regular file will result in
		:data:`True`. Default is :data:`None` for :data:`True`.

		Returns whether the entry is a regular file (:class:`bool`).
		"""
		if follow_links is None:
			follow_links = True

		node_stat = self._stat if follow_links else self._lstat
		return stat.S_ISREG(node_stat.st_mode)

	def is_symlink(self) -> bool:
		"""
		Returns whether the entry is a symbolic link (:class:`bool`).
		"""
		return stat.S_ISLNK(self._lstat.st_mode)

	def stat(self, follow_links: Optional[bool] = None) -> os.stat_result:
		"""
		Get the cached stat result for the entry.

		*follow_links* (:class:`bool` or :data:`None`) is whether to follow symbolic
		links. If this is :data:`True`, the stat result of the linked file will be
		returned. Default is :data:`None` for :data:`True`.

		Returns that stat result (:class:`os.stat_result`).
		"""
		if follow_links is None:
			follow_links = True

		return self._stat if follow_links else self._lstat