From b839e159a899047f2a00011232a580fd66ef6081 Mon Sep 17 00:00:00 2001 From: Matthias Date: Fri, 4 Apr 2025 07:19:47 +0200 Subject: [PATCH 1/5] chore: Support config schema extract without installation --- build_helpers/extract_config_json_schema.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/build_helpers/extract_config_json_schema.py b/build_helpers/extract_config_json_schema.py index 44ecc0f91..31c8e76df 100644 --- a/build_helpers/extract_config_json_schema.py +++ b/build_helpers/extract_config_json_schema.py @@ -1,13 +1,25 @@ """Script to extract the configuration json schema from config_schema.py file.""" +import sys from pathlib import Path import rapidjson -from freqtrade.configuration.config_schema import CONF_SCHEMA - def extract_config_json_schema(): + try: + # Try to import from the installed package + from freqtrade.configuration.config_schema import CONF_SCHEMA + except ImportError: + # If freqtrade is not installed, add the parent directory to sys.path + # to import directly from the source + script_dir = Path(__file__).parent + freqtrade_dir = script_dir.parent + sys.path.insert(0, str(freqtrade_dir)) + + # Now try to import from the source + from freqtrade.configuration.config_schema import CONF_SCHEMA + schema_filename = Path(__file__).parent / "schema.json" with schema_filename.open("w") as f: rapidjson.dump(CONF_SCHEMA, f, indent=2) From 21a47bb1ac6afd6365b28ae51192cab5bbcc2d5c Mon Sep 17 00:00:00 2001 From: Matthias Date: Fri, 4 Apr 2025 07:20:04 +0200 Subject: [PATCH 2/5] chore: add config-schema extract to pre-commit --- .pre-commit-config.yaml | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 200f12818..de9880155 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,6 +1,16 @@ # See https://pre-commit.com for more information # See https://pre-commit.com/hooks.html for more hooks repos: + + - repo: local + hooks: + - id: Extract config json schema + name: extract-config-json-schema + entry: "python build_helpers/extract_config_json_schema.py" + language: python + pass_filenames: false + additional_dependencies: ["python-rapidjson", "jsonschema"] + - repo: https://github.com/pycqa/flake8 rev: "7.2.0" hooks: From eac440649ba1cbae80be4e3f9a822e070858c468 Mon Sep 17 00:00:00 2001 From: Matthias Date: Fri, 4 Apr 2025 07:23:45 +0200 Subject: [PATCH 3/5] refactor: move config_schema to it's own package This is to avoid import conflicts and allow running the schema extraction without a full freqtrade installation. --- build_helpers/extract_config_json_schema.py | 4 ++-- freqtrade/config_schema/__init__.py | 4 ++++ freqtrade/{configuration => config_schema}/config_schema.py | 0 freqtrade/configuration/config_validation.py | 2 +- 4 files changed, 7 insertions(+), 3 deletions(-) create mode 100644 freqtrade/config_schema/__init__.py rename freqtrade/{configuration => config_schema}/config_schema.py (100%) diff --git a/build_helpers/extract_config_json_schema.py b/build_helpers/extract_config_json_schema.py index 31c8e76df..ff568d5b6 100644 --- a/build_helpers/extract_config_json_schema.py +++ b/build_helpers/extract_config_json_schema.py @@ -9,7 +9,7 @@ import rapidjson def extract_config_json_schema(): try: # Try to import from the installed package - from freqtrade.configuration.config_schema import CONF_SCHEMA + from freqtrade.config_schema import CONF_SCHEMA except ImportError: # If freqtrade is not installed, add the parent directory to sys.path # to import directly from the source @@ -18,7 +18,7 @@ def extract_config_json_schema(): sys.path.insert(0, str(freqtrade_dir)) # Now try to import from the source - from freqtrade.configuration.config_schema import CONF_SCHEMA + from freqtrade.config_schema import CONF_SCHEMA schema_filename = Path(__file__).parent / "schema.json" with schema_filename.open("w") as f: diff --git a/freqtrade/config_schema/__init__.py b/freqtrade/config_schema/__init__.py new file mode 100644 index 000000000..0a5c19c1f --- /dev/null +++ b/freqtrade/config_schema/__init__.py @@ -0,0 +1,4 @@ +from freqtrade.config_schema.config_schema import CONF_SCHEMA + + +__all__ = ["CONF_SCHEMA"] diff --git a/freqtrade/configuration/config_schema.py b/freqtrade/config_schema/config_schema.py similarity index 100% rename from freqtrade/configuration/config_schema.py rename to freqtrade/config_schema/config_schema.py diff --git a/freqtrade/configuration/config_validation.py b/freqtrade/configuration/config_validation.py index 8640542a1..e481f663e 100644 --- a/freqtrade/configuration/config_validation.py +++ b/freqtrade/configuration/config_validation.py @@ -6,7 +6,7 @@ from typing import Any from jsonschema import Draft4Validator, validators from jsonschema.exceptions import ValidationError, best_match -from freqtrade.configuration.config_schema import ( +from freqtrade.config_schema.config_schema import ( CONF_SCHEMA, SCHEMA_BACKTEST_REQUIRED, SCHEMA_BACKTEST_REQUIRED_FINAL, From 19a997a2db4cc38d60d1439c67225a5a0786817c Mon Sep 17 00:00:00 2001 From: Matthias Date: Fri, 4 Apr 2025 07:25:37 +0200 Subject: [PATCH 4/5] chore: add comment for custom pre-commit hook --- .pre-commit-config.yaml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index de9880155..3dc08ee47 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -3,6 +3,8 @@ repos: - repo: local + # Keep json schema in sync with the config schema + # This will write the files - and fail pre-commit if a file has been changed. hooks: - id: Extract config json schema name: extract-config-json-schema From e5389be209d04e012cac5692e61ed51383640656 Mon Sep 17 00:00:00 2001 From: Matthias Date: Fri, 4 Apr 2025 09:32:16 +0200 Subject: [PATCH 5/5] chore: import sys only when necessary --- build_helpers/extract_config_json_schema.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/build_helpers/extract_config_json_schema.py b/build_helpers/extract_config_json_schema.py index ff568d5b6..15bfd7720 100644 --- a/build_helpers/extract_config_json_schema.py +++ b/build_helpers/extract_config_json_schema.py @@ -1,6 +1,5 @@ """Script to extract the configuration json schema from config_schema.py file.""" -import sys from pathlib import Path import rapidjson @@ -13,6 +12,8 @@ def extract_config_json_schema(): except ImportError: # If freqtrade is not installed, add the parent directory to sys.path # to import directly from the source + import sys + script_dir = Path(__file__).parent freqtrade_dir = script_dir.parent sys.path.insert(0, str(freqtrade_dir))