place.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. """Contains all logic related to placing an import within a certain section."""
  2. import importlib
  3. from fnmatch import fnmatch
  4. from functools import lru_cache
  5. from pathlib import Path
  6. from typing import FrozenSet, Iterable, Optional, Tuple
  7. from isort import sections
  8. from isort.settings import DEFAULT_CONFIG, Config
  9. from isort.utils import exists_case_sensitive
  10. LOCAL = "LOCALFOLDER"
  11. def module(name: str, config: Config = DEFAULT_CONFIG) -> str:
  12. """Returns the section placement for the given module name."""
  13. return module_with_reason(name, config)[0]
  14. @lru_cache(maxsize=1000)
  15. def module_with_reason(name: str, config: Config = DEFAULT_CONFIG) -> Tuple[str, str]:
  16. """Returns the section placement for the given module name alongside the reasoning."""
  17. return (
  18. _forced_separate(name, config)
  19. or _local(name, config)
  20. or _known_pattern(name, config)
  21. or _src_path(name, config)
  22. or (config.default_section, "Default option in Config or universal default.")
  23. )
  24. def _forced_separate(name: str, config: Config) -> Optional[Tuple[str, str]]:
  25. for forced_separate in config.forced_separate:
  26. # Ensure all forced_separate patterns will match to end of string
  27. path_glob = forced_separate
  28. if not forced_separate.endswith("*"):
  29. path_glob = "%s*" % forced_separate
  30. if fnmatch(name, path_glob) or fnmatch(name, "." + path_glob):
  31. return (forced_separate, f"Matched forced_separate ({forced_separate}) config value.")
  32. return None
  33. def _local(name: str, config: Config) -> Optional[Tuple[str, str]]:
  34. if name.startswith("."):
  35. return (LOCAL, "Module name started with a dot.")
  36. return None
  37. def _known_pattern(name: str, config: Config) -> Optional[Tuple[str, str]]:
  38. parts = name.split(".")
  39. module_names_to_check = (".".join(parts[:first_k]) for first_k in range(len(parts), 0, -1))
  40. for module_name_to_check in module_names_to_check:
  41. for pattern, placement in config.known_patterns:
  42. if placement in config.sections and pattern.match(module_name_to_check):
  43. return (placement, f"Matched configured known pattern {pattern}")
  44. return None
  45. def _src_path(
  46. name: str,
  47. config: Config,
  48. src_paths: Optional[Iterable[Path]] = None,
  49. prefix: Tuple[str, ...] = (),
  50. ) -> Optional[Tuple[str, str]]:
  51. if src_paths is None:
  52. src_paths = config.src_paths
  53. root_module_name, *nested_module = name.split(".", 1)
  54. new_prefix = prefix + (root_module_name,)
  55. namespace = ".".join(new_prefix)
  56. for src_path in src_paths:
  57. module_path = (src_path / root_module_name).resolve()
  58. if not prefix and not module_path.is_dir() and src_path.name == root_module_name:
  59. module_path = src_path.resolve()
  60. if nested_module and (
  61. namespace in config.namespace_packages
  62. or (
  63. config.auto_identify_namespace_packages
  64. and _is_namespace_package(module_path, config.supported_extensions)
  65. )
  66. ):
  67. return _src_path(nested_module[0], config, (module_path,), new_prefix)
  68. if (
  69. _is_module(module_path)
  70. or _is_package(module_path)
  71. or _src_path_is_module(src_path, root_module_name)
  72. ):
  73. return (sections.FIRSTPARTY, f"Found in one of the configured src_paths: {src_path}.")
  74. return None
  75. def _is_module(path: Path) -> bool:
  76. return (
  77. exists_case_sensitive(str(path.with_suffix(".py")))
  78. or any(
  79. exists_case_sensitive(str(path.with_suffix(ext_suffix)))
  80. for ext_suffix in importlib.machinery.EXTENSION_SUFFIXES
  81. )
  82. or exists_case_sensitive(str(path / "__init__.py"))
  83. )
  84. def _is_package(path: Path) -> bool:
  85. return exists_case_sensitive(str(path)) and path.is_dir()
  86. def _is_namespace_package(path: Path, src_extensions: FrozenSet[str]) -> bool:
  87. if not _is_package(path):
  88. return False
  89. init_file = path / "__init__.py"
  90. if not init_file.exists():
  91. filenames = [
  92. filepath
  93. for filepath in path.iterdir()
  94. if filepath.suffix.lstrip(".") in src_extensions
  95. or filepath.name.lower() in ("setup.cfg", "pyproject.toml")
  96. ]
  97. if filenames:
  98. return False
  99. else:
  100. with init_file.open("rb") as open_init_file:
  101. file_start = open_init_file.read(4096)
  102. if (
  103. b"__import__('pkg_resources').declare_namespace(__name__)" not in file_start
  104. and b'__import__("pkg_resources").declare_namespace(__name__)' not in file_start
  105. and b"__path__ = __import__('pkgutil').extend_path(__path__, __name__)"
  106. not in file_start
  107. and b'__path__ = __import__("pkgutil").extend_path(__path__, __name__)'
  108. not in file_start
  109. ):
  110. return False
  111. return True
  112. def _src_path_is_module(src_path: Path, module_name: str) -> bool:
  113. return (
  114. module_name == src_path.name and src_path.is_dir() and exists_case_sensitive(str(src_path))
  115. )