Skip to content

Commit

Permalink
feat: Check scanline is available
Browse files Browse the repository at this point in the history
  • Loading branch information
flozz committed Mar 28, 2024
1 parent 42cac35 commit b49c3f6
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
26 changes: 24 additions & 2 deletions scanline_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,20 @@ def _get_scanline_cmd():
return os.environ.get("SCANLINE_CMD", "scanline")


def _check_scanline_available():
raise NotImplementedError() # TODO
def _is_scanline_available():
"""Checks if the scanline command is available.
:rtype: bool
"""
cmd = _get_scanline_cmd()
if os.path.isfile(cmd) and os.access(cmd, os.X_OK):
return True
if "PATH" in os.environ:
for path in os.environ["PATH"].split(":"):
cmd_path = os.path.join(path, cmd)
if os.path.isfile(cmd_path) and os.access(cmd_path, os.X_OK):
return True
return False


def list_scanners(browsesecs=1, verbose=False):
Expand All @@ -102,6 +114,11 @@ def list_scanners(browsesecs=1, verbose=False):
:rtype: list(str)
:returns: the available scanners.
"""
if not _is_scanline_available():
raise ScanlineExecutableNotFound(
"The scanline command was not found. Is scanline installed?"
)

command = [_get_scanline_cmd()]
command += ["-list"]
command += ["-browsesecs", str(browsesecs)]
Expand Down Expand Up @@ -167,6 +184,11 @@ def scan_flatbed(
:rtype: None
"""
if not _is_scanline_available():
raise ScanlineExecutableNotFound(
"The scanline command was not found. Is scanline installed?"
)

# Normalize path
output_path = Path(output_path).absolute()

Expand Down
38 changes: 38 additions & 0 deletions test/test_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,28 @@ def test_env_overide(self, monkeypatch):
assert scanline_wrapper._get_scanline_cmd() == "foobar"


class Test__is_scanline_available:

def test_with_command_not_available(self, monkeypatch):
monkeypatch.setenv(
"SCANLINE_CMD",
(Path(__file__).parent / "mock" / "scanline-not-exists").as_posix(),
)
assert scanline_wrapper._is_scanline_available() is False

def test_with_command_path(self, monkeypatch):
monkeypatch.setenv(
"SCANLINE_CMD",
(Path(__file__).parent / "mock" / "scanline-nop.sh").as_posix(),
)
assert scanline_wrapper._is_scanline_available() is True

def test_with_command_in_path(self, monkeypatch):
monkeypatch.setenv("PATH", (Path(__file__).parent / "mock").as_posix())
monkeypatch.setenv("SCANLINE_CMD", "scanline-nop.sh")
assert scanline_wrapper._is_scanline_available() is True


class Test_list_scanner:

def test_list_scanner_with_2_scanners(self, monkeypatch):
Expand All @@ -36,6 +58,14 @@ def test_list_scanner_with_no_scanners(self, monkeypatch):
scanners = scanline_wrapper.list_scanners()
assert len(scanners) == 0

def test_scanline_not_available(self, monkeypatch):
monkeypatch.setenv(
"SCANLINE_CMD",
(Path(__file__).parent / "mock" / "scanline-not-exists").as_posix(),
)
with pytest.raises(scanline_wrapper.ScanlineExecutableNotFound):
scanline_wrapper.list_scanners()


class Test_scan_flatbed:

Expand Down Expand Up @@ -126,3 +156,11 @@ def test_scanner_not_found(self, monkeypatch, tmp_path):

with pytest.raises(scanline_wrapper.ScanlineScannerNotFound):
scanline_wrapper.scan_flatbed(tmp_path / "out.jpg", scanner="XXXXXXXX")

def test_scanline_not_available(self, monkeypatch, tmp_path):
monkeypatch.setenv(
"SCANLINE_CMD",
(Path(__file__).parent / "mock" / "scanline-not-exists").as_posix(),
)
with pytest.raises(scanline_wrapper.ScanlineExecutableNotFound):
scanline_wrapper.scan_flatbed(tmp_path / "out.jpg")

0 comments on commit b49c3f6

Please # to comment.