mirror of
https://github.com/freqtrade/freqtrade.git
synced 2026-01-19 21:40:24 +00:00
chore: improve create_command_partials runtime by not calling subprocess several times
This commit is contained in:
@@ -1,5 +1,7 @@
|
|||||||
|
import os
|
||||||
import subprocess # noqa: S404, RUF100
|
import subprocess # noqa: S404, RUF100
|
||||||
import sys
|
import sys
|
||||||
|
from io import StringIO
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
|
|
||||||
@@ -8,7 +10,20 @@ def _write_partial_file(filename: str, content: str):
|
|||||||
f.write(f"``` output\n{content}\n```\n")
|
f.write(f"``` output\n{content}\n```\n")
|
||||||
|
|
||||||
|
|
||||||
|
def _get_help_output(parser) -> str:
|
||||||
|
"""Capture the help output from a parser."""
|
||||||
|
output = StringIO()
|
||||||
|
parser.print_help(file=output)
|
||||||
|
return output.getvalue()
|
||||||
|
|
||||||
|
|
||||||
def extract_command_partials():
|
def extract_command_partials():
|
||||||
|
# Set terminal width to 80 columns for consistent output formatting
|
||||||
|
os.environ["COLUMNS"] = "80"
|
||||||
|
|
||||||
|
# Import Arguments here to avoid circular imports and ensure COLUMNS is set
|
||||||
|
from freqtrade.commands.arguments import Arguments
|
||||||
|
|
||||||
subcommands = [
|
subcommands = [
|
||||||
"trade",
|
"trade",
|
||||||
"create-userdir",
|
"create-userdir",
|
||||||
@@ -46,16 +61,35 @@ def extract_command_partials():
|
|||||||
"recursive-analysis",
|
"recursive-analysis",
|
||||||
]
|
]
|
||||||
|
|
||||||
result = subprocess.run(["freqtrade", "--help"], capture_output=True, text=True)
|
# Build the Arguments class to get the parser with all subcommands
|
||||||
|
args = Arguments(None)
|
||||||
|
args._build_subcommands()
|
||||||
|
|
||||||
_write_partial_file("docs/commands/main.md", result.stdout)
|
# Get main help output
|
||||||
|
main_help = _get_help_output(args.parser)
|
||||||
|
_write_partial_file("docs/commands/main.md", main_help)
|
||||||
|
|
||||||
|
# Get subparsers from the main parser
|
||||||
|
# The subparsers are stored in _subparsers._group_actions[0].choices
|
||||||
|
subparsers_action = None
|
||||||
|
for action in args.parser._subparsers._group_actions:
|
||||||
|
if hasattr(action, "choices"):
|
||||||
|
subparsers_action = action
|
||||||
|
break
|
||||||
|
|
||||||
|
if subparsers_action is None:
|
||||||
|
raise RuntimeError("Could not find subparsers in the main parser")
|
||||||
|
|
||||||
for command in subcommands:
|
for command in subcommands:
|
||||||
print(f"Running for {command}")
|
print(f"Running for {command}")
|
||||||
result = subprocess.run(["freqtrade", command, "--help"], capture_output=True, text=True)
|
if command in subparsers_action.choices:
|
||||||
|
subparser = subparsers_action.choices[command]
|
||||||
_write_partial_file(f"docs/commands/{command}.md", result.stdout)
|
help_output = _get_help_output(subparser)
|
||||||
|
_write_partial_file(f"docs/commands/{command}.md", help_output)
|
||||||
|
else:
|
||||||
|
print(f" Warning: subcommand '{command}' not found in parser")
|
||||||
|
|
||||||
|
# freqtrade-client still uses subprocess as requested
|
||||||
print("Running for freqtrade-client")
|
print("Running for freqtrade-client")
|
||||||
result_client = subprocess.run(["freqtrade-client", "--show"], capture_output=True, text=True)
|
result_client = subprocess.run(["freqtrade-client", "--show"], capture_output=True, text=True)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user