del __config_flags
-def _should_enable_warnings(cmd_line_warn_options: List[str], warn_env_var: str) -> bool:
+def _should_enable_warnings(
+ cmd_line_warn_options: List[str], warn_env_var: str
+) -> bool:
enable = bool(warn_env_var)
for warn_opt in cmd_line_warn_options:
w_action, w_message, w_category, w_module, w_line = (warn_opt + "::::").split(
return enable
-if _should_enable_warnings(sys.warnoptions, os.environ.get("PYPARSINGENABLEALLWARNINGS")):
+if _should_enable_warnings(
+ sys.warnoptions, os.environ.get("PYPARSINGENABLEALLWARNINGS")
+):
enable_all_warnings()
self._parse = self._parse._originalParseMethod
return self
- def set_parse_action(self, *fns: ParseAction, **kwargs) -> OptionalType["ParserElement"]:
+ def set_parse_action(
+ self, *fns: ParseAction, **kwargs
+ ) -> OptionalType["ParserElement"]:
"""
Define one or more actions to perform when successfully matching parse element definition.
# -> ['ablaj', 'lskjd']
"""
import typing
+
if isinstance(other, str_type):
other = Suppress(other)
self.mayIndexError = False
self.asKeyword = asKeyword
+ # see if we can make a regex for this Word
if " " not in self.initChars | self.bodyChars and (min == 1 and exact == 0):
if self.bodyChars == self.initChars:
if max == 0:
elif max == 1:
repeat = ""
else:
- repeat = "{{{}}}".format(max)
+ repeat = "{{{},{}}}".format(
+ self.minLen,
+ "" if self.maxLen == _MAX_INT else self.maxLen
+ )
self.reString = "[{}]{}".format(
_collapseStringToRanges(self.initChars),
repeat,
def __init__(self, exprs: IterableType[ParserElement], savelist: bool = False):
super().__init__(savelist)
- self.exprs : List[ParserElement]
- exprs : Iterable[ParserElement]
+ self.exprs: List[ParserElement]
+ exprs: Iterable[ParserElement]
if isinstance(exprs, _generatorType):
exprs = list(exprs)
import datetime
import re
import sys
+import warnings
from types import SimpleNamespace
from io import StringIO
from textwrap import dedent
-from unittest import TestCase
+from typing import Any
+import unittest
import pyparsing as pp
from examples.jsonParser import jsonObject
return current_method_name(3) + ": "
+class TestCase(unittest.TestCase):
+ @contextlib.contextmanager
+ def assertRaises(self, expected_exception_type: Any, msg: Any = None):
+ """
+ Simple wrapper to print out the exceptions raised after assertRaises
+ """
+ with super().assertRaises(expected_exception_type, msg=msg) as ar:
+ yield
+
+ if getattr(ar, "exception", None) is not None:
+ print(
+ "Raised expected exception: {}: {}".format(
+ type(ar.exception).__name__, str(ar.exception)
+ )
+ )
+ else:
+ print(
+ "Expected {} exception not raised".format(
+ expected_exception_type.__name__
+ )
+ )
+ return ar
+
+ @contextlib.contextmanager
+ def assertDoesNotWarn(self, msg: str = None):
+ if msg is None:
+ msg = "unexpected warning raised"
+ with warnings.catch_warnings(record=True) as w:
+ warnings.simplefilter("error")
+ try:
+ yield
+ except Exception as e:
+ self.fail("{}: {}".format(msg, e))
+
+
class Test01_PyparsingTestInit(TestCase):
def runTest(self):
from pyparsing import (
class Test01a_PyparsingEnvironmentTests(TestCase):
def runTest(self):
# test warnings enable detection
+ # fmt: off
tests = [
(([], "",), False),
((["d", ], "",), True),
((["d:::blah", ], "1",), True),
((["i", ], "1",), False),
]
+ # fmt: on
all_success = True
for args, expected in tests:
def setUp(self):
self.suite_context.restore()
- @contextlib.contextmanager
- def assertRaises(self, expected_exception_type, msg=None):
- """
- Simple wrapper to print out the exceptions raised after assertRaises
- """
- try:
- with super().assertRaises(expected_exception_type, msg=msg) as ar:
- yield
- finally:
- if getattr(ar, "exception", None) is not None:
- print(
- "Raised expected exception: {}: {}".format(
- type(ar.exception).__name__, str(ar.exception)
- )
- )
- else:
- print(
- "Expected {} exception not raised".format(
- expected_exception_type.__name__
- )
- )
- return ar
-
def test000_assert_packrat_status(self):
print("Packrat enabled:", ParserElement._packratEnabled)
self.assertFalse(ParserElement._packratEnabled, "packrat enabled")
self.assertParseResultsEquals(testVal, expected_list=expected)
def testHTMLEntities(self):
- html_source = dedent("""\
- This & that
- 2 > 1
- 0 < 1
- Don't get excited!
- I said "Don't get excited!"
- Copyright © 2021
- Dot ⟶ ˙
- """)
+ html_source = dedent(
+ """\
+ This & that
+ 2 > 1
+ 0 < 1
+ Don't get excited!
+ I said "Don't get excited!"
+ Copyright © 2021
+ Dot ⟶ ˙
+ """
+ )
transformer = pp.common_html_entity.add_parse_action(pp.replace_html_entity)
transformed = transformer.transform_string(html_source)
print(transformed)
- expected = dedent("""\
- This & that
- 2 > 1
- 0 < 1
- Don't get excited!
- I said "Don't get excited!"
- Copyright © 2021
- Dot ⟶ ˙
- """)
+ expected = dedent(
+ """\
+ This & that
+ 2 > 1
+ 0 < 1
+ Don't get excited!
+ I said "Don't get excited!"
+ Copyright © 2021
+ Dot ⟶ ˙
+ """
+ )
self.assertEqual(expected, transformed)
def testInfixNotationBasicArithEval(self):
result, compare_list, msg="issue with ParseResults.insert()"
)
+ def testParseResultsAddingSuppressedTokenWithResultsName(self):
+ parser = "aaa" + (pp.NoMatch() | pp.Suppress("-"))("B")
+ try:
+ dd = parser.parse_string("aaa -").as_dict()
+ except RecursionError:
+ self.fail("fail getting named result when empty")
+
def testIgnoreString(self):
"""test ParserElement.ignore() passed a string arg"""
msg="using different openers and closers shouldn't affect resulting ParseResults",
)
+ def testWordMinMaxArgs(self):
+ parsers = [
+ "A" + pp.Word(pp.nums),
+ "A" + pp.Word(pp.nums, min=1),
+ "A" + pp.Word(pp.nums, max=6),
+ "A" + pp.Word(pp.nums, min=1, max=6),
+ "A" + pp.Word(pp.nums, min=1),
+ "A" + pp.Word(pp.nums, min=2),
+ "A" + pp.Word(pp.nums, min=2, max=6),
+ pp.Word("A", pp.nums),
+ pp.Word("A", pp.nums, min=1),
+ pp.Word("A", pp.nums, max=6),
+ pp.Word("A", pp.nums, min=1, max=6),
+ pp.Word("A", pp.nums, min=1),
+ pp.Word("A", pp.nums, min=2),
+ pp.Word("A", pp.nums, min=2, max=6),
+ pp.Word(pp.alphas, pp.nums),
+ pp.Word(pp.alphas, pp.nums, min=1),
+ pp.Word(pp.alphas, pp.nums, max=6),
+ pp.Word(pp.alphas, pp.nums, min=1, max=6),
+ pp.Word(pp.alphas, pp.nums, min=1),
+ pp.Word(pp.alphas, pp.nums, min=2),
+ pp.Word(pp.alphas, pp.nums, min=2, max=6),
+ ]
+
+ fails = []
+ for p in parsers:
+ print(p, getattr(p, "reString", "..."), end=" ", flush=True)
+ try:
+ p.parseString("A123")
+ except Exception as e:
+ print(" <<< FAIL")
+ fails.append(p)
+ else:
+ print()
+ if fails:
+ self.fail("{} failed to match".format(",".join(str(f) for f in fails)))
+
def testWordExclude(self):
allButPunc = pp.Word(pp.printables, excludeChars=".,:;-_!?")
def testCommonUrlParts(self):
from urllib.parse import urlparse
+
sample_url = "https://bob:secret@www.example.com:8080/path/to/resource?filter=int#book-mark"
parts = urlparse(sample_url)
- warn_on_multiple_string_args_to_oneof - flag to enable warnings when oneOf is
incorrectly called with multiple str arguments (default=True)
"""
+ with self.assertDoesNotWarn(
+ "warn_on_multiple_string_args_to_oneof warning raised when not enabled"
+ ):
+ a = pp.oneOf("A", "B")
with ppt.reset_pyparsing_context():
pp.enable_diag(pp.Diagnostics.warn_on_multiple_string_args_to_oneof)
self.assertFalse(a.customName)
pp.autoname_elements()
self.assertTrue(a.debug)
- self.assertEqual('a', a.name)
- self.assertEqual('bbb', b.name)
+ self.assertEqual("a", a.name)
+ self.assertEqual("bbb", b.name)
def testEnableDebugOnNamedExpressions(self):
"""
)
def testWarnUsingLshiftForward(self):
- import warnings
print(
"verify that using '<<' operator with a Forward raises a warning if there is a dangling '|' operator"
c = fwd | pp.Word("c")
print("safe << and (|), should not warn")
- with warnings.catch_warnings(record=True) as w:
- warnings.simplefilter("error")
-
+ with self.assertDoesNotWarn(
+ "warning raised on safe use of << with Forward and MatchFirst"
+ ):
fwd = pp.Forward()
fwd << (pp.Word("a") | pp.Word("b"))
- try:
- c = fwd | pp.Word("c")
- except Exception as e:
- self.fail("raised warning when it should not have")
+ c = fwd | pp.Word("c")
def testParseExpressionsWithRegex(self):
from itertools import product
def testEmptyExpressionsAreHandledProperly(self):
from pyparsing.diagram import to_railroad
+
for cls in (pp.And, pp.Or, pp.MatchFirst, pp.Each):
print("testing empty", cls.__name__)
expr = cls([])