From 29bb92ef9386ff2d334521966a3ffab197ab6ad2 Mon Sep 17 00:00:00 2001 From: Gregoo Date: Mon, 27 Jan 2025 14:20:12 +0100 Subject: [PATCH] Inject the socket only where necessary --- bricktracker/rebrickable_set.py | 21 ++++++++------------- bricktracker/set.py | 20 +++++++++++--------- bricktracker/socket.py | 4 ++-- 3 files changed, 21 insertions(+), 24 deletions(-) diff --git a/bricktracker/rebrickable_set.py b/bricktracker/rebrickable_set.py index 23072d3..1cd4b8d 100644 --- a/bricktracker/rebrickable_set.py +++ b/bricktracker/rebrickable_set.py @@ -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, diff --git a/bricktracker/set.py b/bricktracker/set.py index 523d336..e521268 100644 --- a/bricktracker/set.py +++ b/bricktracker/set.py @@ -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 (Go to the set)'.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, diff --git a/bricktracker/socket.py b/bricktracker/socket.py index 3fbd6cd..7aedaf2 100644 --- a/bricktracker/socket.py +++ b/bricktracker/socket.py @@ -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(