bricktracker
migrations
sql
views
__init__.py
app.py
config.py
configuration.py
configuration_list.py
exceptions.py
fields.py
instructions.py
instructions_list.py
login.py
metadata.py
metadata_list.py
minifigure.py
minifigure_list.py
navbar.py
parser.py
part.py
part_list.py
rebrickable.py
rebrickable_image.py
rebrickable_minifigure.py
rebrickable_part.py
rebrickable_set.py
rebrickable_set_list.py
record.py
record_list.py
reload.py
retired.py
retired_list.py
set.py
set_list.py
set_owner.py
set_owner_list.py
set_status.py
set_status_list.py
set_tag.py
set_tag_list.py
socket.py
socket_decorator.py
sql.py
sql_counter.py
sql_migration.py
sql_migration_list.py
sql_stats.py
theme.py
theme_list.py
version.py
wish.py
wish_list.py
docs
static
templates
.dockerignore
.env.sample
.gitignore
CHANGELOG.md
Dockerfile
LICENSE
README.md
__init__.py
app.py
compose.legacy.yml
compose.local.yaml
compose.yaml
entrypoint.sh
requirements.txt
test-server.sh
109 lines
3.5 KiB
Python
109 lines
3.5 KiB
Python
import logging
|
|
import traceback
|
|
from typing import Self, TYPE_CHECKING
|
|
|
|
from .exceptions import ErrorException, NotFoundException
|
|
from .part_list import BrickPartList
|
|
from .rebrickable_minifigure import RebrickableMinifigure
|
|
if TYPE_CHECKING:
|
|
from .set import BrickSet
|
|
from .socket import BrickSocket
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
# Lego minifigure
|
|
class BrickMinifigure(RebrickableMinifigure):
|
|
# Queries
|
|
insert_query: str = 'minifigure/insert'
|
|
generic_query: str = 'minifigure/select/generic'
|
|
select_query: str = 'minifigure/select/specific'
|
|
|
|
# Import a minifigure into the database
|
|
def download(self, socket: 'BrickSocket', refresh: bool = False) -> bool:
|
|
if self.brickset is None:
|
|
raise ErrorException('Importing a minifigure from Rebrickable outside of a set is not supported') # noqa: E501
|
|
|
|
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
|
|
)
|
|
)
|
|
|
|
if not refresh:
|
|
# Insert into database
|
|
self.insert(commit=False)
|
|
|
|
# Insert the rebrickable set into database
|
|
self.insert_rebrickable()
|
|
|
|
# Load the inventory
|
|
if not BrickPartList.download(
|
|
socket,
|
|
self.brickset,
|
|
minifigure=self,
|
|
refresh=refresh
|
|
):
|
|
return False
|
|
|
|
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())
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
# Parts
|
|
def generic_parts(self, /) -> BrickPartList:
|
|
return BrickPartList().from_minifigure(self)
|
|
|
|
# Parts
|
|
def parts(self, /) -> BrickPartList:
|
|
if self.brickset is None:
|
|
raise ErrorException('Part list for minifigure {figure} requires a brickset'.format( # noqa: E501
|
|
figure=self.fields.figure,
|
|
))
|
|
|
|
return BrickPartList().list_specific(self.brickset, minifigure=self)
|
|
|
|
# Select a generic minifigure
|
|
def select_generic(self, figure: str, /) -> Self:
|
|
# Save the parameters to the fields
|
|
self.fields.figure = figure
|
|
|
|
if not self.select(override_query=self.generic_query):
|
|
raise NotFoundException(
|
|
'Minifigure with figure {figure} was not found in the database'.format( # noqa: E501
|
|
figure=self.fields.figure,
|
|
),
|
|
)
|
|
|
|
return self
|
|
|
|
# Select a specific minifigure (with a set and a figure)
|
|
def select_specific(self, brickset: 'BrickSet', figure: str, /) -> Self:
|
|
# Save the parameters to the fields
|
|
self.brickset = brickset
|
|
self.fields.figure = figure
|
|
|
|
if not self.select():
|
|
raise NotFoundException(
|
|
'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,
|
|
),
|
|
)
|
|
|
|
return self
|