♻️ Vendor with_attrs_docs

This commit is contained in:
Gina Häußge committed 2022-10-27 14:21:22 +02:00
1 parent 7e495ed476
commit e74739b972
23 files changed
+90 -23

No files matched your search

+2 -1
View File
@@ -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
@@ -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
+2 -1
View File
@@ -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
+2 -1
View File
@@ -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):
+2 -1
View File
@@ -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):
+2 -1
View File
@@ -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):
+2 -1
View File
@@ -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
+2 -1
View File
@@ -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):
+2 -1
View File
@@ -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
+2 -1
View File
@@ -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
@@ -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):
+2 -1
View File
@@ -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
@@ -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
@@ -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
+2 -1
View File
@@ -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
+2 -1
View File
@@ -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):
+2 -1
View File
@@ -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
+2 -1
View File
@@ -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
+2 -1
View File
@@ -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
+2 -1
View File
@@ -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
@@ -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
+3 -2
View File
@@ -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
+45
View File
@@ -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)