__init__.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. """
  2. Soup Sieve.
  3. A CSS selector filter for BeautifulSoup4.
  4. MIT License
  5. Copyright (c) 2018 Isaac Muse
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in all
  13. copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. SOFTWARE.
  21. """
  22. from __future__ import annotations
  23. from .__meta__ import __version__, __version_info__ # noqa: F401
  24. from . import css_parser as cp
  25. from . import css_match as cm
  26. from . import css_types as ct
  27. from .util import DEBUG, SelectorSyntaxError # noqa: F401
  28. import bs4
  29. from typing import Any, Iterator, Iterable
  30. __all__ = (
  31. 'DEBUG', 'SelectorSyntaxError', 'SoupSieve',
  32. 'closest', 'compile', 'filter', 'iselect',
  33. 'match', 'select', 'select_one'
  34. )
  35. SoupSieve = cm.SoupSieve
  36. def compile( # noqa: A001
  37. pattern: str,
  38. namespaces: dict[str, str] | None = None,
  39. flags: int = 0,
  40. *,
  41. custom: dict[str, str] | None = None,
  42. **kwargs: Any
  43. ) -> cm.SoupSieve:
  44. """Compile CSS pattern."""
  45. if isinstance(pattern, SoupSieve):
  46. if flags:
  47. raise ValueError("Cannot process 'flags' argument on a compiled selector list")
  48. elif namespaces is not None:
  49. raise ValueError("Cannot process 'namespaces' argument on a compiled selector list")
  50. elif custom is not None:
  51. raise ValueError("Cannot process 'custom' argument on a compiled selector list")
  52. return pattern
  53. return cp._cached_css_compile(
  54. pattern,
  55. ct.Namespaces(namespaces) if namespaces is not None else namespaces,
  56. ct.CustomSelectors(custom) if custom is not None else custom,
  57. flags
  58. )
  59. def purge() -> None:
  60. """Purge cached patterns."""
  61. cp._purge_cache()
  62. def closest(
  63. select: str,
  64. tag: bs4.Tag,
  65. namespaces: dict[str, str] | None = None,
  66. flags: int = 0,
  67. *,
  68. custom: dict[str, str] | None = None,
  69. **kwargs: Any
  70. ) -> bs4.Tag | None:
  71. """Match closest ancestor."""
  72. return compile(select, namespaces, flags, **kwargs).closest(tag)
  73. def match(
  74. select: str,
  75. tag: bs4.Tag,
  76. namespaces: dict[str, str] | None = None,
  77. flags: int = 0,
  78. *,
  79. custom: dict[str, str] | None = None,
  80. **kwargs: Any
  81. ) -> bool:
  82. """Match node."""
  83. return compile(select, namespaces, flags, **kwargs).match(tag)
  84. def filter( # noqa: A001
  85. select: str,
  86. iterable: Iterable[bs4.Tag],
  87. namespaces: dict[str, str] | None = None,
  88. flags: int = 0,
  89. *,
  90. custom: dict[str, str] | None = None,
  91. **kwargs: Any
  92. ) -> list[bs4.Tag]:
  93. """Filter list of nodes."""
  94. return compile(select, namespaces, flags, **kwargs).filter(iterable)
  95. def select_one(
  96. select: str,
  97. tag: bs4.Tag,
  98. namespaces: dict[str, str] | None = None,
  99. flags: int = 0,
  100. *,
  101. custom: dict[str, str] | None = None,
  102. **kwargs: Any
  103. ) -> bs4.Tag | None:
  104. """Select a single tag."""
  105. return compile(select, namespaces, flags, **kwargs).select_one(tag)
  106. def select(
  107. select: str,
  108. tag: bs4.Tag,
  109. namespaces: dict[str, str] | None = None,
  110. limit: int = 0,
  111. flags: int = 0,
  112. *,
  113. custom: dict[str, str] | None = None,
  114. **kwargs: Any
  115. ) -> list[bs4.Tag]:
  116. """Select the specified tags."""
  117. return compile(select, namespaces, flags, **kwargs).select(tag, limit)
  118. def iselect(
  119. select: str,
  120. tag: bs4.Tag,
  121. namespaces: dict[str, str] | None = None,
  122. limit: int = 0,
  123. flags: int = 0,
  124. *,
  125. custom: dict[str, str] | None = None,
  126. **kwargs: Any
  127. ) -> Iterator[bs4.Tag]:
  128. """Iterate the specified tags."""
  129. yield from compile(select, namespaces, flags, **kwargs).iselect(tag, limit)
  130. def escape(ident: str) -> str:
  131. """Escape identifier."""
  132. return cp.escape(ident)