SPB Git

Repository Public

timeline.py 6.3 KB 109 lines python

1#!/usr/bin/env python32# SP Notes Core3# File: Scripts/recorders/timeline.py4# Description: Records the fixtures of the events journal and of the time machine (API-NEXT §4.4): paging by cursor, kinds,5#              scopes, the states of a page at a date, restoring a history entry (and its 409) — success and errors.6#7# Author: Simon-Pierre Boucher8# Contact: contact@spboucher.ai9import os10import sys11import time12import urllib.parse1314sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))15from spn_recorder import Recorder, new_id  # noqa: E402161718def text(value):19    return {"type": "paragraph", "content": [{"type": "text", "text": value}]}202122def now():23    return time.strftime("%Y-%m-%dT%H:%M:%S", time.gmtime()) + ".%03dZ" % int((time.time() % 1) * 1000)242526def q(value):27    return urllib.parse.quote(value, safe="")282930r = Recorder("timeline")31pages = []323334def page(body):35    made = r.call("POST", "/api/pages", dict({"id": new_id()}, **body)).json36    pages.append(made["id"])37    return made383940before = now()41time.sleep(0.05)42project = page({"title": "Fixture · temps · projet", "section": "projects"})43ref = r.call("POST", "/api/research/references", {"id": new_id(), "title": "Fixture · Time series analysis", "authors": ["Hamilton, James D."], "year": 1994}).json44b = page({"title": "Fixture · temps · données", "parentId": project["id"], "section": "projects",45          "content": {"type": "doc", "content": [text("Première version du texte.")]}})46a = page({"title": "Fixture · temps · journal", "parentId": project["id"], "section": "projects", "content": {"type": "doc", "content": [47    {"type": "paragraph", "content": [{"type": "text", "text": "Voir "}, {"type": "mention", "attrs": {"id": b["id"], "label": "Données"}},48                                      {"type": "text", "text": " et "}, {"type": "citation", "attrs": {"refId": ref["id"], "label": "Hamilton 1994"}},49                                      {"type": "text", "text": "."}]},50    {"type": "callout", "attrs": {"kind": "decision"}, "content": [text("On retient la fréquence quotidienne.")]}]}})51r.call("PUT", "/api/pages/%s/tags" % a["id"], {"tags": ["fixture-temps"]})52task = r.call("POST", "/api/tasks", {"id": new_id(), "title": "Fixture · tâche du journal", "due": "2026-12-05", "pageId": a["id"]}).json53r.call("PATCH", "/api/tasks/" + task["id"], {"status": "done"})54rel = r.call("POST", "/api/graph/relations", {"id": new_id(), "fromId": b["id"], "toId": a["id"], "relType": "supports"}).json55gone = r.call("POST", "/api/graph/relations", {"id": new_id(), "fromId": a["id"], "toId": project["id"], "relType": "part_of"}).json56r.call("PATCH", "/api/pages/" + b["id"], {"title": "Fixture · temps · données v2", "baseVersion": 1})57time.sleep(0.05)58middle = now()59time.sleep(0.05)60r.call("PATCH", "/api/pages/" + b["id"], {"content": {"type": "doc", "content": [text("Deuxième version, plus longue, du texte.")]},61                                          "baseVersion": 2, "versionReason": "manual"})62r.call("DELETE", "/api/graph/relations/" + gone["id"])63r.call("PATCH", "/api/pages/" + a["id"], {"archived": True})64r.call("PATCH", "/api/pages/" + a["id"], {"archived": False})6566# --- events67first = r.call("GET", "/api/events?pageId=%s&limit=3" % a["id"], save="events-page").json68r.call("GET", "/api/events?pageId=%s&limit=3&cursor=%s" % (a["id"], q(first["nextCursor"] or "")), save="events-page-next")69r.call("GET", "/api/events?projectId=%s&limit=200" % project["id"], save="events-project")70r.call("GET", "/api/events?projectId=%s&kinds=relation.*,task.*,page.renamed&limit=50" % project["id"], save="events-project-kinds")71r.call("GET", "/api/events?pageId=%s&from=%s&to=%s" % (b["id"], q(middle), q(now())), save="events-range")72r.call("GET", "/api/events?limit=5", save="events-workspace")73r.call("GET", "/api/events?kinds=page.created,unicorn.seen", save="events-bad-kinds")74r.call("GET", "/api/events?cursor=not-a-cursor", save="events-bad-cursor")75r.call("GET", "/api/events?pageId=%s&projectId=%s" % (a["id"], project["id"]), save="events-both-scopes")76r.call("GET", "/api/events?projectId=%s" % new_id(), save="events-project-not-found")77r.call("GET", "/api/events?from=yesterday", save="events-bad-date")78r.call("GET", "/api/events?limit=0", save="events-bad-limit")7980# --- time machine81r.call("GET", "/api/timemachine?at=%s&pageId=%s" % (q(now()), b["id"]), save="timemachine-page-now")82r.call("GET", "/api/timemachine?at=%s&pageId=%s" % (q(middle), b["id"]), save="timemachine-page-middle")83r.call("GET", "/api/timemachine?at=%s&pageId=%s&content=0" % (q(middle), b["id"]), save="timemachine-page-no-content")84r.call("GET", "/api/timemachine?at=%s&pageId=%s" % (q(before), b["id"]), save="timemachine-page-before")85r.call("GET", "/api/timemachine?at=%s&projectId=%s" % (q(middle), project["id"]), save="timemachine-project-middle")86r.call("GET", "/api/timemachine?at=%s&projectId=%s&content=1" % (q(now()), project["id"]), save="timemachine-project-content")87r.call("GET", "/api/timemachine?pageId=%s" % b["id"], save="timemachine-no-at")88r.call("GET", "/api/timemachine?at=%s" % q(now()), save="timemachine-no-scope")89r.call("GET", "/api/timemachine?at=%s&pageId=%s&projectId=%s" % (q(now()), b["id"], project["id"]), save="timemachine-both-scopes")90r.call("GET", "/api/timemachine?at=%s&pageId=%s" % (q(now()), new_id()), save="timemachine-not-found")9192# --- restore a history entry93versions = r.call("GET", "/api/pages/%s/versions" % b["id"], save="versions").json94entry = versions[-1]95r.call("POST", "/api/pages/%s/versions/%s/restore" % (b["id"], entry["id"]), {"baseVersion": 1}, save="restore-conflict")96r.call("POST", "/api/pages/%s/versions/%s/restore" % (b["id"], entry["id"]), {"baseVersion": 3}, save="restore")97r.call("POST", "/api/pages/%s/versions/%d/restore" % (b["id"], entry["version"]), save="restore-by-number")98r.call("POST", "/api/pages/%s/versions/%s/restore" % (b["id"], new_id()), {}, save="restore-not-found")99r.call("POST", "/api/pages/%s/versions/zz/restore" % b["id"], {}, save="restore-bad-id")100r.call("GET", "/api/timemachine?at=%s&pageId=%s" % (q(now()), b["id"]), save="timemachine-page-after-restore")101102# --- clean up103r.call("DELETE", "/api/graph/relations/" + rel["id"])104r.call("DELETE", "/api/tasks/" + task["id"])105r.call("DELETE", "/api/research/references/" + ref["id"])106r.purge_pages(pages[::-1])107r.call("GET", "/api/events?pageId=%s&limit=5" % b["id"], save="events-purged-page")108r.finish()109
Files main
Menu

Theme