Path: blob/master/venv/Lib/site-packages/soupsieve/__init__.py
811 views
"""1Soup Sieve.23A CSS selector filter for BeautifulSoup4.45MIT License67Copyright (c) 2018 Isaac Muse89Permission is hereby granted, free of charge, to any person obtaining a copy10of this software and associated documentation files (the "Software"), to deal11in the Software without restriction, including without limitation the rights12to use, copy, modify, merge, publish, distribute, sublicense, and/or sell13copies of the Software, and to permit persons to whom the Software is14furnished to do so, subject to the following conditions:1516The above copyright notice and this permission notice shall be included in all17copies or substantial portions of the Software.1819THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR20IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,21FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE22AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER23LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,24OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE25SOFTWARE.26"""27from .__meta__ import __version__, __version_info__ # noqa: F40128from . import css_parser as cp29from . import css_match as cm30from . import css_types as ct31from .util import DEBUG, SelectorSyntaxError # noqa: F4013233__all__ = (34'DEBUG', 'SelectorSyntaxError', 'SoupSieve',35'closest', 'compile', 'filter', 'iselect',36'match', 'select', 'select_one'37)3839SoupSieve = cm.SoupSieve404142def compile(pattern, namespaces=None, flags=0, **kwargs): # noqa: A00143"""Compile CSS pattern."""4445if namespaces is not None:46namespaces = ct.Namespaces(**namespaces)4748custom = kwargs.get('custom')49if custom is not None:50custom = ct.CustomSelectors(**custom)5152if isinstance(pattern, SoupSieve):53if flags:54raise ValueError("Cannot process 'flags' argument on a compiled selector list")55elif namespaces is not None:56raise ValueError("Cannot process 'namespaces' argument on a compiled selector list")57elif custom is not None:58raise ValueError("Cannot process 'custom' argument on a compiled selector list")59return pattern6061return cp._cached_css_compile(pattern, namespaces, custom, flags)626364def purge():65"""Purge cached patterns."""6667cp._purge_cache()686970def closest(select, tag, namespaces=None, flags=0, **kwargs):71"""Match closest ancestor."""7273return compile(select, namespaces, flags, **kwargs).closest(tag)747576def match(select, tag, namespaces=None, flags=0, **kwargs):77"""Match node."""7879return compile(select, namespaces, flags, **kwargs).match(tag)808182def filter(select, iterable, namespaces=None, flags=0, **kwargs): # noqa: A00183"""Filter list of nodes."""8485return compile(select, namespaces, flags, **kwargs).filter(iterable)868788def select_one(select, tag, namespaces=None, flags=0, **kwargs):89"""Select a single tag."""9091return compile(select, namespaces, flags, **kwargs).select_one(tag)929394def select(select, tag, namespaces=None, limit=0, flags=0, **kwargs):95"""Select the specified tags."""9697return compile(select, namespaces, flags, **kwargs).select(tag, limit)9899100def iselect(select, tag, namespaces=None, limit=0, flags=0, **kwargs):101"""Iterate the specified tags."""102103for el in compile(select, namespaces, flags, **kwargs).iselect(tag, limit):104yield el105106107def escape(ident):108"""Escape identifier."""109110return cp.escape(ident)111112113