Skip to content

Commit

Permalink
Updates
Browse files Browse the repository at this point in the history
* Removed hatch support
* Updated macOS SWI discovery
* Updated paths for test fixtures in tests
  • Loading branch information
yuce committed Sep 29, 2024
1 parent d399f0d commit 744bab5
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 80 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ pyswip.egg-info/
build/
/.idea/
/.mypy_cache/
.venv
10 changes: 5 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
.PHONY: build clean coverage upload-coverage test upload

build:
hatch build
build

clean:
rm -rf dist build pyswip.egg-info

coverage:
hatch run test:coverage
py.test tests --verbose --cov=pyswip

upload-coverage:
hatch run upload-coverage:run-coveralls
upload-coverage: coverage
coveralls

test:
hatch run test:all
PYTHONPATH=src py.test tests --verbose -m "not slow"

upload:
twine upload dist/*
Expand Down
1 change: 1 addition & 0 deletions ci-requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
coveralls
6 changes: 4 additions & 2 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
hatch==1.12.0
ruff==0.6.2
ruff==0.6.2
build
pytest-cov
mypy>=1.0.0
46 changes: 8 additions & 38 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
requires = ["setuptools"]
build-backend = "setuptools.build_meta"

[project]
name = "pyswip"
dynamic = ["version"]
version = "0.3.0"
description = "PySwip enables querying SWI-Prolog in your Python programs."
readme = "README.md"
requires-python = ">=3.8"
license = "MIT"
authors = [
{ name = "Yuce Tekol", email = "[email protected]" },
]
Expand Down Expand Up @@ -40,39 +39,10 @@ classifiers = [
Download = "https://github.com/yuce/pyswip/releases"
Homepage = "https://github.com/yuce/pyswip"

[tool.hatch.version]
path = "src/pyswip/__init__.py"

[tool.hatch.build.targets.sdist]
packages = ["src/pyswip"]

[tool.hatch.build.targets.wheel]
packages = ["src/pyswip"]

[tool.hatch.envs.test]
dependencies = [
"pytest",
"pytest-cov",
]

[tool.hatch.envs.test.scripts]
all = "py.test tests --verbose"
coverage = "py.test tests --verbose --cov=pyswip"

[tool.hatch.envs.upload-coverage]
dependencies = [
"coveralls",
]

[tool.hatch.envs.upload-coverage.scripts]
run-coveralls = "coveralls"

[tool.hatch.envs.types]
extra-dependencies = [
"mypy>=1.0.0",
]
[tool.hatch.envs.types.scripts]
check = "mypy --install-types --non-interactive {args:pyswip tests}"

[tool.ruff.lint]
ignore = ["F403", "F405", "E721"]

[tool.pytest.ini_options]
markers = [
"slow: marks tests as slow (deselect with '-m \"not slow\"')",
]
2 changes: 1 addition & 1 deletion src/pyswip/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@


# PySwip version
__VERSION__ = "0.2.11"
__VERSION__ = "0.3.0"

from pyswip.prolog import Prolog as Prolog
from pyswip.easy import *
63 changes: 34 additions & 29 deletions src/pyswip/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,24 @@
from ctypes import *
from ctypes.util import find_library
from subprocess import Popen, PIPE
from typing import Tuple

ENV_LIBSWIPL_PATH = "LIBSWIPL_PATH"
ENV_SWI_HOME_DIR = "SWI_HOME_DIR"


class PySwipError(Exception):

def __init__(self, message):
super().__init__(message)


class SwiPrologNotFoundError(PySwipError):

def __init__(self, message="SWI-Prolog not found"):
super().__init__(message)


# To initialize the SWI-Prolog environment, two things need to be done: the
# first is to find where the SO/DLL is located and the second is to find the
# SWI-Prolog home, to get the saved state.
Expand Down Expand Up @@ -153,7 +166,7 @@ def _findSwiplFromExec():
except (OSError, KeyError): # KeyError from accessing rtvars
pass

return (fullName, swiHome)
return fullName, swiHome


def _find_swipl_windows():
Expand All @@ -175,10 +188,10 @@ def _find_swipl_windows():
# libswipl.dll must be in SWI_DIR/bin
libswipl_path = os.path.join(swi_dir, "bin", libswipl)
if not os.path.exists(libswipl_path):
raise FileNotFoundError(f"could not locate {libswipl}", libswipl_path)
raise SwiPrologNotFoundError(f"could not locate {libswipl} at {libswipl_path}")
return libswipl_path, swi_dir

raise FileNotFoundError("could not locate SWI home directory")
raise SwiPrologNotFoundError


def find_swipl_dir_from_registry():
Expand All @@ -205,14 +218,15 @@ def _find_swipl_unix():
"""

# Maybe the exec is on path?
(path, swiHome) = _findSwiplFromExec()
path, swi_home = _findSwiplFromExec()
if path is not None:
return (path, swiHome)
return path, swi_home

# If it is not, use find_library
path = _findSwiplPathFromFindLib()
if path is not None:
return (path, swiHome)
swi_home = find_swi_home(path)
return path, swi_home

# Our last try: some hardcoded paths.
paths = [
Expand All @@ -234,12 +248,13 @@ def _find_swipl_unix():
break

if path is not None:
return (path, swiHome)
swi_home = find_swi_home(path)
return path, swi_home

return (None, None)
raise SwiPrologNotFoundError


def find_swipl_macos_home():
def find_swipl_macos_home() -> Tuple[str, str]:
"""
This function is guesing where SWI-Prolog is
installed in MacOS via .app.
Expand All @@ -259,12 +274,13 @@ def find_swipl_macos_home():
swi_home = "/Applications/SWI-Prolog.app/Contents/swipl"
if os.path.exists(swi_home):
swi_base = os.path.split(swi_home)[0]
lib = find_swipl_dylib(swi_base)
framework_path = os.path.join(swi_base, "Frameworks")
lib = find_swipl_dylib(framework_path)
if lib:
lib_path = os.path.join(swi_base, "Frameworks", lib)
lib_path = os.path.join(framework_path, lib)
return lib_path, swi_home

raise FileNotFoundError("SWI-Prolog home was not found")
return "", ""


def find_swipl_dylib(root) -> str:
Expand All @@ -286,6 +302,10 @@ def _find_swipl_darwin() -> (str, str):
({str, None}, {str, None})
"""

path, swi_home = find_swipl_macos_home()
if path and swi_home:
return path, swi_home

# If the exec is in path
path, swi_home = _findSwiplFromExec()
if path:
Expand All @@ -297,18 +317,7 @@ def _find_swipl_darwin() -> (str, str):
swi_home = find_swi_home(os.path.dirname(path))
return path, swi_home

# Check the standard install path
swi_home = os.environ.get("SWI_HOME_DIR")
if not swi_home:
swi_home = "/Applications/SWI-Prolog.app/Contents/swipl"
if os.path.exists(swi_home):
swi_base = os.path.split(swi_home)[0]
lib = find_swipl_dylib(swi_base)
if lib:
lib_path = os.path.join(swi_base, "Frameworks", lib)
return lib_path, swi_home

raise FileNotFoundError("SWI-Prolog was not found")
raise SwiPrologNotFoundError


def find_swi_home(path) -> str:
Expand Down Expand Up @@ -350,11 +359,7 @@ def _findSwipl() -> (str, str):
fix_windows_path(libswipl_path)
return libswipl_path, swi_home_dir
elif platform == "darwin":
libswipl_path, swi_home_dir = _find_swipl_darwin()
if not libswipl_path:
libswipl_path, swi_home_dir = find_swipl_macos_home()
return libswipl_path, swi_home_dir

return _find_swipl_darwin()
else:
# This should work for other Linux and BSD
return _find_swipl_unix()
Expand Down
3 changes: 3 additions & 0 deletions tests/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

import unittest

import pytest

from pyswip import *


Expand Down Expand Up @@ -285,6 +287,7 @@ def test_sudoku(self):
else:
self.fail("Failed while running example number %d" % i)

@pytest.mark.slow
def test_large_db(self):
"""
Generates a large database, then runs query
Expand Down
9 changes: 7 additions & 2 deletions tests/test_issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
import subprocess
import sys
import unittest
import os

current_dir = os.path.dirname(os.path.abspath(__file__))


class TestIssues(unittest.TestCase):
Expand Down Expand Up @@ -247,7 +250,8 @@ def test_issue_62(self):
from pyswip import Prolog

prolog = Prolog()
prolog.consult("tests/test_unicode.pl", catcherrors=True)
path = os.path.join(current_dir, "test_unicode.pl")
prolog.consult(path, catcherrors=True)
atoms = list(prolog.query("unicode_atom(B)."))

self.assertEqual(len(atoms), 3, "Query should return exactly three atoms")
Expand All @@ -271,7 +275,8 @@ def test_functor_return(self):
import pyswip.prolog as pl

p = pl.Prolog()
p.consult("tests/test_functor_return.pl", catcherrors=True)
path = os.path.join(current_dir, "test_functor_return.pl")
p.consult(path, catcherrors=True)
query = "sentence(Parse_tree, [the,bat,eats,a,cat], [])"
expectedTree = "s(np(d(the), n(bat)), vp(v(eats), np(d(a), n(cat))))"

Expand Down
8 changes: 5 additions & 3 deletions tests/test_prolog.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@
"""
Tests the Prolog class.
"""

import os.path
import unittest

import pyswip.prolog as pl # This implicitly tests library loading code
from tests.test_issues import current_dir


class TestProlog(unittest.TestCase):
Expand Down Expand Up @@ -111,5 +112,6 @@ def test_prolog_read_file(self):
See: https://github.com/yuce/pyswip/issues/10
"""
prolog = pl.Prolog()
prolog.consult("tests/test_read.pl")
list(prolog.query('read_file("tests/test_read.pl", S)'))
path = os.path.join(current_dir, "test_read.pl")
prolog.consult(path)
list(prolog.query(f'read_file("{path}", S)'))

0 comments on commit 744bab5

Please sign in to comment.