From e74739b9725f63ab0dc4791eb9483618844e9b88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Thu, 27 Oct 2022 14:21:22 +0200 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Vendor=20with=5Fattrs=5Fdo?= =?UTF-8?q?cs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/octoprint/schema/config/__init__.py | 3 +- src/octoprint/schema/config/access_control.py | 3 +- src/octoprint/schema/config/api.py | 3 +- src/octoprint/schema/config/appearance.py | 3 +- src/octoprint/schema/config/controls.py | 3 +- src/octoprint/schema/config/devel.py | 3 +- src/octoprint/schema/config/estimation.py | 3 +- src/octoprint/schema/config/events.py | 3 +- src/octoprint/schema/config/feature.py | 3 +- src/octoprint/schema/config/folder.py | 3 +- src/octoprint/schema/config/gcode_analysis.py | 3 +- src/octoprint/schema/config/plugins.py | 3 +- .../schema/config/printer_parameters.py | 3 +- .../schema/config/printer_profiles.py | 3 +- src/octoprint/schema/config/scripts.py | 3 +- src/octoprint/schema/config/serial.py | 3 +- src/octoprint/schema/config/server.py | 3 +- src/octoprint/schema/config/slicing.py | 3 +- src/octoprint/schema/config/system.py | 3 +- src/octoprint/schema/config/temperature.py | 3 +- .../schema/config/terminalfilters.py | 3 +- src/octoprint/schema/config/webcam.py | 5 ++- src/octoprint/vendor/with_attrs_docs.py | 45 +++++++++++++++++++ 23 files changed, 90 insertions(+), 23 deletions(-) create mode 100644 src/octoprint/vendor/with_attrs_docs.py diff --git a/src/octoprint/schema/config/__init__.py b/src/octoprint/schema/config/__init__.py index 2bcc7ca2c..59b6582cb 100644 --- a/src/octoprint/schema/config/__init__.py +++ b/src/octoprint/schema/config/__init__.py @@ -4,7 +4,8 @@ __copyright__ = "Copyright (C) 2022 The OctoPrint Project - Released under terms from typing import List, Union from pydantic import BaseModel -from pydantic_settings import with_attrs_docs + +from octoprint.vendor.with_attrs_docs import with_attrs_docs from .access_control import AccessControlConfig from .api import ApiConfig diff --git a/src/octoprint/schema/config/access_control.py b/src/octoprint/schema/config/access_control.py index 5546bc020..f84aa5425 100644 --- a/src/octoprint/schema/config/access_control.py +++ b/src/octoprint/schema/config/access_control.py @@ -4,7 +4,8 @@ __copyright__ = "Copyright (C) 2022 The OctoPrint Project - Released under terms from typing import List, Optional from pydantic import BaseModel -from pydantic_settings import with_attrs_docs + +from octoprint.vendor.with_attrs_docs import with_attrs_docs @with_attrs_docs diff --git a/src/octoprint/schema/config/api.py b/src/octoprint/schema/config/api.py index cd0b1ae86..300b56e71 100644 --- a/src/octoprint/schema/config/api.py +++ b/src/octoprint/schema/config/api.py @@ -4,7 +4,8 @@ __copyright__ = "Copyright (C) 2022 The OctoPrint Project - Released under terms from typing import Dict from pydantic import BaseModel -from pydantic_settings import with_attrs_docs + +from octoprint.vendor.with_attrs_docs import with_attrs_docs @with_attrs_docs diff --git a/src/octoprint/schema/config/appearance.py b/src/octoprint/schema/config/appearance.py index 234052422..c136acd74 100644 --- a/src/octoprint/schema/config/appearance.py +++ b/src/octoprint/schema/config/appearance.py @@ -5,7 +5,8 @@ from enum import Enum from typing import List from pydantic import BaseModel -from pydantic_settings import with_attrs_docs + +from octoprint.vendor.with_attrs_docs import with_attrs_docs class ColorEnum(str, Enum): diff --git a/src/octoprint/schema/config/controls.py b/src/octoprint/schema/config/controls.py index 13dcf9649..39fbf486f 100644 --- a/src/octoprint/schema/config/controls.py +++ b/src/octoprint/schema/config/controls.py @@ -5,7 +5,8 @@ from enum import Enum from typing import List, Optional, Union from pydantic import BaseModel -from pydantic_settings import with_attrs_docs + +from octoprint.vendor.with_attrs_docs import with_attrs_docs class LayoutEnum(str, Enum): diff --git a/src/octoprint/schema/config/devel.py b/src/octoprint/schema/config/devel.py index 66f253f3a..9a6759d12 100644 --- a/src/octoprint/schema/config/devel.py +++ b/src/octoprint/schema/config/devel.py @@ -4,7 +4,8 @@ __copyright__ = "Copyright (C) 2022 The OctoPrint Project - Released under terms from enum import Enum from pydantic import BaseModel -from pydantic_settings import with_attrs_docs + +from octoprint.vendor.with_attrs_docs import with_attrs_docs class StylesheetEnum(str, Enum): diff --git a/src/octoprint/schema/config/estimation.py b/src/octoprint/schema/config/estimation.py index 147602215..9675b0522 100644 --- a/src/octoprint/schema/config/estimation.py +++ b/src/octoprint/schema/config/estimation.py @@ -2,7 +2,8 @@ __license__ = "GNU Affero General Public License http://www.gnu.org/licenses/agp __copyright__ = "Copyright (C) 2022 The OctoPrint Project - Released under terms of the AGPLv3 License" from pydantic import BaseModel -from pydantic_settings import with_attrs_docs + +from octoprint.vendor.with_attrs_docs import with_attrs_docs @with_attrs_docs diff --git a/src/octoprint/schema/config/events.py b/src/octoprint/schema/config/events.py index 54455ced1..a27e5aeea 100644 --- a/src/octoprint/schema/config/events.py +++ b/src/octoprint/schema/config/events.py @@ -5,7 +5,8 @@ from enum import Enum from typing import List from pydantic import BaseModel -from pydantic_settings import with_attrs_docs + +from octoprint.vendor.with_attrs_docs import with_attrs_docs class SubscriptionTypeEnum(str, Enum): diff --git a/src/octoprint/schema/config/feature.py b/src/octoprint/schema/config/feature.py index 88f07111d..154a59405 100644 --- a/src/octoprint/schema/config/feature.py +++ b/src/octoprint/schema/config/feature.py @@ -4,7 +4,8 @@ __copyright__ = "Copyright (C) 2022 The OctoPrint Project - Released under terms from typing import List from pydantic import BaseModel -from pydantic_settings import with_attrs_docs + +from octoprint.vendor.with_attrs_docs import with_attrs_docs @with_attrs_docs diff --git a/src/octoprint/schema/config/folder.py b/src/octoprint/schema/config/folder.py index fdaa22a2d..fbfcbeef5 100644 --- a/src/octoprint/schema/config/folder.py +++ b/src/octoprint/schema/config/folder.py @@ -4,7 +4,8 @@ __copyright__ = "Copyright (C) 2022 The OctoPrint Project - Released under terms from typing import Optional from pydantic import BaseModel -from pydantic_settings import with_attrs_docs + +from octoprint.vendor.with_attrs_docs import with_attrs_docs @with_attrs_docs diff --git a/src/octoprint/schema/config/gcode_analysis.py b/src/octoprint/schema/config/gcode_analysis.py index 2d66865f4..9141e3d5e 100644 --- a/src/octoprint/schema/config/gcode_analysis.py +++ b/src/octoprint/schema/config/gcode_analysis.py @@ -4,7 +4,8 @@ __copyright__ = "Copyright (C) 2022 The OctoPrint Project - Released under terms from enum import Enum from pydantic import BaseModel -from pydantic_settings import with_attrs_docs + +from octoprint.vendor.with_attrs_docs import with_attrs_docs class RunAtEnum(str, Enum): diff --git a/src/octoprint/schema/config/plugins.py b/src/octoprint/schema/config/plugins.py index f9abd68c0..d689ae1f9 100644 --- a/src/octoprint/schema/config/plugins.py +++ b/src/octoprint/schema/config/plugins.py @@ -4,7 +4,8 @@ __copyright__ = "Copyright (C) 2022 The OctoPrint Project - Released under terms from typing import Dict, List from pydantic import BaseModel, Field -from pydantic_settings import with_attrs_docs + +from octoprint.vendor.with_attrs_docs import with_attrs_docs @with_attrs_docs diff --git a/src/octoprint/schema/config/printer_parameters.py b/src/octoprint/schema/config/printer_parameters.py index 9219b63d6..387a53681 100644 --- a/src/octoprint/schema/config/printer_parameters.py +++ b/src/octoprint/schema/config/printer_parameters.py @@ -4,7 +4,8 @@ __copyright__ = "Copyright (C) 2022 The OctoPrint Project - Released under terms from typing import List from pydantic import BaseModel -from pydantic_settings import with_attrs_docs + +from octoprint.vendor.with_attrs_docs import with_attrs_docs @with_attrs_docs diff --git a/src/octoprint/schema/config/printer_profiles.py b/src/octoprint/schema/config/printer_profiles.py index b1aa439d5..b4f6852d4 100644 --- a/src/octoprint/schema/config/printer_profiles.py +++ b/src/octoprint/schema/config/printer_profiles.py @@ -4,7 +4,8 @@ __copyright__ = "Copyright (C) 2022 The OctoPrint Project - Released under terms from typing import Optional from pydantic import BaseModel -from pydantic_settings import with_attrs_docs + +from octoprint.vendor.with_attrs_docs import with_attrs_docs @with_attrs_docs diff --git a/src/octoprint/schema/config/scripts.py b/src/octoprint/schema/config/scripts.py index fbec21646..16e8ee1e8 100644 --- a/src/octoprint/schema/config/scripts.py +++ b/src/octoprint/schema/config/scripts.py @@ -4,7 +4,8 @@ __copyright__ = "Copyright (C) 2022 The OctoPrint Project - Released under terms from typing import Dict, Optional from pydantic import BaseModel -from pydantic_settings import with_attrs_docs + +from octoprint.vendor.with_attrs_docs import with_attrs_docs @with_attrs_docs diff --git a/src/octoprint/schema/config/serial.py b/src/octoprint/schema/config/serial.py index 163b0be53..53a8b99a9 100644 --- a/src/octoprint/schema/config/serial.py +++ b/src/octoprint/schema/config/serial.py @@ -5,7 +5,8 @@ from enum import Enum from typing import List, Optional from pydantic import BaseModel -from pydantic_settings import with_attrs_docs + +from octoprint.vendor.with_attrs_docs import with_attrs_docs class AlwaysDetectNeverEnum(str, Enum): diff --git a/src/octoprint/schema/config/server.py b/src/octoprint/schema/config/server.py index 4ac86204b..c85e113cf 100644 --- a/src/octoprint/schema/config/server.py +++ b/src/octoprint/schema/config/server.py @@ -5,7 +5,8 @@ from enum import Enum from typing import Dict, List, Optional from pydantic import BaseModel -from pydantic_settings import with_attrs_docs + +from octoprint.vendor.with_attrs_docs import with_attrs_docs CONST_15MIN = 15 * 60 CONST_1GB = 1024 * 1024 * 1024 diff --git a/src/octoprint/schema/config/slicing.py b/src/octoprint/schema/config/slicing.py index 446a41f9f..1fcd3b0ab 100644 --- a/src/octoprint/schema/config/slicing.py +++ b/src/octoprint/schema/config/slicing.py @@ -4,7 +4,8 @@ __copyright__ = "Copyright (C) 2022 The OctoPrint Project - Released under terms from typing import Dict, Optional from pydantic import BaseModel -from pydantic_settings import with_attrs_docs + +from octoprint.vendor.with_attrs_docs import with_attrs_docs @with_attrs_docs diff --git a/src/octoprint/schema/config/system.py b/src/octoprint/schema/config/system.py index 016ea11e4..41794a821 100644 --- a/src/octoprint/schema/config/system.py +++ b/src/octoprint/schema/config/system.py @@ -4,7 +4,8 @@ __copyright__ = "Copyright (C) 2022 The OctoPrint Project - Released under terms from typing import List, Optional from pydantic import BaseModel, Field -from pydantic_settings import with_attrs_docs + +from octoprint.vendor.with_attrs_docs import with_attrs_docs @with_attrs_docs diff --git a/src/octoprint/schema/config/temperature.py b/src/octoprint/schema/config/temperature.py index 68bc6353d..f09cc2987 100644 --- a/src/octoprint/schema/config/temperature.py +++ b/src/octoprint/schema/config/temperature.py @@ -4,7 +4,8 @@ __copyright__ = "Copyright (C) 2022 The OctoPrint Project - Released under terms from typing import List from pydantic import BaseModel -from pydantic_settings import with_attrs_docs + +from octoprint.vendor.with_attrs_docs import with_attrs_docs @with_attrs_docs diff --git a/src/octoprint/schema/config/terminalfilters.py b/src/octoprint/schema/config/terminalfilters.py index 5882b595e..e2cc38743 100644 --- a/src/octoprint/schema/config/terminalfilters.py +++ b/src/octoprint/schema/config/terminalfilters.py @@ -2,7 +2,8 @@ __license__ = "GNU Affero General Public License http://www.gnu.org/licenses/agp __copyright__ = "Copyright (C) 2022 The OctoPrint Project - Released under terms of the AGPLv3 License" from pydantic import BaseModel -from pydantic_settings import with_attrs_docs + +from octoprint.vendor.with_attrs_docs import with_attrs_docs @with_attrs_docs diff --git a/src/octoprint/schema/config/webcam.py b/src/octoprint/schema/config/webcam.py index fdf394f60..c6bfb1826 100644 --- a/src/octoprint/schema/config/webcam.py +++ b/src/octoprint/schema/config/webcam.py @@ -5,7 +5,8 @@ from enum import Enum from typing import List, Optional from pydantic import BaseModel -from pydantic_settings import with_attrs_docs + +from octoprint.vendor.with_attrs_docs import with_attrs_docs class RatioEnum(str, Enum): @@ -56,7 +57,7 @@ class WebcamConfig(BaseModel): stream: Optional[str] = None - streamRatio: RatioEnum = "16:9" + streamRatio: RatioEnum = RatioEnum.sixteen_nine streamTimeout: int = 5 diff --git a/src/octoprint/vendor/with_attrs_docs.py b/src/octoprint/vendor/with_attrs_docs.py new file mode 100644 index 000000000..7a7e7d48c --- /dev/null +++ b/src/octoprint/vendor/with_attrs_docs.py @@ -0,0 +1,45 @@ +# Based on pydantic-settings version 0.2.5 by Daniel Daniels, licensed under MIT +# +# https://github.com/danields761/pydantic-settings + +from typing import Type + +from class_doc import extract_docs_from_cls_obj +from pydantic import BaseModel + + +def apply_attributes_docs( + model: Type[BaseModel], override_existing: bool = True +) -> None: + """ + Apply model attributes documentation in-place. Resulted docs are placed + inside :code:`field.schema.description` for *pydantic* model field. + :param model: any pydantic model + :param override_existing: override existing descriptions + """ + docs = extract_docs_from_cls_obj(model) + + for field in model.__fields__.values(): + if field.field_info.description and not override_existing: + continue + + try: + field.field_info.description = '\n'.join(docs[field.name]) + except KeyError: + pass + + +def with_attrs_docs( + model_cls: Type[BaseModel] +) -> Type[BaseModel]: + """ + Applies :py:func:`.apply_attributes_docs`. + """ + + def decorator(maybe_model_cls: Type[BaseModel]) -> Type[BaseModel]: + apply_attributes_docs( + maybe_model_cls + ) + return maybe_model_cls + + return decorator(model_cls)