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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ The package version runs on its own semver; the schema it is generated from is `

## Unreleased

- **The Kanka door reads a time with no offset as UTC**, the zone Kanka writes. An export whose campaign `created_at` had no offset while its entities' had one (or the other way round) stopped with `TypeError: can't subtract offset-naive and offset-aware datetimes`; it now imports, and the possible starter entries are named as for any other export. Exports whose times all carry an offset, or all carry none, read as before.
- **The Kanka door's tables and wording are data**: `src/onlyworlds/convert/kanka.json` holds Kanka's folders to element types, its keys to fields, the fixed key lists, the names the door makes and every line of its report; `onlyworlds.kanka` reads it, and its rules stay code. The same bytes are the npm SDK's `src/convert/kanka.json`, read by its `convertKanka`, which writes the same folder; each package's tests hold the file's sha256. Nothing the door writes or prints moves: on the five real Kanka exports and every export its tests make, the folder is the same byte for byte and the report the same text. `MODULE_TYPES`, `PROVEN_MODULES`, `STARTER_WINDOW`, `MADE_NAMES` and the other names keep their values.
- **Each import door tells the names it made.** A name an element gets that is not one string of the export taken as written is now declared and counted, by kind. A joined name is the export's words with the door's between them (a LegendKeeper map document becomes a Map named `<resource>: <document>`); the door's own is a name for a thing the export never names (a frame's corner Marker, `<frame>: corner 1`; an arrow with no label, `Board link`). Each door lists its kinds in `MADE_NAMES`, a tuple of `MadeName` (`kind`, `type`, `pattern`, `source` `joined` or `ours`, and `regex`, read by `MadeName.fits(type, name)`); Notion's is empty. Each `ImportReport` gains `made_names` (kind to count) and prints one line a kind with its count and pattern. Nothing is added to an element. On the doors' real test exports: LegendKeeper's JSON export 80 made names of 280 (17 Narratives and 6 Maps `<resource>: <document>`, 56 frame corners, one nameless pin), its .lk export 78, each Kanka export 2 (`<entity>: hidden details`), World Anvil, Notion and Campfire none.

Expand Down
7 changes: 5 additions & 2 deletions src/onlyworlds/kanka.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@
from collections.abc import Callable, Iterable, Mapping
from contextlib import ExitStack
from dataclasses import dataclass, field
from datetime import datetime
from datetime import UTC, datetime
from html.parser import HTMLParser
from importlib.resources import files
from pathlib import Path
Expand Down Expand Up @@ -357,12 +357,15 @@ def _content(row: Mapping[str, Any]) -> str:


def _when(value: Any) -> datetime | None:
"""A moment, or None when the value is not one. A time with no offset is read as UTC, the zone Kanka
writes, so a campaign's time and an entity's compare whichever of them carries an offset."""
if not isinstance(value, str) or not value:
return None
try:
return datetime.fromisoformat(value.replace("Z", "+00:00"))
moment = datetime.fromisoformat(value.replace("Z", "+00:00"))
except ValueError:
return None
return moment if moment.tzinfo is not None else moment.replace(tzinfo=UTC)


def _int_id(value: Any) -> int | None:
Expand Down
17 changes: 17 additions & 0 deletions tests/test_kanka.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,23 @@ def change(doc: dict[str, Any]) -> None:
assert not any(f.element == "Ada Okafor" for f in report.folded)


def test_a_time_with_no_offset_is_read_as_utc(tmp_path: Path) -> None:
"""A campaign's created_at with no offset beside entities' with one (or the other way) compares, read as UTC."""

def naive(doc: dict[str, Any], holder: str | None = None) -> None:
row = doc[holder] if holder else doc
assert row["created_at"].endswith(".000000Z"), row["created_at"]
row["created_at"] = row["created_at"][: -len(".000000Z")]

campaign = variant(tmp_path, naive, "campaign.json", "naive-campaign.zip")
_, report = run(tmp_path, campaign, "naive-campaign")
assert sorted(report.possibly_starter) == sorted(STARTER)
you = "characters/your-character-name_10074596.json"
entity = variant(tmp_path, lambda d: naive(d, "entity"), you, "naive-entity.zip")
_, report = run(tmp_path, entity, "naive-entity")
assert sorted(report.possibly_starter) == sorted(STARTER)


def test_starter_entries_are_imported_and_named(tmp_path: Path) -> None:
"""Nothing in the data marks them, so they travel; the report names them as possible starters."""
out, report = run(tmp_path)
Expand Down