BrickTracker/bricktracker/minifigure.py

102 lines
3.3 KiB
Python
Raw Normal View History

2025-01-27 18:39:35 +01:00
import logging
import traceback
from typing import Self, TYPE_CHECKING
2025-01-17 11:03:00 +01:00
from .exceptions import ErrorException, NotFoundException
from .part_list import BrickPartList
2025-01-27 18:39:35 +01:00
from .rebrickable_parts import RebrickableParts
from .rebrickable_minifigure import RebrickableMinifigure
2025-01-17 11:03:00 +01:00
if TYPE_CHECKING:
from .set import BrickSet
2025-01-27 18:39:35 +01:00
from .socket import BrickSocket
2025-01-17 11:03:00 +01:00
2025-01-27 18:39:35 +01:00
logger = logging.getLogger(__name__)
2025-01-17 11:03:00 +01:00
2025-01-27 18:39:35 +01:00
# Lego minifigure
class BrickMinifigure(RebrickableMinifigure):
2025-01-17 11:03:00 +01:00
# Queries
insert_query: str = 'minifigure/insert'
generic_query: str = 'minifigure/select/generic'
select_query: str = 'minifigure/select/specific'
2025-01-27 18:39:35 +01:00
def download(self, socket: 'BrickSocket'):
if self.brickset is None:
raise ErrorException('Importing a minifigure from Rebrickable outside of a set is not supported') # noqa: E501
2025-01-17 11:03:00 +01:00
2025-01-27 18:39:35 +01:00
try:
# Insert into the database
socket.auto_progress(
message='Set {set}: inserting minifigure {figure} into database'.format( # noqa: E501
set=self.brickset.fields.set,
figure=self.fields.figure
)
)
2025-01-17 11:03:00 +01:00
2025-01-27 18:39:35 +01:00
# Insert into database
self.insert(commit=False)
2025-01-17 11:03:00 +01:00
2025-01-27 18:39:35 +01:00
# Insert the rebrickable set into database
self.insert_rebrickable()
2025-01-17 11:03:00 +01:00
2025-01-27 18:39:35 +01:00
# Load the inventory
RebrickableParts(
socket,
self.brickset,
minifigure=self,
).download()
except Exception as e:
socket.fail(
message='Error while importing minifigure {figure} from {set}: {error}'.format( # noqa: E501
figure=self.fields.figure,
set=self.brickset.fields.set,
error=e,
)
)
logger.debug(traceback.format_exc())
2025-01-17 11:03:00 +01:00
# Parts
def generic_parts(self, /) -> BrickPartList:
return BrickPartList().from_minifigure(self)
# Parts
def parts(self, /) -> BrickPartList:
if self.brickset is None:
2025-01-27 18:39:35 +01:00
raise ErrorException('Part list for minifigure {figure} requires a brickset'.format( # noqa: E501
figure=self.fields.figure,
2025-01-17 11:03:00 +01:00
))
return BrickPartList().load(self.brickset, minifigure=self)
# Select a generic minifigure
2025-01-27 18:39:35 +01:00
def select_generic(self, figure: str, /) -> Self:
2025-01-17 11:03:00 +01:00
# Save the parameters to the fields
2025-01-27 18:39:35 +01:00
self.fields.figure = figure
2025-01-17 11:03:00 +01:00
if not self.select(override_query=self.generic_query):
2025-01-17 11:03:00 +01:00
raise NotFoundException(
2025-01-27 18:39:35 +01:00
'Minifigure with figure {figure} was not found in the database'.format( # noqa: E501
figure=self.fields.figure,
2025-01-17 11:03:00 +01:00
),
)
return self
2025-01-27 18:39:35 +01:00
# Select a specific minifigure (with a set and a figure)
def select_specific(self, brickset: 'BrickSet', figure: str, /) -> Self:
2025-01-17 11:03:00 +01:00
# Save the parameters to the fields
self.brickset = brickset
2025-01-27 18:39:35 +01:00
self.fields.figure = figure
2025-01-17 11:03:00 +01:00
if not self.select():
2025-01-17 11:03:00 +01:00
raise NotFoundException(
2025-01-27 18:39:35 +01:00
'Minifigure with figure {figure} from set {set} was not found in the database'.format( # noqa: E501
figure=self.fields.figure,
set=self.brickset.fields.set,
2025-01-17 11:03:00 +01:00
),
)
return self