Improve configs - LoadConfig (#16422)
Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com>
This commit is contained in:
+21
-7
@@ -2,7 +2,6 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import asyncio
|
||||
import concurrent
|
||||
import contextlib
|
||||
@@ -25,6 +24,7 @@ import socket
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import textwrap
|
||||
import threading
|
||||
import time
|
||||
import traceback
|
||||
@@ -32,6 +32,8 @@ import types
|
||||
import uuid
|
||||
import warnings
|
||||
import weakref
|
||||
from argparse import (Action, ArgumentDefaultsHelpFormatter, ArgumentParser,
|
||||
ArgumentTypeError)
|
||||
from asyncio import FIRST_COMPLETED, AbstractEventLoop, Task
|
||||
from collections import UserDict, defaultdict
|
||||
from collections.abc import (AsyncGenerator, Awaitable, Generator, Hashable,
|
||||
@@ -1209,7 +1211,7 @@ def run_once(f: Callable[P, None]) -> Callable[P, None]:
|
||||
return wrapper
|
||||
|
||||
|
||||
class StoreBoolean(argparse.Action):
|
||||
class StoreBoolean(Action):
|
||||
|
||||
def __call__(self, parser, namespace, values, option_string=None):
|
||||
if values.lower() == "true":
|
||||
@@ -1221,15 +1223,28 @@ class StoreBoolean(argparse.Action):
|
||||
"Expected 'true' or 'false'.")
|
||||
|
||||
|
||||
class SortedHelpFormatter(argparse.ArgumentDefaultsHelpFormatter):
|
||||
class SortedHelpFormatter(ArgumentDefaultsHelpFormatter):
|
||||
"""SortedHelpFormatter that sorts arguments by their option strings."""
|
||||
|
||||
def _split_lines(self, text, width):
|
||||
"""
|
||||
1. Sentences split across lines have their single newlines removed.
|
||||
2. Paragraphs and explicit newlines are split into separate lines.
|
||||
3. Each line is wrapped to the specified width (width of terminal).
|
||||
"""
|
||||
# The patterns also include whitespace after the newline
|
||||
single_newline = re.compile("(?<!\n)\n(?!\n)\s*")
|
||||
multiple_newlines = re.compile("\n{2,}\s*")
|
||||
text = single_newline.sub(' ', text)
|
||||
lines = re.split(multiple_newlines, text)
|
||||
return sum([textwrap.wrap(line, width) for line in lines], [])
|
||||
|
||||
def add_arguments(self, actions):
|
||||
actions = sorted(actions, key=lambda x: x.option_strings)
|
||||
super().add_arguments(actions)
|
||||
|
||||
|
||||
class FlexibleArgumentParser(argparse.ArgumentParser):
|
||||
class FlexibleArgumentParser(ArgumentParser):
|
||||
"""ArgumentParser that allows both underscore and dash in names."""
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
@@ -1280,11 +1295,10 @@ class FlexibleArgumentParser(argparse.ArgumentParser):
|
||||
value = int(value)
|
||||
except ValueError:
|
||||
msg = "Port must be an integer"
|
||||
raise argparse.ArgumentTypeError(msg) from None
|
||||
raise ArgumentTypeError(msg) from None
|
||||
|
||||
if not (1024 <= value <= 65535):
|
||||
raise argparse.ArgumentTypeError(
|
||||
"Port must be between 1024 and 65535")
|
||||
raise ArgumentTypeError("Port must be between 1024 and 65535")
|
||||
|
||||
return value
|
||||
|
||||
|
||||
Reference in New Issue
Block a user