__init__.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 .__meta__ import __version__, __version_info__ # noqa: F401
  23. from . import css_parser as cp
  24. from . import css_match as cm
  25. from . import css_types as ct
  26. from .util import DEBUG, SelectorSyntaxError # noqa: F401
  27. import bs4 # type: ignore[import]
  28. from typing import Dict, Optional, Any, List, Iterator, Iterable
  29. __all__ = (
  30. 'DEBUG', 'SelectorSyntaxError', 'SoupSieve',
  31. 'closest', 'compile', 'filter', 'iselect',
  32. 'match', 'select', 'select_one'
  33. )
  34. SoupSieve = cm.SoupSieve
  35. def compile( # noqa: A001
  36. pattern: str,
  37. namespaces: Optional[Dict[str, str]] = None,
  38. flags: int = 0,
  39. *,
  40. custom: Optional[Dict[str, str]] = None,
  41. **kwargs: Any
  42. ) -> cm.SoupSieve:
  43. """Compile CSS pattern."""
  44. ns = ct.Namespaces(namespaces) if namespaces is not None else namespaces # type: Optional[ct.Namespaces]
  45. cs = ct.CustomSelectors(custom) if custom is not None else custom # type: Optional[ct.CustomSelectors]
  46. if isinstance(pattern, SoupSieve):
  47. if flags:
  48. raise ValueError("Cannot process 'flags' argument on a compiled selector list")
  49. elif namespaces is not None:
  50. raise ValueError("Cannot process 'namespaces' argument on a compiled selector list")
  51. elif custom is not None:
  52. raise ValueError("Cannot process 'custom' argument on a compiled selector list")
  53. return pattern
  54. return cp._cached_css_compile(pattern, ns, cs, flags)
  55. def purge() -> None:
  56. """Purge cached patterns."""
  57. cp._purge_cache()
  58. def closest(
  59. select: str,
  60. tag: 'bs4.Tag',
  61. namespaces: Optional[Dict[str, str]] = None,
  62. flags: int = 0,
  63. *,
  64. custom: Optional[Dict[str, str]] = None,
  65. **kwargs: Any
  66. ) -> 'bs4.Tag':
  67. """Match closest ancestor."""
  68. return compile(select, namespaces, flags, **kwargs).closest(tag)
  69. def match(
  70. select: str,
  71. tag: 'bs4.Tag',
  72. namespaces: Optional[Dict[str, str]] = None,
  73. flags: int = 0,
  74. *,
  75. custom: Optional[Dict[str, str]] = None,
  76. **kwargs: Any
  77. ) -> bool:
  78. """Match node."""
  79. return compile(select, namespaces, flags, **kwargs).match(tag)
  80. def filter( # noqa: A001
  81. select: str,
  82. iterable: Iterable['bs4.Tag'],
  83. namespaces: Optional[Dict[str, str]] = None,
  84. flags: int = 0,
  85. *,
  86. custom: Optional[Dict[str, str]] = None,
  87. **kwargs: Any
  88. ) -> List['bs4.Tag']:
  89. """Filter list of nodes."""
  90. return compile(select, namespaces, flags, **kwargs).filter(iterable)
  91. def select_one(
  92. select: str,
  93. tag: 'bs4.Tag',
  94. namespaces: Optional[Dict[str, str]] = None,
  95. flags: int = 0,
  96. *,
  97. custom: Optional[Dict[str, str]] = None,
  98. **kwargs: Any
  99. ) -> 'bs4.Tag':
  100. """Select a single tag."""
  101. return compile(select, namespaces, flags, **kwargs).select_one(tag)
  102. def select(
  103. select: str,
  104. tag: 'bs4.Tag',
  105. namespaces: Optional[Dict[str, str]] = None,
  106. limit: int = 0,
  107. flags: int = 0,
  108. *,
  109. custom: Optional[Dict[str, str]] = None,
  110. **kwargs: Any
  111. ) -> List['bs4.Tag']:
  112. """Select the specified tags."""
  113. return compile(select, namespaces, flags, **kwargs).select(tag, limit)
  114. def iselect(
  115. select: str,
  116. tag: 'bs4.Tag',
  117. namespaces: Optional[Dict[str, str]] = None,
  118. limit: int = 0,
  119. flags: int = 0,
  120. *,
  121. custom: Optional[Dict[str, str]] = None,
  122. **kwargs: Any
  123. ) -> Iterator['bs4.Tag']:
  124. """Iterate the specified tags."""
  125. for el in compile(select, namespaces, flags, **kwargs).iselect(tag, limit):
  126. yield el
  127. def escape(ident: str) -> str:
  128. """Escape identifier."""
  129. return cp.escape(ident)