Instructions downloader #54

Merged
FrederikBaerentsen merged 19 commits from instructions into master 2025-01-26 19:17:42 +01:00
3 changed files with 41 additions and 39 deletions
Showing only changes of commit 6d3285dbc9 - Show all commits

37
bricktracker/parser.py Normal file
View File

@ -0,0 +1,37 @@
from .exceptions import ErrorException
# Make sense of string supposed to contain a set ID
def parse_set(set: str, /) -> str:
number, _, version = set.partition('-')
# Making sure both are integers
if version == '':
version = 1
try:
number = int(number)
except Exception:
raise ErrorException('Number "{number}" is not a number'.format(
number=number,
))
try:
version = int(version)
except Exception:
raise ErrorException('Version "{version}" is not a number'.format(
version=version,
))
# Make sure both are positive
if number < 0:
raise ErrorException('Number "{number}" should be positive'.format(
number=number,
))
if version < 0:
raise ErrorException('Version "{version}" should be positive'.format( # noqa: E501
version=version,
))
return '{number}-{version}'.format(number=number, version=version)

View File

@ -7,6 +7,7 @@ from flask import current_app
from .exceptions import ErrorException, NotFoundException from .exceptions import ErrorException, NotFoundException
from .instructions import BrickInstructions from .instructions import BrickInstructions
from .parser import parse_set
from .rebrickable import Rebrickable from .rebrickable import Rebrickable
from .rebrickable_image import RebrickableImage from .rebrickable_image import RebrickableImage
from .record import BrickRecord from .record import BrickRecord
@ -98,7 +99,7 @@ class RebrickableSet(BrickRecord):
try: try:
self.socket.auto_progress(message='Parsing set number') self.socket.auto_progress(message='Parsing set number')
set = RebrickableSet.parse_number(str(data['set'])) set = parse_set(str(data['set']))
self.socket.auto_progress( self.socket.auto_progress(
message='Set {set}: loading from Rebrickable'.format( message='Set {set}: loading from Rebrickable'.format(
@ -187,39 +188,3 @@ class RebrickableSet(BrickRecord):
'url': str(data['set_url']), 'url': str(data['set_url']),
'last_modified': str(data['last_modified_dt']), 'last_modified': str(data['last_modified_dt']),
} }
# Make sense of the number from the data
@staticmethod
def parse_number(set: str, /) -> str:
number, _, version = set.partition('-')
# Making sure both are integers
if version == '':
version = 1
try:
number = int(number)
except Exception:
raise ErrorException('Number "{number}" is not a number'.format(
number=number,
))
try:
version = int(version)
except Exception:
raise ErrorException('Version "{version}" is not a number'.format(
version=version,
))
# Make sure both are positive
if number < 0:
raise ErrorException('Number "{number}" should be positive'.format(
number=number,
))
if version < 0:
raise ErrorException('Version "{version}" should be positive'.format( # noqa: E501
version=version,
))
return '{number}-{version}'.format(number=number, version=version)

View File

@ -4,9 +4,9 @@ from typing import Self
from flask import current_app from flask import current_app
from .exceptions import NotFoundException from .exceptions import NotFoundException
from .parser import parse_set
from .rebrickable import Rebrickable from .rebrickable import Rebrickable
from .rebrickable_image import RebrickableImage from .rebrickable_image import RebrickableImage
from .rebrickable_set import RebrickableSet
from .record_list import BrickRecordList from .record_list import BrickRecordList
from .wish import BrickWish from .wish import BrickWish
@ -34,7 +34,7 @@ class BrickWishList(BrickRecordList[BrickWish]):
@staticmethod @staticmethod
def add(set: str, /) -> None: def add(set: str, /) -> None:
try: try:
set = RebrickableSet.parse_number(set) set = parse_set(set)
BrickWish().select_specific(set) BrickWish().select_specific(set)
except NotFoundException: except NotFoundException:
logger.debug('rebrick.lego.get_set("{set}")'.format( logger.debug('rebrick.lego.get_set("{set}")'.format(