Updated most python dependencies

Primarily FastAPI and Pydantic to v2. Also ruff to latest.

Updated some code to support Pydantic v2
This commit is contained in:
Donkie
2024-05-23 19:42:27 +02:00
parent 306dbe40af
commit 535fa40ad2
29 changed files with 839 additions and 531 deletions

View File

@@ -2,10 +2,9 @@
from datetime import datetime, timezone
from enum import Enum
from typing import Literal, Optional
from typing import Annotated, Literal, Optional
from pydantic import BaseModel as PydanticBaseModel
from pydantic import Field
from pydantic import BaseModel, Field, PlainSerializer
from spoolman.database import models
from spoolman.math import length_from_weight
@@ -19,13 +18,7 @@ def datetime_to_str(dt: datetime) -> str:
return dt.isoformat().replace("+00:00", "Z")
class BaseModel(PydanticBaseModel):
class Config:
"""Pydantic configuration."""
json_encoders = { # noqa: RUF012
datetime: datetime_to_str,
}
SpoolmanDateTime = Annotated[datetime, PlainSerializer(datetime_to_str)]
class Message(BaseModel):
@@ -57,16 +50,27 @@ class SettingKV(BaseModel):
class Vendor(BaseModel):
id: int = Field(description="Unique internal ID of this vendor.")
registered: datetime = Field(description="When the vendor was registered in the database. UTC Timezone.")
name: str = Field(max_length=64, description="Vendor name.", example="Polymaker")
comment: Optional[str] = Field(max_length=1024, description="Free text comment about this vendor.", example="")
empty_spool_weight: Optional[float] = Field(gt=0, description="The empty spool weight, in grams.", example=140)
registered: SpoolmanDateTime = Field(description="When the vendor was registered in the database. UTC Timezone.")
name: str = Field(max_length=64, description="Vendor name.", examples=["Polymaker"])
comment: Optional[str] = Field(
None,
max_length=1024,
description="Free text comment about this vendor.",
examples=[""],
)
empty_spool_weight: Optional[float] = Field(
None,
gt=0,
description="The empty spool weight, in grams.",
examples=[140],
)
external_id: Optional[str] = Field(
None,
max_length=256,
description=(
"Set if this vendor comes from an external database. This contains the ID in the external database."
),
example="eSun",
examples=["eSun"],
)
extra: dict[str, str] = Field(
description=(
@@ -91,66 +95,76 @@ class Vendor(BaseModel):
class Filament(BaseModel):
id: int = Field(description="Unique internal ID of this filament type.")
registered: datetime = Field(description="When the filament was registered in the database. UTC Timezone.")
registered: SpoolmanDateTime = Field(description="When the filament was registered in the database. UTC Timezone.")
name: Optional[str] = Field(
None,
max_length=64,
description=(
"Filament name, to distinguish this filament type among others from the same vendor."
"Should contain its color for example."
),
example="PolyTerra™ Charcoal Black",
examples=["PolyTerra™ Charcoal Black"],
)
vendor: Optional[Vendor] = Field(description="The vendor of this filament type.")
vendor: Optional[Vendor] = Field(None, description="The vendor of this filament type.")
material: Optional[str] = Field(
None,
max_length=64,
description="The material of this filament, e.g. PLA.",
example="PLA",
examples=["PLA"],
)
price: Optional[float] = Field(
None,
ge=0,
description="The price of this filament in the system configured currency.",
example=20.0,
examples=[20.0],
)
density: float = Field(gt=0, description="The density of this filament in g/cm3.", example=1.24)
diameter: float = Field(gt=0, description="The diameter of this filament in mm.", example=1.75)
density: float = Field(gt=0, description="The density of this filament in g/cm3.", examples=[1.24])
diameter: float = Field(gt=0, description="The diameter of this filament in mm.", examples=[1.75])
weight: Optional[float] = Field(
None,
gt=0,
description="The weight of the filament in a full spool, in grams.",
example=1000,
examples=[1000],
)
spool_weight: Optional[float] = Field(gt=0, description="The empty spool weight, in grams.", example=140)
spool_weight: Optional[float] = Field(None, gt=0, description="The empty spool weight, in grams.", examples=[140])
article_number: Optional[str] = Field(
None,
max_length=64,
description="Vendor article number, e.g. EAN, QR code, etc.",
example="PM70820",
examples=["PM70820"],
)
comment: Optional[str] = Field(
None,
max_length=1024,
description="Free text comment about this filament type.",
example="",
examples=[""],
)
settings_extruder_temp: Optional[int] = Field(
None,
ge=0,
description="Overridden extruder temperature, in °C.",
example=210,
examples=[210],
)
settings_bed_temp: Optional[int] = Field(
None,
ge=0,
description="Overridden bed temperature, in °C.",
example=60,
examples=[60],
)
color_hex: Optional[str] = Field(
None,
min_length=6,
max_length=8,
description="Hexadecimal color code of the filament, e.g. FF0000 for red. Supports alpha channel at the end.",
example="FF0000",
examples=["FF0000"],
)
external_id: Optional[str] = Field(
None,
max_length=256,
description=(
"Set if this filament comes from an external database. This contains the ID in the external database."
),
example="polymaker_pla_polysonicblack_1000_175",
examples=["polymaker_pla_polysonicblack_1000_175"],
)
extra: dict[str, str] = Field(
description=(
@@ -185,14 +199,21 @@ class Filament(BaseModel):
class Spool(BaseModel):
id: int = Field(description="Unique internal ID of this spool of filament.")
registered: datetime = Field(description="When the spool was registered in the database. UTC Timezone.")
first_used: Optional[datetime] = Field(description="First logged occurence of spool usage. UTC Timezone.")
last_used: Optional[datetime] = Field(description="Last logged occurence of spool usage. UTC Timezone.")
registered: SpoolmanDateTime = Field(description="When the spool was registered in the database. UTC Timezone.")
first_used: Optional[SpoolmanDateTime] = Field(
None,
description="First logged occurence of spool usage. UTC Timezone.",
)
last_used: Optional[SpoolmanDateTime] = Field(
None,
description="Last logged occurence of spool usage. UTC Timezone.",
)
filament: Filament = Field(description="The filament type of this spool.")
price: Optional[float] = Field(
None,
ge=0,
description="The price of this spool in the system configured currency.",
example=20.0,
examples=[20.0],
)
remaining_weight: Optional[float] = Field(
default=None,
@@ -201,21 +222,25 @@ class Spool(BaseModel):
"Estimated remaining weight of filament on the spool in grams. "
"Only set if the filament type has a weight set."
),
example=500.6,
examples=[500.6],
)
initial_weight: Optional[float] = Field(
default=None,
ge=0,
description=("The initial weight, in grams, of the filament on the spool (net weight)."),
example=1246,
examples=[1246],
)
spool_weight: Optional[float] = Field(
default=None,
ge=0,
description=("Weight of an empty spool (tare weight)."),
example=246,
examples=[246],
)
used_weight: float = Field(
ge=0,
description="Consumed weight of filament from the spool in grams.",
examples=[500.3],
)
used_weight: float = Field(ge=0, description="Consumed weight of filament from the spool in grams.", example=500.3)
remaining_length: Optional[float] = Field(
default=None,
ge=0,
@@ -223,23 +248,30 @@ class Spool(BaseModel):
"Estimated remaining length of filament on the spool in millimeters."
" Only set if the filament type has a weight set."
),
example=5612.4,
examples=[5612.4],
)
used_length: float = Field(
ge=0,
description="Consumed length of filament from the spool in millimeters.",
example=50.7,
examples=[50.7],
)
location: Optional[str] = Field(
None,
max_length=64,
description="Where this spool can be found.",
examples=["Shelf A"],
)
location: Optional[str] = Field(max_length=64, description="Where this spool can be found.", example="Shelf A")
lot_nr: Optional[str] = Field(
None,
max_length=64,
description="Vendor manufacturing lot/batch number of the spool.",
example="52342",
examples=["52342"],
)
comment: Optional[str] = Field(
None,
max_length=1024,
description="Free text comment about this specific spool.",
example="",
examples=[""],
)
archived: bool = Field(description="Whether this spool is archived and should not be used anymore.")
extra: dict[str, str] = Field(
@@ -300,26 +332,26 @@ class Spool(BaseModel):
class Info(BaseModel):
version: str = Field(example="0.7.0")
debug_mode: bool = Field(example=False)
automatic_backups: bool = Field(example=True)
data_dir: str = Field(example="/home/app/.local/share/spoolman")
logs_dir: str = Field(example="/home/app/.local/share/spoolman")
backups_dir: str = Field(example="/home/app/.local/share/spoolman/backups")
db_type: str = Field(example="sqlite")
git_commit: Optional[str] = Field(example="a1b2c3d")
build_date: Optional[datetime] = Field(example="2021-01-01T00:00:00Z")
version: str = Field(examples=["0.7.0"])
debug_mode: bool = Field(examples=[False])
automatic_backups: bool = Field(examples=[True])
data_dir: str = Field(examples=["/home/app/.local/share/spoolman"])
logs_dir: str = Field(examples=["/home/app/.local/share/spoolman"])
backups_dir: str = Field(examples=["/home/app/.local/share/spoolman/backups"])
db_type: str = Field(examples=["sqlite"])
git_commit: Optional[str] = Field(None, examples=["a1b2c3d"])
build_date: Optional[SpoolmanDateTime] = Field(None, examples=["2021-01-01T00:00:00Z"])
class HealthCheck(BaseModel):
status: str = Field(example="healthy")
status: str = Field(examples=["healthy"])
class BackupResponse(BaseModel):
path: str = Field(
default=None,
description="Path to the created backup file.",
example="/home/app/.local/share/spoolman/backups/spoolman.db",
examples=["/home/app/.local/share/spoolman/backups/spoolman.db"],
)
@@ -336,7 +368,7 @@ class Event(BaseModel):
type: EventType = Field(description="Event type.")
resource: str = Field(description="Resource type.")
date: datetime = Field(description="When the event occured. UTC Timezone.")
date: SpoolmanDateTime = Field(description="When the event occured. UTC Timezone.")
payload: BaseModel