-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbookmarks.py
More file actions
205 lines (182 loc) · 7.88 KB
/
Copy pathbookmarks.py
File metadata and controls
205 lines (182 loc) · 7.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
"""
Per-user investigation bookmarks: pinned log lines with notes.
Each bookmark records enough to redisplay and jump back to a line
(source basename, line index, timestamp, a text snippet) plus the user's note
and the workspace context it was taken in. Stored per user as a small JSON file
under the data root, so notes survive across sessions and logins.
"""
from __future__ import annotations
import json
import os
import re
import threading
import time
import uuid
_SLUG_RE = re.compile(r"[^A-Za-z0-9._-]+")
def _user_slug(name: str) -> str:
slug = _SLUG_RE.sub("-", (name or "").strip()).strip("-._")
return slug[:80] or "user"
class BookmarkStore:
def __init__(self, data_root: str):
self.data_root = os.path.realpath(data_root)
self._lock = threading.RLock()
def _path(self, username: str) -> str:
udir = os.path.join(self.data_root, _user_slug(username))
os.makedirs(udir, exist_ok=True)
return os.path.join(udir, "bookmarks.json")
def _load(self, username: str) -> list:
path = self._path(username)
if not os.path.isfile(path):
return []
try:
with open(path, "r", encoding="utf-8") as fh:
data = json.load(fh)
except (OSError, json.JSONDecodeError):
return []
items = data.get("bookmarks") if isinstance(data, dict) else None
return items if isinstance(items, list) else []
def _save(self, username: str, items: list) -> None:
with open(self._path(username), "w", encoding="utf-8") as fh:
json.dump({"bookmarks": items}, fh, indent=2)
def list(self, username: str, scope=None) -> list:
with self._lock:
items = self._load(username)
if scope is not None:
items = [b for b in items if b.get("scope", "") == scope]
items.sort(key=lambda b: b.get("created_at", ""), reverse=True)
return items
def keys(self, username: str, scope=None) -> set:
"""Set of (source, seq) identifying pinned lines — including every line
of a range bookmark — for marking the view."""
out = set()
for b in self.list(username, scope):
try:
out.add((b.get("source", ""), int(b.get("seq", -1))))
except (TypeError, ValueError):
pass
for m in (b.get("members") or []):
try:
out.add((m.get("source", ""), int(m.get("seq", -1))))
except (TypeError, ValueError):
pass
return out
def add(self, username: str, bm: dict) -> dict:
"""Add a bookmark, or update the note if this exact line is already
pinned within the same scope (log folder / workspace).
Identity includes the unique ``srcid`` so two different sources that
share a basename/display name (e.g. several capture sessions' own
``wifiCtrl.txt``) never collapse into one bookmark. Legacy notes with
no ``srcid`` (``None``) still upsert against each other by
(scope, source, seq)."""
with self._lock:
items = self._load(username)
scope = bm.get("scope", "")
new_srcid = bm.get("srcid")
try:
seq_val = int(bm.get("seq", -1))
except (TypeError, ValueError):
seq_val = -1
key = ((scope, new_srcid, bm.get("source", ""), seq_val)
if seq_val >= 0 else None)
if key is not None:
for b in items:
try:
bkey = (b.get("scope", ""), b.get("srcid"),
b.get("source", ""), int(b.get("seq", -1)))
except (TypeError, ValueError):
continue
if bkey == key:
b["note"] = bm.get("note", b.get("note", ""))
b["text"] = bm.get("text", b.get("text", ""))
if "srcid" in bm:
b["srcid"] = bm["srcid"]
if "members" in bm:
b["members"] = bm["members"]
self._save(username, items)
return b
rec = dict(bm)
rec["id"] = uuid.uuid4().hex
rec["created_at"] = time.strftime("%Y-%m-%dT%H:%M:%S")
items.append(rec)
self._save(username, items)
return rec
def get_by_line(self, username: str, source: str, seq: int) -> dict | None:
for b in self.list(username):
try:
if b.get("source", "") == source and int(b.get("seq", -1)) == seq:
return b
except (TypeError, ValueError):
continue
return None
def update(self, username: str, bid: str, note: str) -> dict | None:
with self._lock:
items = self._load(username)
for b in items:
if b.get("id") == bid:
b["note"] = note
self._save(username, items)
return b
return None
def delete(self, username: str, bid: str) -> None:
with self._lock:
items = self._load(username)
kept = [b for b in items if b.get("id") != bid]
if len(kept) != len(items):
self._save(username, kept)
def delete_by_line(self, username: str, source: str, seq: int,
scope=None, srcid=None) -> bool:
with self._lock:
items = self._load(username)
kept = []
removed = False
for b in items:
try:
same = (b.get("source", "") == source
and int(b.get("seq", -1)) == seq)
except (TypeError, ValueError):
same = False
if same and scope is not None and b.get("scope", "") != scope:
same = False
# When a unique source_id is supplied, only remove the matching
# source so sibling sessions sharing a basename are left intact.
if same and srcid is not None and b.get("srcid") != srcid:
same = False
if same:
removed = True
else:
kept.append(b)
if removed:
self._save(username, kept)
return removed
def rescope(self, username: str, old_scope: str, new_scope: str) -> int:
"""Move a user's bookmarks from ``old_scope`` to ``new_scope``.
Used when a fresh upload's notes (keyed by an ``up:<hash>`` scope) must
follow the logs into a newly saved workspace (``ws:<name>``) so they are
not orphaned when the workspace is later reloaded. A bookmark whose line
is already pinned in the destination scope is dropped as a duplicate.
Returns the number of bookmarks moved.
"""
if not old_scope or old_scope == new_scope:
return 0
with self._lock:
items = self._load(username)
def ident(b):
try:
seq = int(b.get("seq", -1))
except (TypeError, ValueError):
seq = -1
return (b.get("srcid"), b.get("source", ""), seq)
existing = {ident(b) for b in items if b.get("scope", "") == new_scope}
kept = []
moved = 0
for b in items:
if b.get("scope", "") == old_scope:
if ident(b) in existing:
continue # destination already pins this line — drop dup
b["scope"] = new_scope
existing.add(ident(b))
moved += 1
kept.append(b)
if moved or len(kept) != len(items):
self._save(username, kept)
return moved