WIP: Initial work on deduplicating the minifigures and parts #57

Draft
gregoo wants to merge 19 commits from gregoo/BrickTracker:master into master
3 changed files with 21 additions and 24 deletions
Showing only changes of commit 29bb92ef93 - Show all commits

View File

@ -21,7 +21,6 @@ logger = logging.getLogger(__name__)
# A set from Rebrickable
class RebrickableSet(BrickRecord):
socket: 'BrickSocket'
theme: 'BrickTheme'
instructions: list[BrickInstructions]
@ -36,7 +35,6 @@ class RebrickableSet(BrickRecord):
self,
/,
*,
socket: 'BrickSocket | None' = None,
record: Row | dict[str, Any] | None = None
):
super().__init__()
@ -44,10 +42,6 @@ class RebrickableSet(BrickRecord):
# Placeholders
self.instructions = []
# Save the socket
if socket is not None:
self.socket = socket
# Ingest the record if it has one
if record is not None:
self.ingest(record)
@ -92,20 +86,21 @@ class RebrickableSet(BrickRecord):
# Load the set from Rebrickable
def load(
self,
socket: 'BrickSocket',
data: dict[str, Any],
/,
*,
from_download=False,
) -> bool:
# Reset the progress
self.socket.progress_count = 0
self.socket.progress_total = 2
socket.progress_count = 0
socket.progress_total = 2
try:
self.socket.auto_progress(message='Parsing set number')
socket.auto_progress(message='Parsing set number')
set = parse_set(str(data['set']))
self.socket.auto_progress(
socket.auto_progress(
message='Set {set}: loading from Rebrickable'.format(
set=set,
),
@ -122,12 +117,12 @@ class RebrickableSet(BrickRecord):
instance=self,
).get()
self.socket.emit('SET_LOADED', self.short(
socket.emit('SET_LOADED', self.short(
from_download=from_download
))
if not from_download:
self.socket.complete(
socket.complete(
message='Set {set}: loaded from Rebrickable'.format(
set=self.fields.set
)
@ -136,7 +131,7 @@ class RebrickableSet(BrickRecord):
return True
except Exception as e:
self.socket.fail(
socket.fail(
message='Could not load the set from Rebrickable: {error}. Data: {data}'.format( # noqa: E501
error=str(e),
data=data,

View File

@ -1,6 +1,6 @@
import logging
import traceback
from typing import Any, Self
from typing import Any, Self, TYPE_CHECKING
from uuid import uuid4
from flask import current_app, url_for
@ -14,6 +14,8 @@ from .rebrickable_set import RebrickableSet
from .set_checkbox import BrickSetCheckbox
from .set_checkbox_list import BrickSetCheckboxList
from .sql import BrickSQL
if TYPE_CHECKING:
from .socket import BrickSocket
logger = logging.getLogger(__name__)
@ -33,14 +35,14 @@ class BrickSet(RebrickableSet):
)
# Import a set into the database
def download(self, data: dict[str, Any], /) -> None:
def download(self, socket: 'BrickSocket', data: dict[str, Any], /) -> None:
# Load the set
if not self.load(data, from_download=True):
if not self.load(socket, data, from_download=True):
return
try:
# Insert into the database
self.socket.auto_progress(
socket.auto_progress(
message='Set {set}: inserting into database'.format(
set=self.fields.set
),
@ -57,13 +59,13 @@ class BrickSet(RebrickableSet):
self.insert_rebrickable()
# Load the inventory
RebrickableParts(self.socket, self).download()
RebrickableParts(socket, self).download()
# Load the minifigures
RebrickableMinifigures(self.socket, self).download()
RebrickableMinifigureList(socket, self).download()
# Commit the transaction to the database
self.socket.auto_progress(
socket.auto_progress(
message='Set {set}: writing to the database'.format(
set=self.fields.set
),
@ -79,7 +81,7 @@ class BrickSet(RebrickableSet):
))
# Complete
self.socket.complete(
socket.complete(
message='Set {set}: imported (<a href="{url}">Go to the set</a>)'.format( # noqa: E501
set=self.fields.set,
url=self.url()
@ -88,7 +90,7 @@ class BrickSet(RebrickableSet):
)
except Exception as e:
self.socket.fail(
socket.fail(
message='Error while importing set {set}: {error}'.format(
set=self.fields.set,
error=e,

View File

@ -109,11 +109,11 @@ class BrickSocket(object):
@self.socket.on(MESSAGES['IMPORT_SET'], namespace=self.namespace)
@rebrickable_socket(self)
def import_set(data: dict[str, Any], /) -> None:
BrickSet(socket=self).download(data)
BrickSet().download(self, data)
@self.socket.on(MESSAGES['LOAD_SET'], namespace=self.namespace)
def load_set(data: dict[str, Any], /) -> None:
BrickSet(socket=self).load(data)
BrickSet().load(self, data)
# Update the progress auto-incrementing
def auto_progress(