Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ The package version runs on its own semver; the schema it is generated from is `

## Unreleased

## 0.150.1 (2026-11-24)

### Fixed

- **A world file that leaves the author fields out leaves them out of every element-folder file again.** Since 0.145.0, `world_file_bytes` and `write_world_file` removed `created_by`, `changed_by`, `changed_via`, `changed_client` and `doors` only from the files the folder reader serves as an element, so a copy (`include_hidden=True`, the default) kept them in every other file it carried under `elements/` or `spatial/`: both files of an id two files hold, a file with no id, a `.JSON` name, a file deeper than a type folder or in a folder that is no type. Now the five are removed from every carried file there that is one JSON object by the folder reader's own JSON rules (UTF-8, a byte order mark dropped, at most 200 deep, every number a double holds), as the folder format says of a world handed on. A file that holds none of the five is carried byte for byte, and so is one that is no such object (an array, text that is not JSON, or JSON the reader refuses, such as a number a double cannot hold): it travels with whatever its text holds, and a share (`include_hidden=False`) leaves it out. `include_author_fields=True` still carries every file byte for byte. The shared fixture `tests/fixtures/author-fields.fixture.json` changes two cases and gains two, the same bytes as the npm SDK's.

## 0.150.0 (2026-11-24)

- **Added: `most_linked_of(graph, *, per_type=None)`, the most linked elements of each type.** From the graph `world_graph` answers for a reader, so only what that reader is served is counted (map space, hidden, proposed and room elements stay out unless the graph brought them in). For each type, at most `per_type` (default 3) of its elements by the landing profile's degree, highest first, ties by id in code point order; a node of degree 0 and a known-of node are never picked, and a type with nothing to pick is left out. It answers `{"per_type", "by_type"}`, each entry `{"id", "name", "degree"}`, the name as the graph holds it or None. A few of every type, never one ranking; it is "the most linked", not "where to start": in a world with a secret, the secret is often the hub, so where a world has a Guide, the Guide is the start. A draft reading, held to the package's own fixture (`tests/fixtures/most-linked.fixture.json`, the same bytes as the npm SDK's) until the landing profile takes it. A `per_type` that is not a whole number of 1 or more is a ValueError naming it. The npm SDK's `mostLinkedOf` answers the same.
Expand Down
2 changes: 1 addition & 1 deletion src/onlyworlds/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.150.0"
__version__ = "0.150.1"
41 changes: 30 additions & 11 deletions src/onlyworlds/worldfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -874,12 +874,26 @@ def _under_element_folders(rel: str) -> bool:
_AUTHOR_FIELDS: tuple[str, ...] = ("created_by", "changed_by", "changed_via", "changed_client", "doors")


def _unread_object(src: Path | bytes) -> dict[str, Any] | None:
"""A carried file under ``elements/`` or ``spatial/`` that the folder reader did not serve as an
element (a name or a depth it passes over, no id, an id two files hold), read as the reader reads
JSON (UTF-8, a byte order mark dropped, at most 200 deep, every number a double holds), when it
holds one JSON object. None otherwise: an array, text that is not JSON, or JSON the reader
refuses, which a rewrite would change, so it travels byte for byte."""
data = src if isinstance(src, bytes) else src.read_bytes()
try:
doc = _outside_json.loads(data.decode("utf-8").removeprefix(_BOM), parse_constant=_reject_constant)
except ValueError: # not UTF-8 (UnicodeDecodeError is a ValueError), not JSON, or refused
return None
return doc if isinstance(doc, dict) else None


def _without_author_fields(body: dict[str, Any] | None) -> bytes | None:
"""An element file's object (``_element_files``) without its author fields, in the folder's own
form (``_jsonfmt.encode``: two-space indent, a trailing newline, the other keys in their order),
or None when it holds none of them or the file was not read as an element (``body`` None): then
it travels byte for byte. Only top-level keys go; an ``x_*`` value that holds one of the names is
the extension's, and stays."""
"""An element-folder file's object (``_element_files``, else ``_unread_object``) without its
author fields, in the folder's own form (``_jsonfmt.encode``: two-space indent, a trailing
newline, the other keys in their order), or None when it holds none of them or holds no JSON
object (``body`` None): then it travels byte for byte. Only top-level keys go; an ``x_*`` value
that holds one of the names is the extension's, and stays."""
if body is None or not any(k in body for k in _AUTHOR_FIELDS):
return None
return encode({k: v for k, v in body.items() if k not in _AUTHOR_FIELDS})
Expand Down Expand Up @@ -964,7 +978,10 @@ def in_private(rel: str) -> bool:
if rel == WORLD_FILE:
plan.append((rel, world_bytes))
continue
bare = None if include_author_fields else _without_author_fields(elements.get(rel))
body = elements.get(rel)
if body is None and not include_author_fields and _under_element_folders(rel):
body = _unread_object(files[rel])
bare = None if include_author_fields else _without_author_fields(body)
plan.append((rel, files[rel] if bare is None else bare))
return top, plan

Expand Down Expand Up @@ -1078,11 +1095,13 @@ def write_world_file(
- Author fields are left out: ``created_by``, ``changed_by``, ``changed_via``,
``changed_client`` and ``doors`` record which member and which tool wrote each element (a
membership id, a door), which means nothing to someone the world is handed to, so every
element file (one the folder reader serves, as above) loses them;
``created_at`` and ``updated_at`` stay. A file
holding any of them is rewritten without them in the folder's own form (two-space indent, a
trailing newline, the other keys in their order); one holding none, or that was not read
as an element, travels byte for byte, and nothing outside the element files is touched.
carried file under ``elements/`` or ``spatial/`` that holds one JSON object loses them,
whether or not the folder reader serves it as an element; ``created_at`` and
``updated_at`` stay. A file holding any of them is rewritten without them in the folder's
own form (two-space indent, a trailing newline, the other keys in their order); one holding
none, one that holds an array or is not JSON, and one whose JSON the reader refuses (nested
past 200, or a number a double cannot hold, which a rewrite would change) travel byte for
byte, and nothing outside those two folders is touched.
``include_author_fields=True`` carries every element file byte for byte, for an archive of
your own folder.
- ``world.json`` is carried byte for byte, ``producer``, ``source`` and
Expand Down
48 changes: 39 additions & 9 deletions tests/fixtures/author-fields.fixture.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"what": "The world file writers' author fields: writeWorldFile (npm SDK) and world_file_bytes (Python package) on the same folders. Every expected file is written by hand. The same bytes in both packages; each test holds this file's sha256.",
"keys": "fields are the five a world file leaves out of every element file by default. Each case's files is a folder, path to text (UTF-8); every file in it travels. default is the archive's entries with the default, path to text; with includeAuthorFields (include_author_fields) true the entries are files, each byte for byte. stripped counts the element files that lost a field.",
"keys": "fields are the five a world file leaves out by default of every file under elements/ or the legacy spatial/ that holds one JSON object, read as an element or not. Each case's files is a folder, path to text (UTF-8); every file in it travels. default is the archive's entries with the default, path to text; with includeAuthorFields (include_author_fields) true the entries are files, each byte for byte. stripped counts the files that lost a field.",
"fields": ["created_by", "changed_by", "changed_via", "changed_client", "doors"],
"cases": [
{
Expand Down Expand Up @@ -32,7 +32,7 @@
"stripped": 0
},
{
"name": "only the fields present are removed, a null one too; a file with no id is not read as an element and keeps its bytes",
"name": "only the fields present are removed, a null one too; a file with no id is not read as an element and loses them all the same",
"files": {
"world.json": "{\n \"id\": \"0192a000-aaaa-7bbb-8ccc-000000000001\",\n \"name\": \"W\"\n}\n",
"elements/location/hall--l1.json": "{\r\n \"id\": \"l1\",\r\n \"name\": \"Hall\",\r\n \"changed_by\": null,\r\n \"doors\": {}\r\n}\r\n",
Expand All @@ -41,9 +41,9 @@
"default": {
"world.json": "{\n \"id\": \"0192a000-aaaa-7bbb-8ccc-000000000001\",\n \"name\": \"W\"\n}\n",
"elements/location/hall--l1.json": "{\n \"id\": \"l1\",\n \"name\": \"Hall\"\n}\n",
"elements/event/only--e2.json": "{\"created_by\":\"u\",\"doors\":{\"x\":\"2026-10-29T10:00:00Z\"}}"
"elements/event/only--e2.json": "{}\n"
},
"stripped": 1
"stripped": 2
},
{
"name": "a rewritten file is in the folder's own form: no byte order mark, strings, numbers and nesting as JSON.stringify writes them",
Expand All @@ -58,7 +58,7 @@
"stripped": 1
},
{
"name": "only element files: a .json file, the extension in lower case, directly in elements/<type>/ or the legacy spatial/<type>/; world.json, records, media and every other file byte for byte",
"name": "every file under elements/ or the legacy spatial/ that holds one JSON object loses them, whatever its name, extension or depth; one that holds an array or is not JSON keeps its bytes, and so do world.json, records and media",
"files": {
"world.json": "{\n \"id\": \"0192a000-aaaa-7bbb-8ccc-000000000001\",\n \"name\": \"W\",\n \"created_by\": \"u\"\n}\n",
"spatial/pin/p--p1.json": "{\"id\":\"p1\",\"name\":\"P\",\"changed_via\":\"site\"}",
Expand All @@ -74,16 +74,46 @@
"default": {
"world.json": "{\n \"id\": \"0192a000-aaaa-7bbb-8ccc-000000000001\",\n \"name\": \"W\",\n \"created_by\": \"u\"\n}\n",
"spatial/pin/p--p1.json": "{\n \"id\": \"p1\",\n \"name\": \"P\"\n}\n",
"elements/map/M--m1.JSON": "{\"id\":\"m1\",\"name\":\"M\",\"changed_client\":\"atlas\"}",
"elements/character/sub/s.json": "{\"id\":\"s\",\"created_by\":\"u\"}",
"elements/character/notes.txt": "{\"created_by\":\"u\"}",
"elements/map/M--m1.JSON": "{\n \"id\": \"m1\",\n \"name\": \"M\"\n}\n",
"elements/character/sub/s.json": "{\n \"id\": \"s\"\n}\n",
"elements/character/notes.txt": "{}\n",
"elements/character/list.json": "[{\"created_by\":\"u\"}]",
"elements/character/broken.json": "{\"id\":\"x\",\"created_by\":\"u\"",
"media/m.json": "{\"created_by\":\"u\"}",
"records/RECORDS.json": "[{\"producer\":\"lab\",\"format\":\"f\",\"version\":\"1\",\"deltas\":false,\"created_by\":\"u\"}]",
"records/lab/r.json": "{\"created_by\":\"u\",\"doors\":{}}"
},
"stripped": 1
"stripped": 4
},
{
"name": "two files that hold one id are no element, and both lose them; a file holding a number no double can hold is not rewritten, so it keeps its bytes",
"files": {
"world.json": "{\n \"id\": \"0192a000-aaaa-7bbb-8ccc-000000000001\",\n \"name\": \"W\"\n}\n",
"elements/character/a--d1.json": "{\"id\":\"d1\",\"name\":\"A\",\"created_by\":\"u\"}",
"elements/character/b--d1.json": "{\"id\":\"d1\",\"name\":\"B\",\"changed_by\":\"v\"}",
"elements/object/big--o1.json": "{\"id\":\"o1\",\"name\":\"Big\",\"weight\":1e400,\"created_by\":\"u\"}"
},
"default": {
"world.json": "{\n \"id\": \"0192a000-aaaa-7bbb-8ccc-000000000001\",\n \"name\": \"W\"\n}\n",
"elements/character/a--d1.json": "{\n \"id\": \"d1\",\n \"name\": \"A\"\n}\n",
"elements/character/b--d1.json": "{\n \"id\": \"d1\",\n \"name\": \"B\"\n}\n",
"elements/object/big--o1.json": "{\"id\":\"o1\",\"name\":\"Big\",\"weight\":1e400,\"created_by\":\"u\"}"
},
"stripped": 2
},
{
"name": "a file the reader passed over that holds none of the five at its top level is not rewritten: its spacing and its bytes travel as written, a name inside an extension's value included",
"files": {
"world.json": "{\n \"id\": \"0192a000-aaaa-7bbb-8ccc-000000000001\",\n \"name\": \"W\"\n}\n",
"elements/character/old/odd.json": "{ \"id\" : \"o\",\n\t\"name\":\"Odd\", \"created_at\" : \"2026-10-01T09:00:00Z\" }\n\n",
"elements/character/Spaced.JSON": "{\"id\":\"sp\", \"x_note\":{\"created_by\":\"kept, an extension's own\"}}"
},
"default": {
"world.json": "{\n \"id\": \"0192a000-aaaa-7bbb-8ccc-000000000001\",\n \"name\": \"W\"\n}\n",
"elements/character/old/odd.json": "{ \"id\" : \"o\",\n\t\"name\":\"Odd\", \"created_at\" : \"2026-10-01T09:00:00Z\" }\n\n",
"elements/character/Spaced.JSON": "{\"id\":\"sp\", \"x_note\":{\"created_by\":\"kept, an extension's own\"}}"
},
"stripped": 0
}
]
}
5 changes: 3 additions & 2 deletions tests/test_outside_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,9 @@ def calls(path: Path, module: str) -> int:
assert len(files) > 40 # the search saw the package
assert {p.name: calls(p, "json") for p in files if calls(p, "json")} == NAMED
used = {p.name: calls(p, "_outside_json") for p in files if calls(p, "_outside_json")}
# worldfile.py: an archive's world.json and a folder's records manifest; folder.py's reader reads its element files.
assert used == {"folder.py": 1, "kanka.py": 1, "legendkeeper.py": 2, "lint.py": 1, "worldfile.py": 2}
# worldfile.py: an archive's world.json, a folder's records manifest, and an element-folder file the reader
# passed over, read for its author fields; folder.py's reader reads its element files.
assert used == {"folder.py": 1, "kanka.py": 1, "legendkeeper.py": 2, "lint.py": 1, "worldfile.py": 3}


# --- a writer that must read a file to leave something out ------------------------------------------------
Expand Down
35 changes: 27 additions & 8 deletions tests/test_worldfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -754,12 +754,31 @@ def test_a_share_carries_under_the_element_folders_only_what_the_reader_serves(t
for n, group in enumerate(groups):
alone = make_folder(tmp_path / f"alone-{n}", {"world.json": world_json(), **{p: unread[p] for p in group}})
assert _shared(alone) == (set(), [], sorted(group)), group
# A copy carries each of them byte for byte, and so does one that leaves author fields out: a file
# not read as an element is never rewritten.
for with_authors in (True, False):
copy = read_world_file(world_file_bytes(whole, include_author_fields=with_authors)).entries
assert all(copy[p] == data for p, data in unread.items()), with_authors
assert set(copy) == {*control, *unread}
# A copy carries each of them byte for byte. One that leaves author fields out carries them all too,
# and removes the five from every one that is one JSON object by the reader's own JSON rules, read as
# an element or not (since 0.150.1: the folder spec's guidance; 0.145.0 to 0.150.0 kept them); a file
# that is no such object travels byte for byte.
copy = read_world_file(world_file_bytes(whole, include_author_fields=True)).entries
assert all(copy[p] == data for p, data in unread.items())
assert set(copy) == {*control, *unread}
not_objects = {
"elements/character/broken.json",
"elements/character/latin.json",
"elements/character/list.json",
"spatial/pin/cut.JSON",
"elements/character/two.json",
"elements/character/tail.json",
}
assert not_objects <= set(unread)
bare = read_world_file(world_file_bytes(whole)).entries
assert set(bare) == {*control, *unread}
for p, data in unread.items():
if p in not_objects:
assert bare[p] == data, p
continue
rest = json.loads(data)
assert rest.pop("changed_by") == "m-1", p
assert bare[p] == encode(rest), f"{p}: the file the reader passed over loses its author field"
# Kept: a null mark, a nested mark, whitespace after the value, a byte order mark, hidden: true
# outside the element folders, and one UUID in two spellings, which is two ids, each judged by its own mark.
keep: dict[str, bytes] = {
Expand All @@ -782,8 +801,8 @@ def test_a_share_carries_under_the_element_folders_only_what_the_reader_serves(t

# The same bytes as the npm SDK's test/fixtures/author-fields.fixture.json (both suites hold its sha256).
AUTHOR_FIXTURE = REPO / "tests" / "fixtures" / "author-fields.fixture.json"
AUTHOR_FIXTURE_SHA256 = "481db037f008ceff9989bbe44b2c27a41ba4205d7e7fd258792cbadc2365787d"
AUTHOR_CASES = 5
AUTHOR_FIXTURE_SHA256 = "6943ce5a8c2cd7bf69f82facb6f1d545d87d68f5ea0104cc4d711d94e035898a"
AUTHOR_CASES = 7
AUTHOR_FIELDS = ["created_by", "changed_by", "changed_via", "changed_client", "doors"]


Expand Down
Loading