Move parse_number out of RebrickableSet as it imports way too much for such a simple function
This commit is contained in:
parent
52f73d5bf9
commit
6d3285dbc9
37
bricktracker/parser.py
Normal file
37
bricktracker/parser.py
Normal 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)
|
@ -7,6 +7,7 @@ from flask import current_app
|
||||
|
||||
from .exceptions import ErrorException, NotFoundException
|
||||
from .instructions import BrickInstructions
|
||||
from .parser import parse_set
|
||||
from .rebrickable import Rebrickable
|
||||
from .rebrickable_image import RebrickableImage
|
||||
from .record import BrickRecord
|
||||
@ -98,7 +99,7 @@ class RebrickableSet(BrickRecord):
|
||||
|
||||
try:
|
||||
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(
|
||||
message='Set {set}: loading from Rebrickable'.format(
|
||||
@ -187,39 +188,3 @@ class RebrickableSet(BrickRecord):
|
||||
'url': str(data['set_url']),
|
||||
'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)
|
||||
|
@ -4,9 +4,9 @@ from typing import Self
|
||||
from flask import current_app
|
||||
|
||||
from .exceptions import NotFoundException
|
||||
from .parser import parse_set
|
||||
from .rebrickable import Rebrickable
|
||||
from .rebrickable_image import RebrickableImage
|
||||
from .rebrickable_set import RebrickableSet
|
||||
from .record_list import BrickRecordList
|
||||
from .wish import BrickWish
|
||||
|
||||
@ -34,7 +34,7 @@ class BrickWishList(BrickRecordList[BrickWish]):
|
||||
@staticmethod
|
||||
def add(set: str, /) -> None:
|
||||
try:
|
||||
set = RebrickableSet.parse_number(set)
|
||||
set = parse_set(set)
|
||||
BrickWish().select_specific(set)
|
||||
except NotFoundException:
|
||||
logger.debug('rebrick.lego.get_set("{set}")'.format(
|
||||
|
Loading…
Reference in New Issue
Block a user