Skip to content

Commit 5cc3fb7

Browse files
committed
Merge branch 'main' into feat/logging
- keep both imports in wiki_tools: logging and warnings - semantic_search debug output logs at DEBUG, over main's prints, while keeping main's result count n and the truncation warning
2 parents f63458c + 3a45142 commit 5cc3fb7

10 files changed

Lines changed: 445 additions & 35 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,41 @@
1010
1111
<!-- version list -->
1212

13+
## v2.2.0 (2026-09-02)
14+
15+
### Features
16+
17+
- **wtsite**: Allow an edit comment on upload_page
18+
([`0d3899c`](https://github.com/OpenSemanticLab/osw-python/commit/0d3899c782998c5750338dc11beea569207cb26c))
19+
20+
21+
## v2.1.5 (2026-09-02)
22+
23+
### Bug Fixes
24+
25+
- Handle empty SMW ask results in semantic_search
26+
([`e00ca8b`](https://github.com/OpenSemanticLab/osw-python/commit/e00ca8be80eb1575a520b609d5d988604ad59e24))
27+
28+
29+
## v2.1.4 (2026-09-02)
30+
31+
### Bug Fixes
32+
33+
- **page_package**: Apply the same search order to package scripts
34+
([`751a0e3`](https://github.com/OpenSemanticLab/osw-python/commit/751a0e3df80000e639349401b29a6ed311fb5f69))
35+
36+
- **page_package**: Prefer the package found in the working dir
37+
([`6e58d9e`](https://github.com/OpenSemanticLab/osw-python/commit/6e58d9ea1fe9067326571356ef8de736c7938ab2))
38+
39+
40+
## v2.1.3 (2026-09-02)
41+
42+
### Bug Fixes
43+
44+
- **wtsite**: Repair the dangling combine_into reference
45+
([`707be40`](https://github.com/OpenSemanticLab/osw-python/commit/707be4030e9221e8180de0198e4282b4d0cb4fcc))
46+
47+
1348
## v2.1.2 (2026-09-02)
1449

1550
### Bug Fixes

‎CITATION.cff‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ message: "If you use this software, please cite it as below."
33
type: software
44
title: "OpenSemanticWorld Python Package (osw-python)"
55
# Managed by python-semantic-release (version_variables). Do not edit by hand.
6-
version: 2.1.2
6+
version: 2.2.0
77
abstract: "Python toolset for data processing, queries, wikicode generation and page manipulation within OpenSemanticLab: work with OpenSemanticLab instances as typed pydantic entities, query with semantic search, generate models from wiki schemas, and write changes back."
88
repository-code: "https://github.com/OpenSemanticLab/osw-python"
99
url: "https://opensemanticlab.github.io/osw-python/"

‎pyproject.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ license-files = ["LICENSE.txt"]
88
requires-python = ">=3.10,<3.14"
99
# Managed by python-semantic-release (see [tool.semantic_release]). Do not
1010
# edit by hand; it is bumped automatically on merge to main.
11-
version = "2.1.2"
11+
version = "2.2.0"
1212
classifiers = [
1313
"Development Status :: 3 - Alpha",
1414
"Intended Audience :: Developers",

‎src/osw/controller/page_package.py‎

Lines changed: 73 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,32 @@ def find_package_dir(
248248
return matching_dirs[0]
249249

250250

251+
def find_first_package_dir(
252+
package_or_script_name: str, search_paths: List[Path] = None
253+
) -> Optional[Path]:
254+
"""searches the search_paths in order and returns the first match
255+
256+
Unlike find_package_dir, which searches all paths at once and rejects a
257+
name that exists in more than one of them, this prefers the earlier path.
258+
Returns None if no search path holds the name. A path holding more than one
259+
match is ambiguous on its own, so it is skipped with a warning.
260+
"""
261+
if search_paths is None:
262+
search_paths = []
263+
for search_path in search_paths:
264+
try:
265+
return find_package_dir(package_or_script_name, [search_path])
266+
except FileNotFoundError:
267+
# expected: not every search path holds every package
268+
continue
269+
except ValueError as e:
270+
warn(
271+
f"Multiple elements {package_or_script_name} found in "
272+
f"{search_path}: {e}"
273+
)
274+
return None
275+
276+
251277
def get_listed_pages_from_package_info(package_info: Union[dict, Path]) -> List[str]:
252278
"""Takes in the output of read_package_info_file and returns a list of
253279
pages listed in the package"""
@@ -663,35 +689,58 @@ def recursive(
663689
if params.read_listed_pages_from_script:
664690
search_paths = [params.script_dir]
665691
search_paths.extend(params.additional_script_dirs)
666-
try:
667-
script_path = find_package_dir(
668-
f"{package_to_process}.py", search_paths
669-
)
670-
package_script = read_package_script_file(script_path)
671-
new_listed_pages = get_listed_pages_from_package_script(
672-
package_script
673-
)
674-
required_packages = get_required_packages_from_package_script(
675-
package_script
692+
# Prefer the script in script_dir over the additional dirs,
693+
# instead of failing on a script present in several.
694+
script_path = find_first_package_dir(
695+
f"{package_to_process}.py", search_paths
696+
)
697+
new_listed_pages = []
698+
required_packages = []
699+
if script_path is None:
700+
warn(
701+
f"Package script for {package_to_process} not found in any "
702+
f"of the search paths: {search_paths}"
676703
)
677-
except Exception as e:
678-
warn(f"Error reading package script for {package_to_process}: {e}")
679-
new_listed_pages = []
680-
required_packages = []
704+
else:
705+
try:
706+
package_script = read_package_script_file(script_path)
707+
new_listed_pages = get_listed_pages_from_package_script(
708+
package_script
709+
)
710+
required_packages = get_required_packages_from_package_script(
711+
package_script
712+
)
713+
except Exception as e:
714+
warn(
715+
f"Error reading package script for "
716+
f"{package_to_process}: {e}"
717+
)
681718
else:
682719
search_paths = [params.creation_config.working_dir.parent]
683720
search_paths.extend(params.additional_package_dirs)
684-
try:
685-
package_dir = find_package_dir(package_to_process, search_paths)
686-
package_info = read_package_info_file(package_dir)
687-
new_listed_pages = get_listed_pages_from_package_info(package_info)
688-
required_packages = get_required_packages_from_package_info_file(
689-
package_info
721+
# Prefer the package in the working dir over the additional
722+
# dirs, instead of failing on a package present in several.
723+
package_dir = find_first_package_dir(package_to_process, search_paths)
724+
new_listed_pages = []
725+
required_packages = []
726+
if package_dir is None:
727+
warn(
728+
f"Package info for {package_to_process} not found in any "
729+
f"of the search paths: {search_paths}"
690730
)
691-
except Exception as e:
692-
warn(f"Error reading package info for {package_to_process}: {e}")
693-
new_listed_pages = []
694-
required_packages = []
731+
else:
732+
try:
733+
package_info = read_package_info_file(package_dir)
734+
new_listed_pages = get_listed_pages_from_package_info(
735+
package_info
736+
)
737+
required_packages = (
738+
get_required_packages_from_package_info_file(package_info)
739+
)
740+
except Exception as e:
741+
warn(
742+
f"Error reading package info for {package_to_process}: {e}"
743+
)
695744
# Check for redundant pages
696745
for pack_ in listed_pages.keys():
697746
new_redundant_pages = list(

‎src/osw/wiki_tools.py‎

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import getpass
22
import logging
3+
import warnings
34
from typing import Dict, List, Optional, Tuple, Union
45

56
import mwclient
@@ -219,6 +220,33 @@ def prefix_search_(single_text) -> Union[List[str], dict]:
219220
# return page_list # original return
220221

221222

223+
def _ask_results_as_dict(results: Union[dict, list]) -> dict:
224+
"""Normalise the ``results`` payload of an SMW ``ask`` response to a mapping.
225+
226+
SMW serialises a non-empty result set as a JSON object keyed by page title,
227+
but an empty one as a JSON array, which would otherwise break attribute
228+
access on the result.
229+
230+
Parameters
231+
----------
232+
results :
233+
The ``result["query"]["results"]`` payload of an SMW ``ask`` response.
234+
235+
Returns
236+
-------
237+
result:
238+
The payload normalised to a dict, keyed by page title (or index if a
239+
title is unavailable).
240+
"""
241+
if isinstance(results, dict):
242+
return results
243+
if not results:
244+
return {}
245+
return {
246+
page.get("fulltext", str(index)): page for index, page in enumerate(results)
247+
}
248+
249+
222250
def semantic_search(
223251
site: mwclient.client.Site, query: Union[str, List[str], SearchParam]
224252
) -> Union[List[str], List[dict]]:
@@ -245,26 +273,39 @@ def semantic_search_(single_query):
245273
page_list = list()
246274
single_query += f"|limit={query.limit}"
247275
result = site.api("ask", query=single_query, format="json")
276+
results = _ask_results_as_dict(result["query"]["results"])
277+
n = len(results)
248278
if query.debug:
249-
if len(result["query"]["results"]) == 0:
279+
if n == 0:
250280
_logger.debug(f"Query '{single_query}' returned no results")
251281
else:
252-
_logger.debug(
253-
"Query '{}' returned {} results".format(
254-
single_query, len(result["query"]["results"])
255-
)
256-
)
282+
_logger.debug(f"Query '{single_query}' returned {n} results")
283+
if n >= query.limit:
284+
warnings.warn(
285+
f"Query '{single_query}' returned {n} results, which meets the "
286+
f"requested limit of {query.limit}. Results are truncated - raise "
287+
f"the limit or page through with '|offset=' to retrieve the "
288+
f"remainder."
289+
)
257290
if query.return_json:
258291
return result
259292

260-
for page in result["query"]["results"].values():
293+
dropped = 0
294+
for page in results.values():
261295
title = page["fulltext"]
262296
exists = page["exists"]
263297
if "#" not in title and query.debug:
264298
_logger.debug(title)
265299
# original position of "page_list.append(title)" line
266300
if exists == "1":
267301
page_list.append(title)
302+
else:
303+
dropped += 1
304+
if dropped > 0:
305+
warnings.warn(
306+
f"Query '{single_query}': {dropped} of {n} results were dropped "
307+
f"because the wiki reported them as non-existing pages."
308+
)
268309
return page_list
269310

270311
if query.parallel:

‎src/osw/wtsite.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,8 @@ class UploadPageParam(OswBaseModel):
652652
"""If True, uploads the pages in parallel."""
653653
debug: Optional[bool] = False
654654
"""If True, debug messages will be printed."""
655+
comment: Optional[str] = None
656+
"""Edit comment for the page history, applied to every uploaded page."""
655657

656658
class Config:
657659
arbitrary_types_allowed = True
@@ -691,7 +693,7 @@ def upload_page_(page, index: int = None):
691693
f"WtSite from which this method is called from "
692694
f"are not matching!"
693695
)
694-
page.edit()
696+
page.edit(param.comment)
695697

696698
if index is None:
697699
_logger.info(f"Uploaded page to {page.get_url()}.")

‎tests/test_page_package_search.py‎

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
"""Unit tests for package directory lookup in osw.controller.page_package.
2+
3+
Regression guard for #135: a package present in both the working dir and an
4+
additional package dir must resolve to the working dir one, instead of raising
5+
because the name was found more than once.
6+
"""
7+
8+
import pytest
9+
10+
from osw.controller.page_package import find_first_package_dir, find_package_dir
11+
12+
13+
@pytest.fixture
14+
def two_dirs(tmp_path):
15+
"""A working dir and an additional dir, both holding 'MyPackage'."""
16+
work_dir = tmp_path / "work"
17+
extra_dir = tmp_path / "extra"
18+
(work_dir / "MyPackage").mkdir(parents=True)
19+
(extra_dir / "MyPackage").mkdir(parents=True)
20+
return work_dir, extra_dir
21+
22+
23+
def test_prefers_the_first_search_path(two_dirs):
24+
work_dir, extra_dir = two_dirs
25+
26+
found = find_first_package_dir("MyPackage", [work_dir, extra_dir])
27+
28+
assert found == work_dir / "MyPackage"
29+
30+
31+
def test_search_order_decides(two_dirs):
32+
"""The same two dirs in the other order resolve to the other package."""
33+
work_dir, extra_dir = two_dirs
34+
35+
found = find_first_package_dir("MyPackage", [extra_dir, work_dir])
36+
37+
assert found == extra_dir / "MyPackage"
38+
39+
40+
def test_falls_through_to_a_later_path(two_dirs, tmp_path):
41+
_, extra_dir = two_dirs
42+
empty_dir = tmp_path / "empty"
43+
empty_dir.mkdir()
44+
45+
found = find_first_package_dir("MyPackage", [empty_dir, extra_dir])
46+
47+
assert found == extra_dir / "MyPackage"
48+
49+
50+
def test_returns_none_when_nothing_matches(tmp_path):
51+
assert find_first_package_dir("MyPackage", [tmp_path]) is None
52+
53+
54+
def test_returns_none_for_empty_search_paths():
55+
assert find_first_package_dir("MyPackage", None) is None
56+
57+
58+
def test_find_package_dir_still_rejects_ambiguity(two_dirs):
59+
"""The all-at-once helper keeps its previous behaviour."""
60+
work_dir, extra_dir = two_dirs
61+
62+
with pytest.raises(ValueError):
63+
find_package_dir("MyPackage", [work_dir, extra_dir])
64+
65+
66+
@pytest.fixture
67+
def two_script_dirs(tmp_path):
68+
"""A script dir and an additional dir, both holding 'MyPackage.py'."""
69+
script_dir = tmp_path / "scripts"
70+
extra_dir = tmp_path / "extra_scripts"
71+
script_dir.mkdir()
72+
extra_dir.mkdir()
73+
(script_dir / "MyPackage.py").write_text("# working copy")
74+
(extra_dir / "MyPackage.py").write_text("# dependency copy")
75+
return script_dir, extra_dir
76+
77+
78+
def test_prefers_the_first_search_path_for_scripts(two_script_dirs):
79+
"""The script lookup takes the same route, so files must resolve too."""
80+
script_dir, extra_dir = two_script_dirs
81+
82+
found = find_first_package_dir("MyPackage.py", [script_dir, extra_dir])
83+
84+
assert found == script_dir / "MyPackage.py"
85+
86+
87+
def test_falls_through_to_a_later_script_dir(two_script_dirs, tmp_path):
88+
_, extra_dir = two_script_dirs
89+
empty_dir = tmp_path / "empty_scripts"
90+
empty_dir.mkdir()
91+
92+
found = find_first_package_dir("MyPackage.py", [empty_dir, extra_dir])
93+
94+
assert found == extra_dir / "MyPackage.py"

0 commit comments

Comments
 (0)