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