Skip to content

Commit

Permalink
Did ruff formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
denisenkom committed Mar 2, 2024
1 parent 391b5af commit dab80e2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 24 deletions.
58 changes: 37 additions & 21 deletions src/twain/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,16 @@
# Corresponding states are defined in TWAIN spec 2.11 paragraph
# Data Source Manager states
_DsmStates = typing.Literal[
"closed", # TWAIN state 2
"open", # TWAIN state 3
"closed", # TWAIN state 2
"open", # TWAIN state 3
]

# Data Source states
_SourceStates = typing.Literal[
"closed", # TWAIN state 2
"open", # TWAIN state 4
"enabled", # TWAIN state 5
"ready", # TWAIN state 6
"closed", # TWAIN state 2
"open", # TWAIN state 4
"enabled", # TWAIN state 5
"ready", # TWAIN state 6
]


Expand Down Expand Up @@ -126,11 +126,11 @@ def _twain1_alloc(size: int) -> ct.c_void_p:

class _Image(_IImage):
def __init__(
self,
handle,
free: collections.abc.Callable[[typing.Any], None],
lock: collections.abc.Callable[[typing.Any], ct.c_void_p],
unlock: collections.abc.Callable[[typing.Any], None]
self,
handle,
free: collections.abc.Callable[[typing.Any], None],
lock: collections.abc.Callable[[typing.Any], ct.c_void_p],
unlock: collections.abc.Callable[[typing.Any], None],
):
self._handle = handle
self._free = free
Expand Down Expand Up @@ -289,7 +289,8 @@ def _get_capability(self, cap: int, current: int):
raise exceptions.CapabilityFormatNotSupported(msg)
ctype = _mapping[enum.ItemType]
item_p = ct.cast(
ptr + ct.sizeof(structs.TW_ENUMERATION), ct.POINTER(ctype) # type: ignore # needs fixing
ptr + ct.sizeof(structs.TW_ENUMERATION),
ct.POINTER(ctype), # type: ignore # needs fixing
)
values = [el for el in item_p[0 : enum.NumItems]]
return enum.ItemType, (enum.CurrentIndex, enum.DefaultIndex, values)
Expand All @@ -300,7 +301,8 @@ def _get_capability(self, cap: int, current: int):
raise exceptions.CapabilityFormatNotSupported(msg)
ctype = _mapping[arr.ItemType]
item_p = ct.cast(
ptr + ct.sizeof(structs.TW_ARRAY), ct.POINTER(ctype) # type: ignore # needs fixing
ptr + ct.sizeof(structs.TW_ARRAY),
ct.POINTER(ctype), # type: ignore # needs fixing
)
return arr.ItemType, [el for el in item_p[0 : arr.NumItems]]
else:
Expand Down Expand Up @@ -594,7 +596,9 @@ def _process_event(self, msg: structs.MSG) -> tuple[int, int]:
self._state = "ready"
return rv, event.TWMessage

def _modal_loop(self, event_callback: collections.abc.Callable[[int], None] | None) -> None:
def _modal_loop(
self, event_callback: collections.abc.Callable[[int], None] | None
) -> None:
logger.info("entering modal loop")
done = False
msg = structs.MSG()
Expand Down Expand Up @@ -924,13 +928,20 @@ def acquire_natively(
def xfer_ready_callback() -> int:
before(self.image_info)
handle, more = self.xfer_image_natively()
after(_Image(handle=handle, free=self._free, lock=self._lock, unlock=self._unlock), more)
after(
_Image(
handle=handle, free=self._free, lock=self._lock, unlock=self._unlock
),
more,
)
return more

self.set_capability(
constants.ICAP_XFERMECH, constants.TWTY_UINT16, constants.TWSX_NATIVE
)
self._acquire(xfer_ready_callback=xfer_ready_callback, show_ui=show_ui, modal=modal)
self._acquire(
xfer_ready_callback=xfer_ready_callback, show_ui=show_ui, modal=modal
)

def is_twain2(self) -> bool:
return self._version2
Expand Down Expand Up @@ -1072,7 +1083,9 @@ def XferImageByFile(self):
return self.xfer_image_by_file()


def _get_protocol_major_version(requested_protocol_major_version: None | typing.Literal[1, 2]) -> typing.Literal[1, 2]:
def _get_protocol_major_version(
requested_protocol_major_version: None | typing.Literal[1, 2]
) -> typing.Literal[1, 2]:
if requested_protocol_major_version not in [None, 1, 2]:
raise ValueError("Invalid protocol version specified")
if utils.is_windows():
Expand All @@ -1082,7 +1095,9 @@ def _get_protocol_major_version(requested_protocol_major_version: None | typing.
return 2 or requested_protocol_major_version


def _get_dsm(dsm_name: str | None, protocol_major_version: typing.Literal[1, 2]) -> ct.CDLL:
def _get_dsm(
dsm_name: str | None, protocol_major_version: typing.Literal[1, 2]
) -> ct.CDLL:
"""
Loads TWAIN Data Source Manager DLL.
If dsm_name parameter is set will load that exact DSM DLL.
Expand Down Expand Up @@ -1163,7 +1178,7 @@ def __init__(
"""
self._sources: weakref.WeakSet[Source] = weakref.WeakSet()
self._cb: collections.abc.Callable[[int], None] | None = None
self._state : _DsmStates = "closed"
self._state: _DsmStates = "closed"
self._parent_window = parent_window
self._hwnd = 0
if utils.is_windows():
Expand Down Expand Up @@ -1307,7 +1322,8 @@ def _call(
)
if rv != constants.TWRC_SUCCESS:
logger.warning(
"Getting additional error information returned non success code: %d", rv
"Getting additional error information returned non success code: %d",
rv,
)
raise exceptions.TwainError()
code = status.ConditionCode
Expand Down Expand Up @@ -1563,5 +1579,5 @@ def after(more: int) -> None:


# import all constants for backward compatibility
from .lowlevel.constants import * # noqa
from .lowlevel.constants import * # noqa
from .windows import * # noqa
4 changes: 1 addition & 3 deletions tests/test_integ.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,4 @@ def save_and_close(img):
logger.info("opening source")
with sm.open_source() as ss: # this posts a modeless dialog...
logger.info("calling acquire_natively")
ss.acquire_natively(
after=lambda img, no: save_and_close(img)
)
ss.acquire_natively(after=lambda img, no: save_and_close(img))

0 comments on commit dab80e2

Please sign in to comment.