comparison env/lib/python3.9/site-packages/soupsieve/__init__.py @ 0:4f3585e2f14b draft default tip

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