Skip to content

Commit

Permalink
Handle negative numbers properly
Browse files Browse the repository at this point in the history
  • Loading branch information
CoolCat467 committed Nov 5, 2024
1 parent 451a2f7 commit 0d21d4c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ repos:
hooks:
- id: black
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.7.1
rev: v0.7.2
hooks:
- id: ruff
types: [file]
Expand Down
17 changes: 12 additions & 5 deletions src/sanescansrv/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,16 +361,21 @@ def preform_scan(
if not setting.usable:
continue
value: str | int | float = setting.set
if sane.TYPE_STR[device[name].type] == float_:
type_string = sane.TYPE_STR[device[name].type]
if type_string == float_:
assert isinstance(value, str), f"{value = } {type(value) = }"
try:
value = float(value)
except ValueError:
continue
elif sane.TYPE_STR[device[name].type] in ints:
elif type_string in ints:
assert isinstance(value, str), f"{value = } {type(value) = }"
negative = value.startswith("-")
value = value.removeprefix("-")
if value.isdigit():
value = int(value)
if negative:
value *= -1
options = setting.options
if options and isinstance(options, tuple):
min_ = options[0]
Expand All @@ -380,7 +385,7 @@ def preform_scan(
try:
setattr(device, name, value)
except (AttributeError, TypeError) as exc:
print(f"\n{name} = {value}")
print(f"\n{name} = {value!r}")
# traceback.print_exception changed in 3.10
if sys.version_info < (3, 10):
tb = sys.exc_info()[2]
Expand All @@ -389,7 +394,9 @@ def preform_scan(
traceback.print_exception(exc)
## APP_STORAGE["device_settings"][device_name][idx].usable = False
with device.scan(progress) as image:
# bounds = image.getbbox()
bounds = image.getbbox()
if bounds is not None:
image = image.crop(bounds)
image.save(filepath, out_type)

return filename
Expand Down Expand Up @@ -462,7 +469,7 @@ def progress(current: int, total: int) -> None:
progress,
thread_name="preform_scan_async",
)
except SaneError as exc:
except (SaneError, RuntimeError) as exc:
# traceback.print_exception changed in 3.10
if sys.version_info < (3, 10):
tb = sys.exc_info()[2]
Expand Down

0 comments on commit 0d21d4c

Please sign in to comment.