|
From: <mi...@us...> - 2024-01-07 10:21:15
|
Revision: 9506
http://sourceforge.net/p/docutils/code/9506
Author: milde
Date: 2024-01-07 10:21:12 +0000 (Sun, 07 Jan 2024)
Log Message:
-----------
Simplify `frontend.validate_*()` function interface.
Allow `frontend.validate_*()` functions to be called with just
the "value" argument.
Keep the legacy interface for use with "optparse".
Modified Paths:
--------------
trunk/docutils/HISTORY.txt
trunk/docutils/docutils/frontend.py
trunk/docutils/test/test_settings.py
Modified: trunk/docutils/HISTORY.txt
===================================================================
--- trunk/docutils/HISTORY.txt 2024-01-07 10:21:01 UTC (rev 9505)
+++ trunk/docutils/HISTORY.txt 2024-01-07 10:21:12 UTC (rev 9506)
@@ -30,6 +30,11 @@
- The <image> element accepts the new attribute "loading".
+* docutils/frontend.py
+
+ - Allow `validate_*()` functions to be called with just the "value"
+ argument but keep the legacy interface for use with optparse.
+
* docutils/io.py
- Simpler and more secure `input encoding`_ default behaviour:
Modified: trunk/docutils/docutils/frontend.py
===================================================================
--- trunk/docutils/docutils/frontend.py 2024-01-07 10:21:01 UTC (rev 9505)
+++ trunk/docutils/docutils/frontend.py 2024-01-07 10:21:12 UTC (rev 9506)
@@ -27,10 +27,10 @@
Also exports the following functions:
Interface function:
- `get_default_settings()`. New in 0.19.
+ `get_default_settings()`. New in 0.19.
Option callbacks:
- `store_multiple()`, `read_config_file()`. Deprecated.
+ `store_multiple()`, `read_config_file()`. Deprecated.
Setting validators:
`validate_encoding()`, `validate_encoding_error_handler()`,
@@ -90,8 +90,13 @@
parser.values.update(new_settings, parser)
-def validate_encoding(setting, value, option_parser,
+def validate_encoding(setting, value=None, option_parser=None,
config_parser=None, config_section=None):
+ # All arguments except `value` are ignored
+ # (kept for compatibility with "optparse" module).
+ # If there is only one positional argument, it is interpreted as `value`.
+ if value is None:
+ value = setting
if value == '':
return None # allow overwriting a config file value
try:
@@ -102,8 +107,13 @@
return value
-def validate_encoding_error_handler(setting, value, option_parser,
+def validate_encoding_error_handler(setting, value=None, option_parser=None,
config_parser=None, config_section=None):
+ # All arguments except `value` are ignored
+ # (kept for compatibility with "optparse" module).
+ # If there is only one positional argument, it is interpreted as `value`.
+ if value is None:
+ value = setting
try:
codecs.lookup_error(value)
except LookupError:
@@ -119,13 +129,11 @@
setting, value, option_parser, config_parser=None, config_section=None):
"""
Side-effect: if an error handler is included in the value, it is inserted
- into the appropriate place as if it was a separate setting/option.
+ into the appropriate place as if it were a separate setting/option.
"""
if ':' in value:
encoding, handler = value.split(':')
- validate_encoding_error_handler(
- setting + '_error_handler', handler, option_parser,
- config_parser, config_section)
+ validate_encoding_error_handler(handler)
if config_parser:
config_parser.set(config_section, setting + '_error_handler',
handler)
@@ -133,42 +141,57 @@
setattr(option_parser.values, setting + '_error_handler', handler)
else:
encoding = value
- validate_encoding(setting, encoding, option_parser,
- config_parser, config_section)
- return encoding
+ return validate_encoding(encoding)
-def validate_boolean(setting, value, option_parser,
+def validate_boolean(setting, value=None, option_parser=None,
config_parser=None, config_section=None):
"""Check/normalize boolean settings:
True: '1', 'on', 'yes', 'true'
False: '0', 'off', 'no','false', ''
+
+ All arguments except `value` are ignored
+ (kept for compatibility with "optparse" module).
+ If there is only one positional argument, it is interpreted as `value`.
"""
+ if value is None:
+ value = setting
if isinstance(value, bool):
return value
try:
- return option_parser.booleans[value.strip().lower()]
+ return OptionParser.booleans[value.strip().lower()]
except KeyError:
raise LookupError('unknown boolean value: "%s"' % value)
-def validate_ternary(setting, value, option_parser,
+def validate_ternary(setting, value=None, option_parser=None,
config_parser=None, config_section=None):
"""Check/normalize three-value settings:
True: '1', 'on', 'yes', 'true'
False: '0', 'off', 'no','false', ''
any other value: returned as-is.
+
+ All arguments except `value` are ignored
+ (kept for compatibility with "optparse" module).
+ If there is only one positional argument, it is interpreted as `value`.
"""
+ if value is None:
+ value = setting
if isinstance(value, bool) or value is None:
return value
try:
- return option_parser.booleans[value.strip().lower()]
+ return OptionParser.booleans[value.strip().lower()]
except KeyError:
return value
-def validate_nonnegative_int(setting, value, option_parser,
+def validate_nonnegative_int(setting, value=None, option_parser=None,
config_parser=None, config_section=None):
+ # All arguments except `value` are ignored
+ # (kept for compatibility with "optparse" module).
+ # If there is only one positional argument, it is interpreted as `value`.
+ if value is None:
+ value = setting
value = int(value)
if value < 0:
raise ValueError('negative value; must be positive or zero')
@@ -175,19 +198,30 @@
return value
-def validate_threshold(setting, value, option_parser,
+def validate_threshold(setting, value=None, option_parser=None,
config_parser=None, config_section=None):
+ # All arguments except `value` are ignored
+ # (kept for compatibility with "optparse" module).
+ # If there is only one positional argument, it is interpreted as `value`.
+ if value is None:
+ value = setting
try:
return int(value)
except ValueError:
try:
- return option_parser.thresholds[value.lower()]
+ return OptionParser.thresholds[value.lower()]
except (KeyError, AttributeError):
raise LookupError('unknown threshold: %r.' % value)
def validate_colon_separated_string_list(
- setting, value, option_parser, config_parser=None, config_section=None):
+ setting, value=None, option_parser=None,
+ config_parser=None, config_section=None):
+ # All arguments except `value` are ignored
+ # (kept for compatibility with "optparse" module).
+ # If there is only one positional argument, it is interpreted as `value`.
+ if value is None:
+ value = setting
if not isinstance(value, list):
value = value.split(':')
else:
@@ -196,10 +230,16 @@
return value
-def validate_comma_separated_list(setting, value, option_parser,
+def validate_comma_separated_list(setting, value=None, option_parser=None,
config_parser=None, config_section=None):
"""Check/normalize list arguments (split at "," and strip whitespace).
+
+ All arguments except `value` are ignored
+ (kept for compatibility with "optparse" module).
+ If there is only one positional argument, it is interpreted as `value`.
"""
+ if value is None:
+ value = setting
# `value` may be ``bytes``, ``str``, or a ``list`` (when given as
# command line option and "action" is "append").
if not isinstance(value, list):
@@ -212,8 +252,13 @@
return value
-def validate_url_trailing_slash(
- setting, value, option_parser, config_parser=None, config_section=None):
+def validate_url_trailing_slash(setting, value=None, option_parser=None,
+ config_parser=None, config_section=None):
+ # All arguments except `value` are ignored
+ # (kept for compatibility with "optparse" module).
+ # If there is only one positional argument, it is interpreted as `value`.
+ if value is None:
+ value = setting
if not value:
return './'
elif value.endswith('/'):
@@ -222,8 +267,13 @@
return value + '/'
-def validate_dependency_file(setting, value, option_parser,
+def validate_dependency_file(setting, value=None, option_parser=None,
config_parser=None, config_section=None):
+ # All arguments except `value` are ignored
+ # (kept for compatibility with "optparse" module).
+ # If there is only one positional argument, it is interpreted as `value`.
+ if value is None:
+ value = setting
try:
return utils.DependencyList(value)
except OSError:
@@ -231,11 +281,15 @@
return utils.DependencyList(None)
-def validate_strip_class(setting, value, option_parser,
+def validate_strip_class(setting, value=None, option_parser=None,
config_parser=None, config_section=None):
+ # All arguments except `value` are ignored
+ # (kept for compatibility with "optparse" module).
+ # If there is only one positional argument, it is interpreted as `value`.
+ if value is None:
+ value = setting
# value is a comma separated string list:
- value = validate_comma_separated_list(setting, value, option_parser,
- config_parser, config_section)
+ value = validate_comma_separated_list(value)
# validate list elements:
for cls in value:
normalized = docutils.nodes.make_id(cls)
@@ -245,15 +299,20 @@
return value
-def validate_smartquotes_locales(setting, value, option_parser,
+def validate_smartquotes_locales(setting, value=None, option_parser=None,
config_parser=None, config_section=None):
"""Check/normalize a comma separated list of smart quote definitions.
- Return a list of (language-tag, quotes) string tuples."""
+ Return a list of (language-tag, quotes) string tuples.
+ All arguments except `value` are ignored
+ (kept for compatibility with "optparse" module).
+ If there is only one positional argument, it is interpreted as `value`.
+ """
+ if value is None:
+ value = setting
# value is a comma separated string list:
- value = validate_comma_separated_list(setting, value, option_parser,
- config_parser, config_section)
+ value = validate_comma_separated_list(value)
# validate list elements
lc_quotes = []
for item in value:
Modified: trunk/docutils/test/test_settings.py
===================================================================
--- trunk/docutils/test/test_settings.py 2024-01-07 10:21:01 UTC (rev 9505)
+++ trunk/docutils/test/test_settings.py 2024-01-07 10:21:12 UTC (rev 9506)
@@ -287,10 +287,8 @@
)
def test_validate_boolean(self):
- for t in self.boolean_settings:
- self.assertEqual(
- frontend.validate_boolean(None, t[0], self.option_parser),
- t[1])
+ for v, result in self.boolean_settings:
+ self.assertEqual(frontend.validate_boolean(v), result)
def test_validate_ternary(self):
tests = (
@@ -297,46 +295,50 @@
('500V', '500V'),
('parrot', 'parrot'),
)
- for t in self.boolean_settings + tests:
+ for v, result in self.boolean_settings + tests:
+ self.assertEqual(frontend.validate_ternary(v), result)
+
+ def test_validate_threshold(self):
+ tests = (('1', 1),
+ ('info', 1),
+ ('warning', 2),
+ ('error', 3),
+ ('severe', 4),
+ ('none', 5),
+ )
+ for v, result in tests:
self.assertEqual(
- frontend.validate_ternary(None, t[0], self.option_parser),
- t[1])
+ frontend.validate_threshold(v), result)
+ with self.assertRaisesRegex(LookupError, "unknown threshold: 'debug'"):
+ frontend.validate_threshold('debug')
def test_validate_colon_separated_string_list(self):
- tests = (
- ('a', ['a']),
- ('a:b', ['a', 'b']),
- (['a'], ['a']),
- (['a', 'b:c'], ['a', 'b', 'c']),
- )
- for t in tests:
- self.assertEqual(frontend.validate_colon_separated_string_list(
- None, t[0], None),
- t[1])
+ tests = (('a', ['a']),
+ ('a:b', ['a', 'b']),
+ (['a'], ['a']),
+ (['a', 'b:c'], ['a', 'b', 'c']),
+ )
+ for v, result in tests:
+ self.assertEqual(
+ frontend.validate_colon_separated_string_list(v), result)
def test_validate_comma_separated_list(self):
- tests = (
- ('a', ['a']),
- ('a,b', ['a', 'b']),
- (['a'], ['a']),
- (['a', 'b,c'], ['a', 'b', 'c']),
- )
- for t in tests:
- self.assertEqual(
- frontend.validate_comma_separated_list(None, t[0], None),
- t[1])
+ tests = (('a', ['a']),
+ ('a,b', ['a', 'b']),
+ (['a'], ['a']),
+ (['a', 'b,c'], ['a', 'b', 'c']),
+ )
+ for v, result in tests:
+ self.assertEqual(frontend.validate_comma_separated_list(v), result)
def test_validate_url_trailing_slash(self):
- tests = (
- ('', './'),
- (None, './'),
- ('http://example.org', 'http://example.org/'),
- ('http://example.org/', 'http://example.org/'),
- )
- for t in tests:
- self.assertEqual(
- frontend.validate_url_trailing_slash(None, t[0], None),
- t[1])
+ tests = (('', './'),
+ (None, './'),
+ ('http://example.org', 'http://example.org/'),
+ ('http://example.org/', 'http://example.org/'),
+ )
+ for v, result in tests:
+ self.assertEqual(frontend.validate_url_trailing_slash(v), result)
def test_validate_smartquotes_locales(self):
tests = (
@@ -345,10 +347,8 @@
([('sd', '«»°°'), 'ds:°°«»'], [('sd', '«»°°'), ('ds', '°°«»')]),
('frs:« : »:((:))', [('frs', ['« ', ' »', '((', '))'])]),
)
- for t in tests:
- self.assertEqual(
- frontend.validate_smartquotes_locales(None, t[0], None),
- t[1])
+ for v, result in tests:
+ self.assertEqual(frontend.validate_smartquotes_locales(v), result)
def test_set_conditions_deprecation_warning(self):
reporter = utils.Reporter('test', 1, 4)
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|