Inject the socket only where necessary

This commit is contained in:
Gregoo 2025-01-27 14:20:12 +01:00
parent f8ba0abbbe
commit 29bb92ef93
3 changed files with 21 additions and 24 deletions

View File

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

View File

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

View File

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