Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

49 linhas
1.3 KiB

  1. import sys
  2. import types
  3. from pathlib import Path
  4. from typing import Any, _GenericAlias # type: ignore [attr-defined]
  5. from typing_extensions import get_origin
  6. _PATH_TYPE_LABELS = {
  7. Path.is_dir: 'directory',
  8. Path.is_file: 'file',
  9. Path.is_mount: 'mount point',
  10. Path.is_symlink: 'symlink',
  11. Path.is_block_device: 'block device',
  12. Path.is_char_device: 'char device',
  13. Path.is_fifo: 'FIFO',
  14. Path.is_socket: 'socket',
  15. }
  16. def path_type_label(p: Path) -> str:
  17. """
  18. Find out what sort of thing a path is.
  19. """
  20. assert p.exists(), 'path does not exist'
  21. for method, name in _PATH_TYPE_LABELS.items():
  22. if method(p):
  23. return name
  24. return 'unknown' # pragma: no cover
  25. # TODO remove and replace usage by `isinstance(cls, type) and issubclass(cls, class_or_tuple)`
  26. # once we drop support for Python 3.10.
  27. def _lenient_issubclass(cls: Any, class_or_tuple: Any) -> bool: # pragma: no cover
  28. try:
  29. return isinstance(cls, type) and issubclass(cls, class_or_tuple)
  30. except TypeError:
  31. if get_origin(cls) is not None:
  32. # Up until Python 3.10, isinstance(<generic_alias>, type) is True
  33. # (e.g. list[int])
  34. return False
  35. raise
  36. if sys.version_info < (3, 10):
  37. _WithArgsTypes = tuple()
  38. else:
  39. _WithArgsTypes = (_GenericAlias, types.GenericAlias, types.UnionType)