summaryrefslogtreecommitdiff
path: root/pyparsing.py
diff options
context:
space:
mode:
authorPaul McGuire <ptmcg@austin.rr.com>2019-08-10 06:57:36 -0500
committerPaul McGuire <ptmcg@austin.rr.com>2019-08-10 06:57:36 -0500
commit675e87a859ce9f7bfefb091e9d6198c4a7f5eafd (patch)
treea980f282c739cf701283d08b2559886e2dc7d009 /pyparsing.py
parent7c1db54c6b4de188d7bebcc7372b926a13fd3da4 (diff)
downloadpyparsing-git-675e87a859ce9f7bfefb091e9d6198c4a7f5eafd.tar.gz
Rework __diag__ and __compat__ to be actual classes instead of just namespaces, to add helpful behavior and methods
Diffstat (limited to 'pyparsing.py')
-rw-r--r--pyparsing.py75
1 files changed, 59 insertions, 16 deletions
diff --git a/pyparsing.py b/pyparsing.py
index aa49a06..43f1abc 100644
--- a/pyparsing.py
+++ b/pyparsing.py
@@ -96,7 +96,7 @@ classes inherit from. Use the docstrings for examples of how to:
"""
__version__ = "2.5.0a1"
-__versionTime__ = "09 Aug 2019 11:27 UTC"
+__versionTime__ = "10 Aug 2019 11:56 UTC"
__author__ = "Paul McGuire <ptmcg@users.sourceforge.net>"
import string
@@ -127,24 +127,52 @@ from collections.abc import MutableMapping, Mapping
from collections import OrderedDict
from types import SimpleNamespace
-# version compatibility configuration
-__compat__ = SimpleNamespace()
-__compat__.__doc__ = """
+
+class __config_flags:
+ """Internal class for defining compatibility and debugging flags"""
+ _all_names = []
+ _fixed_names = []
+ _type_desc = "configuration"
+
+ @classmethod
+ def _set(cls, dname, value):
+ if dname in cls._fixed_names:
+ warnings.warn("{}.{} {} is {} and cannot be overridden".format(cls.__name__,
+ dname,
+ cls._type_desc,
+ str(getattr(cls, dname)).upper()))
+ if dname in cls._all_names:
+ setattr(cls, dname, value)
+ else:
+ raise ValueError("no such {} {!r}".format(cls._type_desc, dname))
+
+ enable = classmethod(lambda cls, name: cls._set(name, True))
+ disable = classmethod(lambda cls, name: cls._set(name, False))
+
+class __compat__(__config_flags):
+ """
A cross-version compatibility configuration for pyparsing features that will be
released in a future version. By setting values in this configuration to True,
those features can be enabled in prior versions for compatibility development
and testing.
- collect_all_And_tokens - flag to enable fix for Issue #63 that fixes erroneous grouping
- of results names when an And expression is nested within an Or or MatchFirst;
+ of results names when an And expression is nested within an Or or MatchFirst;
maintained for compatibility, but setting to False no longer restores pre-2.3.1
behavior
-"""
-__compat__.collect_all_And_tokens = True
+ """
+ _type_desc = "compatibility"
+
+ collect_all_And_tokens = True
-__diag__ = SimpleNamespace()
-__diag__.__doc__ = """
-Diagnostic configuration (all default to False)
+ _all_names = [__ for __ in locals() if not __.startswith('_')]
+ _fixed_names = """
+ collect_all_And_tokens
+ """.split()
+
+class __diag__(__config_flags):
+ """
+ Diagnostic configuration (all default to False)
- warn_multiple_tokens_in_named_alternation - flag to enable warnings when a results
name is defined on a MatchFirst or Or expression with one or more And subexpressions
- warn_ungrouped_named_tokens_in_collection - flag to enable warnings when a results
@@ -156,12 +184,27 @@ Diagnostic configuration (all default to False)
incorrectly called with multiple str arguments
- enable_debug_on_named_expressions - flag to auto-enable debug on all subsequent
calls to ParserElement.setName()
-"""
-__diag__.warn_multiple_tokens_in_named_alternation = False
-__diag__.warn_ungrouped_named_tokens_in_collection = False
-__diag__.warn_name_set_on_empty_Forward = False
-__diag__.warn_on_multiple_string_args_to_oneof = False
-__diag__.enable_debug_on_named_expressions = False
+ """
+ _type_desc = "diagnostic"
+
+ warn_multiple_tokens_in_named_alternation = False
+ warn_ungrouped_named_tokens_in_collection = False
+ warn_name_set_on_empty_Forward = False
+ warn_on_multiple_string_args_to_oneof = False
+ enable_debug_on_named_expressions = False
+
+ _all_names = [__ for __ in locals() if not __.startswith('_')]
+ _warning_names = [name for name in _all_names if name.startswith("warn")]
+ _debug_names = [name for name in _all_names if name.startswith("enable_debug")]
+
+ @classmethod
+ def enable_all_warnings(cls):
+ for name in cls._warning_names:
+ cls.enable(name)
+
+# hide abstract class
+del __config_flags
+
# ~ sys.stderr.write("testing pyparsing module, version %s, %s\n" % (__version__, __versionTime__))