1# actions.py
2from __future__ import annotations
3
4from typing import Union, Callable, Any
5
6from .exceptions import ParseException
7from .util import col, replaced_by_pep8
8from .results import ParseResults
9
10
11ParseAction = Union[
12 Callable[[], Any],
13 Callable[[ParseResults], Any],
14 Callable[[int, ParseResults], Any],
15 Callable[[str, int, ParseResults], Any],
16]
17
18
19class OnlyOnce:
20 """
21 Wrapper for parse actions, to ensure they are only called once.
22 Note: parse action signature must include all 3 arguments.
23 """
24
25 def __init__(self, method_call: Callable[[str, int, ParseResults], Any]) -> None:
26 from .core import _trim_arity
27
28 self.callable = _trim_arity(method_call)
29 self.called = False
30
31 def __call__(self, s: str, l: int, t: ParseResults) -> ParseResults:
32 if not self.called:
33 results = self.callable(s, l, t)
34 self.called = True
35 return results
36 raise ParseException(s, l, "OnlyOnce obj called multiple times w/out reset")
37
38 def reset(self):
39 """
40 Allow the associated parse action to be called once more.
41 """
42
43 self.called = False
44
45
46def match_only_at_col(n: int) -> ParseAction:
47 """
48 Helper method for defining parse actions that require matching at
49 a specific column in the input text.
50 """
51
52 def verify_col(strg: str, locn: int, toks: ParseResults) -> None:
53 if col(locn, strg) != n:
54 raise ParseException(strg, locn, f"matched token not at column {n}")
55
56 return verify_col
57
58
59def replace_with(repl_str: Any) -> ParseAction:
60 """
61 Helper method for common parse actions that simply return
62 a literal value. Especially useful when used with
63 :class:`transform_string<ParserElement.transform_string>` ().
64
65 Example::
66
67 num = Word(nums).set_parse_action(lambda toks: int(toks[0]))
68 na = one_of("N/A NA").set_parse_action(replace_with(math.nan))
69 term = na | num
70
71 term[1, ...].parse_string("324 234 N/A 234") # -> [324, 234, nan, 234]
72 """
73 return lambda s, l, t: [repl_str]
74
75
76def remove_quotes(s: str, l: int, t: ParseResults) -> Any:
77 """
78 Helper parse action for removing quotation marks from parsed
79 quoted strings, that use a single character for quoting. For parsing
80 strings that may have multiple characters, use the QuotedString class.
81
82 Example::
83
84 # by default, quotation marks are included in parsed results
85 quoted_string.parse_string("'Now is the Winter of our Discontent'") # -> ["'Now is the Winter of our Discontent'"]
86
87 # use remove_quotes to strip quotation marks from parsed results
88 quoted_string.set_parse_action(remove_quotes)
89 quoted_string.parse_string("'Now is the Winter of our Discontent'") # -> ["Now is the Winter of our Discontent"]
90 """
91 return t[0][1:-1]
92
93
94def with_attribute(*args: tuple[str, str], **attr_dict) -> ParseAction:
95 """
96 Helper to create a validating parse action to be used with start
97 tags created with :class:`make_xml_tags` or
98 :class:`make_html_tags`. Use ``with_attribute`` to qualify
99 a starting tag with a required attribute value, to avoid false
100 matches on common tags such as ``<TD>`` or ``<DIV>``.
101
102 Call ``with_attribute`` with a series of attribute names and
103 values. Specify the list of filter attributes names and values as:
104
105 - keyword arguments, as in ``(align="right")``, or
106 - as an explicit dict with ``**`` operator, when an attribute
107 name is also a Python reserved word, as in ``**{"class":"Customer", "align":"right"}``
108 - a list of name-value tuples, as in ``(("ns1:class", "Customer"), ("ns2:align", "right"))``
109
110 For attribute names with a namespace prefix, you must use the second
111 form. Attribute names are matched insensitive to upper/lower case.
112
113 If just testing for ``class`` (with or without a namespace), use
114 :class:`with_class`.
115
116 To verify that the attribute exists, but without specifying a value,
117 pass ``with_attribute.ANY_VALUE`` as the value.
118
119 Example::
120
121 html = '''
122 <div>
123 Some text
124 <div type="grid">1 4 0 1 0</div>
125 <div type="graph">1,3 2,3 1,1</div>
126 <div>this has no type</div>
127 </div>
128 '''
129 div,div_end = make_html_tags("div")
130
131 # only match div tag having a type attribute with value "grid"
132 div_grid = div().set_parse_action(with_attribute(type="grid"))
133 grid_expr = div_grid + SkipTo(div | div_end)("body")
134 for grid_header in grid_expr.search_string(html):
135 print(grid_header.body)
136
137 # construct a match with any div tag having a type attribute, regardless of the value
138 div_any_type = div().set_parse_action(with_attribute(type=with_attribute.ANY_VALUE))
139 div_expr = div_any_type + SkipTo(div | div_end)("body")
140 for div_header in div_expr.search_string(html):
141 print(div_header.body)
142
143 prints::
144
145 1 4 0 1 0
146
147 1 4 0 1 0
148 1,3 2,3 1,1
149 """
150 attrs_list: list[tuple[str, str]] = []
151 if args:
152 attrs_list.extend(args)
153 else:
154 attrs_list.extend(attr_dict.items())
155
156 def pa(s: str, l: int, tokens: ParseResults) -> None:
157 for attrName, attrValue in attrs_list:
158 if attrName not in tokens:
159 raise ParseException(s, l, "no matching attribute " + attrName)
160 if attrValue != with_attribute.ANY_VALUE and tokens[attrName] != attrValue: # type: ignore [attr-defined]
161 raise ParseException(
162 s,
163 l,
164 f"attribute {attrName!r} has value {tokens[attrName]!r}, must be {attrValue!r}",
165 )
166
167 return pa
168
169
170with_attribute.ANY_VALUE = object() # type: ignore [attr-defined]
171
172
173def with_class(classname: str, namespace: str = "") -> ParseAction:
174 """
175 Simplified version of :class:`with_attribute` when
176 matching on a div class - made difficult because ``class`` is
177 a reserved word in Python.
178
179 Example::
180
181 html = '''
182 <div>
183 Some text
184 <div class="grid">1 4 0 1 0</div>
185 <div class="graph">1,3 2,3 1,1</div>
186 <div>this <div> has no class</div>
187 </div>
188
189 '''
190 div,div_end = make_html_tags("div")
191 div_grid = div().set_parse_action(with_class("grid"))
192
193 grid_expr = div_grid + SkipTo(div | div_end)("body")
194 for grid_header in grid_expr.search_string(html):
195 print(grid_header.body)
196
197 div_any_type = div().set_parse_action(with_class(withAttribute.ANY_VALUE))
198 div_expr = div_any_type + SkipTo(div | div_end)("body")
199 for div_header in div_expr.search_string(html):
200 print(div_header.body)
201
202 prints::
203
204 1 4 0 1 0
205
206 1 4 0 1 0
207 1,3 2,3 1,1
208 """
209 classattr = f"{namespace}:class" if namespace else "class"
210 return with_attribute(**{classattr: classname})
211
212
213# Compatibility synonyms
214# fmt: off
215replaceWith = replaced_by_pep8("replaceWith", replace_with)
216removeQuotes = replaced_by_pep8("removeQuotes", remove_quotes)
217withAttribute = replaced_by_pep8("withAttribute", with_attribute)
218withClass = replaced_by_pep8("withClass", with_class)
219matchOnlyAtCol = replaced_by_pep8("matchOnlyAtCol", match_only_at_col)
220# fmt: on