1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
|
"""
stringparser
~~~~~~~~~~~~
A simple way to match patterns and extract information within strings without
typing regular expressions. It can be considered as the inverse of `format`
as patterns are given using the familiar format string specification :pep:`3101`.
:copyright: (c) 2023 by Hernan E. Grecco.
:license: BSD, see LICENSE for more details.
"""
import copy
import re
import string
from functools import partial
from io import StringIO
from re import ( # noqa: F401
DOTALL,
IGNORECASE,
LOCALE,
MULTILINE,
UNICODE,
VERBOSE,
I,
L,
M,
S,
U,
X,
)
from typing import (
Any,
Callable,
Generator,
Iterable,
Literal,
Optional,
Union,
overload,
)
from typing_extensions import TypeAlias
class ObjectLike:
"""This class is used by string parser when
the string formatter contains attribute
access such as `{0.name}`.
"""
KEY_TYPES: TypeAlias = Union[str, int]
VALUE_TYPES: TypeAlias = Union[
str, int, float, list["VALUE_TYPES"], dict[KEY_TYPES, "VALUE_TYPES"], ObjectLike
]
_FORMATTER = string.Formatter()
# This is due to the fact that int, float, etc
# are not recognized as Callable[[str, ], Any]
_STRCALLABLE: TypeAlias = Union[
type,
Callable[
[
str,
],
VALUE_TYPES,
],
]
_REGEX2CONVERTER: TypeAlias = tuple[str, _STRCALLABLE]
# This dictionary maps each format type to a tuple containing
# 1. The regular expression to match the string
# 2. A callable that will used to convert the matched string into the
# appropriate Python object.
_REG: dict[Optional[str], _REGEX2CONVERTER] = {
None: (".*?", str),
"s": (".*?", str),
"d": ("[0-9]+?", int),
"b": ("[0-1]+?", partial(int, base=2)),
"o": ("[0-7]+?", partial(int, base=8)),
"x": ("[0-9a-f]+?", partial(int, base=16)),
"X": ("[0-9A-F]+?", partial(int, base=16)),
"e": ("[0-9]+\\.?[0-9]+(?:e[-+]?[0-9]+)?", float),
"E": ("[0-9]+\\.?[0-9]+(?:E[-+]?[0-9]+)?", float),
"f": ("[0-9]+\\.?[0-9]+", float),
"F": ("[0-9]+\\.?[0-9]+", float),
"g": ("[0-9]+\\.?[0-9]+(?:[eE][-+]?[0-9]+)?", float),
"G": ("[0-9]+\\.?[0-9]+(?:[eE][-+]?[0-9]+)?", float),
"%": ("[0-9]+\\.?[0-9]+%", lambda x: float(x[:-1]) / 100),
}
# This regex is used to match the parts within standard format specifier string
#
# [[fill]align][sign][#][0][width][,][.precision][type]
#
# fill ::= <a character other than '}'>
# align ::= "<" | ">" | "=" | "^"
# sign ::= "+" | "-" | " "
# width ::= integer
# precision ::= integer
# type ::= "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o"
# | "s" | "x" | "X" | "%"
_FMT: re.Pattern[str] = re.compile(
"(?P<align>(?P<fill>[^{}])?[<>=\\^])?"
"(?P<sign>[\\+\\- ])?(?P<alternate>#)?"
"(?P<zero>0)?(?P<width>[0-9]+)?(?P<comma>[,])?"
"(?P<precision>\\.[0-9]+)?(?P<type>[bcdeEfFgGnosxX%]+)?"
)
def fmt_to_regex(fmt: str) -> _REGEX2CONVERTER:
"""For a given standard format specifier string it returns
with the regex to match and the callable to convert from string.
Parameters
----------
fmt
Format specifier string as defined in :pep:3101.
Returns
-------
A tuple with a regex and a string to value callable.
Raises
------
ValueError
If the formatting string cannot be parsed
or contains invalid parts.
Examples
--------
>>> fmt_to_regex("{:d})
("[0-9]+?", int)
Notes
-----
`fill`, `align, `width`, `precision` are not implemented.
"""
matched = _FMT.search(fmt)
if matched is None: # pragma: no cover
raise ValueError(f"Could not parse the formatting string {fmt}")
(
_align,
_fill,
sign,
alternate,
_zero,
_width,
_comma,
_precision,
ctype,
) = matched.groups(default=None)
try:
reg, fun = _REG[ctype] # typing: ignore
except KeyError: # pragma: no cover
raise ValueError("{} is not an valid type".format(ctype))
if alternate:
if ctype in ("o", "x", "X", "b"):
reg = "0" + ctype + reg
else: # pragma: no cover
raise ValueError("Alternate form (#) not allowed in {} type".format(ctype))
if sign == "-" or sign is None:
reg = "[-]?" + reg
elif sign == "+":
reg = "[-+]" + reg
elif sign == " ":
reg = "[- ]" + reg
else: # pragma: no cover
raise ValueError("{} is not a valid sign".format(sign))
return reg, fun
_ITEM_ATTR: TypeAlias = Union[
tuple[Literal["attribute"], str], tuple[Literal["item"], Any]
]
def _split_field_name(name: str) -> Generator[_ITEM_ATTR, None, None]:
"""Split a compound field name containing attribute or item
access into multiple simple field names.
For example, `x.y[0]` yields the following sequence
`[('item', 'x'), ('attribute', 'y'), ('item', 0)]`
Parameters
----------
name
Simple or compound field name.
Yields
------
Tuple indicating
- `attribute` or `index`
- corresponding attribute name or key
Raises
------
ValueError
If the empty field is empty or is invalid.
"""
first = True
for namepart in name.split("."):
# Split that part by open bracket chars
keyparts = namepart.split("[")
# The first part is just a bare name
key = keyparts[0]
# Empty strings are not allowed as field names
if key == "": # pragma: no cover
raise ValueError(f"empty field name in {name}")
# The first name in the sequence is used to index
# the args/kwargs arrays. Subsequent names are used
# on the result of the previous operation.
if first:
first = False
yield ("item", key)
else:
yield ("attribute", key)
# Now process any bracket expressions which followed
# the first part.
for key in keyparts[1:]:
endbracket = key.find("]")
if endbracket < 0 or endbracket != len(key) - 1: # pragma: no cover
raise ValueError(f"Invalid field syntax in {name}")
# Strip off the closing bracket and try to coerce to int
key = key[:-1]
try:
yield ("item", int(key))
except ValueError:
yield ("item", key)
def _build_datastructure(field_parts: Iterable[_ITEM_ATTR], top: Any) -> Any:
"""Build a hierarchical datastructure of dictionary and ObjectLike.
Parameters
----------
field_parts
Iterable of simple field names and type
top
Element to be placed on the top of the datastructure
Returns
-------
A hierarchical datastructure.
"""
tmp: Union[dict[Any, Any], ObjectLike]
for typ, name in reversed(list(field_parts)):
if typ == "attribute":
tmp = ObjectLike()
setattr(tmp, name, top)
top = tmp
elif typ == "item":
tmp = dict()
tmp[name] = top
top = tmp
return top
def _append_to_datastructure(
bottom: Any, field_parts: Iterable[_ITEM_ATTR], top: Any
) -> None:
"""Append datastructure to another datastructure.
Parameters
----------
bottom
Existing datastructure.
field_parts
Iterable of simple field names and type.
top
Element to be placed on the top of the datastructure.
Raises
------
ValueError
If an incompatible accesor is found for a given value.
"""
for typ_, name in field_parts:
if isinstance(bottom, dict):
if not typ_ == "item": # pragma: no cover
raise ValueError(f"Incompatible {typ_}, {name}")
try:
bottom = bottom[name]
except KeyError:
bottom[name] = _build_datastructure(field_parts, top)
elif isinstance(bottom, ObjectLike):
if not typ_ == "attribute": # pragma: no cover
raise ValueError(f"Incompatible {typ_}, {name}")
try:
bottom = getattr(bottom, name)
except AttributeError: # pragma: no cover
setattr(bottom, name, _build_datastructure(field_parts, top))
else: # pragma: no cover
raise ValueError(f"Incompatible {typ_}, {name}")
def _set_in_datastructure(
bottom: Any, field_parts: Iterable[_ITEM_ATTR], top: Any
) -> None:
"""Traverse a datastructure and set the top element.
Parameters
----------
bottom
Existing datastructure.
field_parts
Iterable of simple field names and type
top
Element to be placed on the top of the datastructure
"""
for _typ, name in field_parts:
if isinstance(bottom, dict):
if bottom[name] is None:
bottom[name] = top
else:
_set_in_datastructure(bottom[name], field_parts, top)
elif isinstance(bottom, ObjectLike):
if getattr(bottom, name) is None:
setattr(bottom, name, top)
else:
_set_in_datastructure(getattr(bottom, name), field_parts, top)
elif isinstance(bottom, list):
if bottom[int(name)] is None:
bottom[int(name)] = top
else:
_set_in_datastructure(bottom[int(name)], field_parts, top)
@overload
def _convert(obj: None) -> None:
...
@overload
def _convert(
obj: dict[KEY_TYPES, VALUE_TYPES]
) -> Union[dict[KEY_TYPES, VALUE_TYPES], list[VALUE_TYPES]]:
...
@overload
def _convert(obj: ObjectLike) -> ObjectLike:
...
def _convert(obj):
"""Recursively traverse template data structure converting dictionaries
to lists if all keys are numbers which fill the range from [0, len(keys))
Parameters
----------
obj
Nested template data structure.
Returns
-------
Updated datastructure.
"""
if obj is None:
return None
elif isinstance(obj, dict):
try:
keys = sorted([int(key) for key in obj.keys()])
if min(keys) == 0 and max(keys) == len(keys) - 1:
return [_convert(obj[str(key)]) for key in keys]
except Exception:
pass
for key, value in obj.items():
obj[key] = _convert(value)
return obj
elif isinstance(obj, ObjectLike):
for key, value in obj.__dict__.items():
setattr(obj, key, _convert(value))
return obj
class Parser(object):
"""Callable object to parse a text line using a format string (PEP 3101)
as a template.
"""
# List of tuples (name of the field, converter function)
_fields: list[tuple[str, _STRCALLABLE]]
# If any of the fields has a non-numeric name, this variable is toggled
# and the return is a dictionary
_output_as_dict: bool
_template: Union[dict[KEY_TYPES, VALUE_TYPES], list[VALUE_TYPES], ObjectLike]
# Compiled regex pattern
_regex: re.Pattern[str]
def __init__(self, format_string: str, flags: Union[re.RegexFlag, int] = 0):
"""_summary_
Parameters
----------
format_string
PEP 3101 format string to be used as a template.
flags, optional
modifies the regex expression behaviour. Passed to re.compile, by default 0
"""
self._fields = []
self._output_as_dict = False
pattern = StringIO()
number = 0
# Assembly regex, list of fields, converter function,
# and output template data structure by inspecting
# each replacement field.
template: dict[Any, Any] = dict()
for literal, field, fmt, _conv in _FORMATTER.parse(format_string):
pattern.write(re.escape(literal))
if field is None:
continue
if fmt is None or fmt == "":
reg, fun = _REG["s"]
else:
reg, fun = fmt_to_regex(fmt)
# Ignored fields are added as non-capturing groups
# Named and unnamed fields are added as capturing groups
if field == "_":
pattern.write("(?:" + reg + ")")
continue
if not field or field[0] in (".", "["):
field = str(number) + field
number += 1
pattern.write("(" + reg + ")")
self._fields.append((field, fun))
_append_to_datastructure(template, _split_field_name(field), None)
self._template = _convert(template)
self._regex = re.compile("^" + pattern.getvalue() + "$", flags)
def __call__(self, text: str) -> VALUE_TYPES:
"""Parse a given string."""
# Try to match the text with the stored regex
mobj = self._regex.search(text)
if mobj is None:
raise ValueError(f"Could not parse '{text}' with '{self._regex.pattern}'")
# Put each matched string in the corresponding output slot in the template
parsed = copy.deepcopy(self._template)
for group, (field, fun) in zip(mobj.groups(), self._fields):
_set_in_datastructure(parsed, _split_field_name(field), fun(group))
# If the result is a list with a single object, return it without Container
if isinstance(parsed, list) and len(parsed) == 1:
return parsed[0]
return parsed
__all__ = ["Parser"]
|