memory-cli 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- memory_cli/__init__.py +1 -0
- memory_cli/__main__.py +49 -0
- memory_cli/capture/__init__.py +7 -0
- memory_cli/capture/codex_rollout.py +164 -0
- memory_cli/capture/event_lines.py +66 -0
- memory_cli/capture/extract.py +458 -0
- memory_cli/capture/state.py +183 -0
- memory_cli/commands/__init__.py +21 -0
- memory_cli/commands/admin.py +363 -0
- memory_cli/commands/common.py +609 -0
- memory_cli/commands/curate.py +1189 -0
- memory_cli/commands/diagnostics.py +126 -0
- memory_cli/commands/edit.py +1282 -0
- memory_cli/commands/hooks.py +730 -0
- memory_cli/commands/import_recovery.py +45 -0
- memory_cli/commands/install.py +152 -0
- memory_cli/commands/integrate.py +480 -0
- memory_cli/commands/memory.py +2152 -0
- memory_cli/commands/read.py +1290 -0
- memory_cli/commands/sources.py +766 -0
- memory_cli/commands/split.py +97 -0
- memory_cli/commands/split_plan.py +196 -0
- memory_cli/commands/split_transaction.py +230 -0
- memory_cli/commands/synthesis.py +1122 -0
- memory_cli/core/__init__.py +16 -0
- memory_cli/core/auto_state.py +30 -0
- memory_cli/core/codex_pass.py +109 -0
- memory_cli/core/config.py +87 -0
- memory_cli/core/file_journal.py +163 -0
- memory_cli/core/headless.py +130 -0
- memory_cli/core/jobs.py +130 -0
- memory_cli/core/resources.py +57 -0
- memory_cli/core/runner.py +398 -0
- memory_cli/core/safe_io.py +155 -0
- memory_cli/core/store_git.py +407 -0
- memory_cli/core/yaml_load.py +14 -0
- memory_cli/corroborate/__init__.py +11 -0
- memory_cli/corroborate/cli.py +267 -0
- memory_cli/corroborate/decide.py +893 -0
- memory_cli/corroborate/digest.py +425 -0
- memory_cli/corroborate/fill.py +744 -0
- memory_cli/corroborate/job.py +15 -0
- memory_cli/corroborate/reword.py +446 -0
- memory_cli/corroborate/run.py +1142 -0
- memory_cli/corroborate/state.py +300 -0
- memory_cli/digest/__init__.py +4 -0
- memory_cli/digest/lessons.py +483 -0
- memory_cli/entry/__init__.py +12 -0
- memory_cli/entry/auto.py +660 -0
- memory_cli/entry/capture.py +1403 -0
- memory_cli/entry/corroborate.py +214 -0
- memory_cli/entry/integrate.py +684 -0
- memory_cli/entry/integrate_self_check.py +203 -0
- memory_cli/entry/research.py +9 -0
- memory_cli/entry/settle.py +614 -0
- memory_cli/entry/split_auto.py +460 -0
- memory_cli/entry/synthesize.py +22 -0
- memory_cli/integrate/__init__.py +9 -0
- memory_cli/integrate/answers.py +42 -0
- memory_cli/integrate/briefs/check-brief.md +16 -0
- memory_cli/integrate/briefs/place-brief.md +26 -0
- memory_cli/integrate/log.py +43 -0
- memory_cli/integrate/plan.py +125 -0
- memory_cli/integrate/rows.py +99 -0
- memory_cli/integrate/state.py +158 -0
- memory_cli/integrate/undo.py +143 -0
- memory_cli/mem.py +1180 -0
- memory_cli/memories/__init__.py +16 -0
- memory_cli/memories/fingerprints.py +51 -0
- memory_cli/memories/journal.py +93 -0
- memory_cli/memories/locations.py +282 -0
- memory_cli/memories/matching.py +451 -0
- memory_cli/memories/model.py +205 -0
- memory_cli/memories/parse.py +237 -0
- memory_cli/memories/records.py +266 -0
- memory_cli/memories/rules.py +291 -0
- memory_cli/memories/sightings.py +314 -0
- memory_cli/memories/terms.py +148 -0
- memory_cli/memories/write.py +358 -0
- memory_cli/notes/__init__.py +13 -0
- memory_cli/notes/check.py +806 -0
- memory_cli/notes/due.py +260 -0
- memory_cli/notes/fields.py +217 -0
- memory_cli/notes/index.py +230 -0
- memory_cli/notes/links.py +170 -0
- memory_cli/notes/note.py +222 -0
- memory_cli/notes/search.py +391 -0
- memory_cli/notes/split_candidates.py +72 -0
- memory_cli/research/__init__.py +5 -0
- memory_cli/research/checks.py +1065 -0
- memory_cli/research/pipeline.py +952 -0
- memory_cli/session/__init__.py +5 -0
- memory_cli/session/handoff.py +139 -0
- memory_cli/session/notices.py +111 -0
- memory_cli/settle/__init__.py +5 -0
- memory_cli/settle/state.py +111 -0
- memory_cli/settle/verdicts.py +693 -0
- memory_cli/split/briefs/checker-brief.md +5 -0
- memory_cli/split/briefs/editor-brief.md +20 -0
- memory_cli/syntheses/__init__.py +8 -0
- memory_cli/syntheses/document.py +400 -0
- memory_cli/syntheses/edit.py +581 -0
- memory_cli/syntheses/project.py +384 -0
- memory_cli/syntheses/split_conservation.py +50 -0
- memory_cli/syntheses/units.py +427 -0
- memory_cli/synthesize/__init__.py +9 -0
- memory_cli/synthesize/backfill.py +431 -0
- memory_cli/synthesize/briefs/backfill-brief.md +26 -0
- memory_cli/synthesize/briefs/checker-brief.md +13 -0
- memory_cli/synthesize/briefs/fixer-brief.md +19 -0
- memory_cli/synthesize/briefs/memory-brief.md +30 -0
- memory_cli/synthesize/briefs/settle-brief.md +32 -0
- memory_cli/synthesize/briefs/verdict-brief.md +50 -0
- memory_cli/synthesize/briefs/web-brief.md +33 -0
- memory_cli/synthesize/cli.py +311 -0
- memory_cli/synthesize/pipeline.py +764 -0
- memory_cli/synthesize/references.py +164 -0
- memory_cli/synthesize/run.py +150 -0
- memory_cli/synthesize/topics.py +528 -0
- memory_cli/templates/knowledge.md +23 -0
- memory_cli/templates/literature.md +25 -0
- memory_cli/templates/research.md +23 -0
- memory_cli/templates/synthesis.md +6 -0
- memory_cli-0.1.0.dist-info/METADATA +102 -0
- memory_cli-0.1.0.dist-info/RECORD +128 -0
- memory_cli-0.1.0.dist-info/WHEEL +4 -0
- memory_cli-0.1.0.dist-info/entry_points.txt +9 -0
- memory_cli-0.1.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,363 @@
|
|
|
1
|
+
"""The `terms`, `projects`, `init` and `history` handlers (A5, ADR 0028): store and project administration."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import contextlib
|
|
6
|
+
import datetime
|
|
7
|
+
import os
|
|
8
|
+
import shutil
|
|
9
|
+
import sys
|
|
10
|
+
import time
|
|
11
|
+
from pathlib import Path
|
|
12
|
+
from typing import TYPE_CHECKING, cast
|
|
13
|
+
|
|
14
|
+
from memory_cli.commands import common
|
|
15
|
+
from memory_cli.core import auto_state, safe_io, store_git
|
|
16
|
+
from memory_cli.memories import records
|
|
17
|
+
from memory_cli.syntheses import document
|
|
18
|
+
|
|
19
|
+
if TYPE_CHECKING:
|
|
20
|
+
import argparse
|
|
21
|
+
from collections.abc import Callable, Sequence
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
_INIT_ALLOWED = {
|
|
25
|
+
"mem-store.yaml",
|
|
26
|
+
"topics.yaml",
|
|
27
|
+
"literature",
|
|
28
|
+
"knowledge",
|
|
29
|
+
"research",
|
|
30
|
+
"memories",
|
|
31
|
+
"templates",
|
|
32
|
+
"syntheses",
|
|
33
|
+
".git",
|
|
34
|
+
".gitignore",
|
|
35
|
+
"README.md",
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def cmd_init(args: argparse.Namespace) -> int:
|
|
40
|
+
if args.folder is None:
|
|
41
|
+
default = records.default_store(os.environ)
|
|
42
|
+
if default is None:
|
|
43
|
+
print(
|
|
44
|
+
"mem: no folder given and neither XDG_DATA_HOME nor HOME is set.", file=sys.stderr
|
|
45
|
+
)
|
|
46
|
+
return 2
|
|
47
|
+
args.folder = str(default)
|
|
48
|
+
code = _init_store(Path(args.folder).expanduser())
|
|
49
|
+
if code == 0:
|
|
50
|
+
# the list is made again by the first global write
|
|
51
|
+
with contextlib.suppress(OSError):
|
|
52
|
+
records.ensure_terms_file(os.environ)
|
|
53
|
+
return code
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def _history_store(args: argparse.Namespace) -> Path | None:
|
|
57
|
+
root = common.resolve_root(args.root, os.environ)
|
|
58
|
+
if root is None or not root.is_dir():
|
|
59
|
+
print(
|
|
60
|
+
common.no_store_message(os.environ)
|
|
61
|
+
if root is None
|
|
62
|
+
else f"mem: the store folder does not exist: {root}",
|
|
63
|
+
file=sys.stderr,
|
|
64
|
+
)
|
|
65
|
+
return None
|
|
66
|
+
return root
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def _history_init_folder(args: argparse.Namespace) -> Path | None:
|
|
70
|
+
"""The store folder `mem history init` turns into a repository; None after a message on stderr."""
|
|
71
|
+
folder = (
|
|
72
|
+
Path(args.folder).expanduser()
|
|
73
|
+
if args.folder
|
|
74
|
+
else common.resolve_root(args.root, os.environ)
|
|
75
|
+
)
|
|
76
|
+
if folder is None:
|
|
77
|
+
print(common.no_store_message(os.environ), file=sys.stderr)
|
|
78
|
+
return None
|
|
79
|
+
if not folder.is_dir():
|
|
80
|
+
print(f"mem: the store folder does not exist: {folder}", file=sys.stderr)
|
|
81
|
+
return None
|
|
82
|
+
if not records.store_is_writable(folder):
|
|
83
|
+
print(
|
|
84
|
+
f'mem: {folder} is read-only: it has no mem-store.yaml (create a store with "mem init").',
|
|
85
|
+
file=sys.stderr,
|
|
86
|
+
)
|
|
87
|
+
return None
|
|
88
|
+
if store_git.is_repo(folder):
|
|
89
|
+
print(f"mem: {folder} is a git repository already", file=sys.stderr)
|
|
90
|
+
return None
|
|
91
|
+
return folder
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def _history_init(args: argparse.Namespace) -> int:
|
|
95
|
+
folder = _history_init_folder(args)
|
|
96
|
+
if folder is None:
|
|
97
|
+
return 2
|
|
98
|
+
try:
|
|
99
|
+
print(store_git.init(folder) or "nothing to commit")
|
|
100
|
+
except store_git.GitError as exc:
|
|
101
|
+
print(f"mem: history: {exc}", file=sys.stderr)
|
|
102
|
+
return 1
|
|
103
|
+
return 0
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def _history_repo(args: argparse.Namespace, hook: bool) -> Path | None:
|
|
107
|
+
"""The store when it is a git repository; None otherwise, with a message unless `hook`."""
|
|
108
|
+
store = _history_store(args) if not hook else common.resolve_root(args.root, os.environ)
|
|
109
|
+
if store is None or not store.is_dir():
|
|
110
|
+
return None
|
|
111
|
+
if not store_git.is_repo(store):
|
|
112
|
+
if not hook:
|
|
113
|
+
print(f'mem: {store} is not a git repository (run "mem history init")', file=sys.stderr)
|
|
114
|
+
return None
|
|
115
|
+
return store
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def _history_message(args: argparse.Namespace, hook: bool) -> str | Callable[[int], str]:
|
|
119
|
+
"""The commit message: `-m`, or a line stamped with the time of the call."""
|
|
120
|
+
if args.message:
|
|
121
|
+
return args.message
|
|
122
|
+
now = datetime.datetime.now()
|
|
123
|
+
label = "session end" if hook else "manual commit"
|
|
124
|
+
return lambda n: f"{label} {now:%Y-%m-%d %H:%M}: {n} files"
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
def _history_commit(args: argparse.Namespace, store: Path, hook: bool, started: float) -> int:
|
|
128
|
+
detached = hook and args.detached
|
|
129
|
+
if detached:
|
|
130
|
+
common._log_hook({"hook": "session-end", "phase": "started"})
|
|
131
|
+
if hook and auto_state.run_lock_held(common.data_dir(os.environ), store):
|
|
132
|
+
if detached:
|
|
133
|
+
common._log_hook({
|
|
134
|
+
"hook": "session-end",
|
|
135
|
+
"phase": "done",
|
|
136
|
+
"ms": int((time.monotonic() - started) * 1000),
|
|
137
|
+
"committed": False,
|
|
138
|
+
"failed": False,
|
|
139
|
+
})
|
|
140
|
+
return 0 # an automatic run holds the store: it commits its own work
|
|
141
|
+
message = _history_message(args, hook)
|
|
142
|
+
held: list[str] = []
|
|
143
|
+
commit, failed = common._commit_points(
|
|
144
|
+
store, message, quiet=hook, held=held
|
|
145
|
+
) # the hook is silent; a failure is in the usage log
|
|
146
|
+
if hook:
|
|
147
|
+
common._log_hook({
|
|
148
|
+
"hook": "session-end",
|
|
149
|
+
**({"phase": "done"} if detached else {}),
|
|
150
|
+
"ms": int((time.monotonic() - started) * 1000),
|
|
151
|
+
"committed": commit is not None,
|
|
152
|
+
"failed": failed,
|
|
153
|
+
})
|
|
154
|
+
return 0
|
|
155
|
+
print(commit or "nothing to commit")
|
|
156
|
+
for path in [] if failed else held:
|
|
157
|
+
print(f"held back: {path}", file=sys.stderr)
|
|
158
|
+
return 1 if failed else 0
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
def _history_log(args: argparse.Namespace, store: Path) -> int:
|
|
162
|
+
try:
|
|
163
|
+
print("\n".join(store_git.log(store, args.n)))
|
|
164
|
+
except store_git.GitError as exc:
|
|
165
|
+
print(f"mem: history: {exc}", file=sys.stderr)
|
|
166
|
+
return 1
|
|
167
|
+
return 0
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
def cmd_history(args: argparse.Namespace) -> int:
|
|
171
|
+
started = time.monotonic()
|
|
172
|
+
hook = args.history_command == "commit" and args.hook
|
|
173
|
+
if args.history_command == "init":
|
|
174
|
+
return _history_init(args)
|
|
175
|
+
store = _history_repo(args, hook)
|
|
176
|
+
if store is None:
|
|
177
|
+
return 0 if hook else 2
|
|
178
|
+
if args.history_command == "commit":
|
|
179
|
+
return _history_commit(args, store, hook, started)
|
|
180
|
+
return _history_log(args, store)
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
def _init_store_check(folder: Path) -> int | None:
|
|
184
|
+
"""The exit code when `folder` cannot become a store, or already is one; None when it can be created."""
|
|
185
|
+
if folder.exists() and not folder.is_dir():
|
|
186
|
+
print(f"mem: {folder} is not a folder", file=sys.stderr)
|
|
187
|
+
return 2
|
|
188
|
+
if folder.is_dir() and any(p.name not in _INIT_ALLOWED for p in folder.iterdir()):
|
|
189
|
+
print(f"mem: {folder} is not empty and is not a store", file=sys.stderr)
|
|
190
|
+
return 2
|
|
191
|
+
if (folder / records.STORE_MARKER).is_file():
|
|
192
|
+
print(folder)
|
|
193
|
+
return 0
|
|
194
|
+
if any((folder / n).exists() for n in ("literature", "knowledge", "research", "topics.yaml")):
|
|
195
|
+
print(
|
|
196
|
+
f"mem: {folder} already holds a store without mem-store.yaml; it stays read-only "
|
|
197
|
+
"(add the file by hand only if you own it and mean to write to it).",
|
|
198
|
+
file=sys.stderr,
|
|
199
|
+
)
|
|
200
|
+
return 2
|
|
201
|
+
return None
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
def _init_store_create(folder: Path) -> None:
|
|
205
|
+
"""Make the folders, the templates and the two files of a new store; what exists stays."""
|
|
206
|
+
folder.mkdir(parents=True, exist_ok=True)
|
|
207
|
+
for name in (
|
|
208
|
+
"literature",
|
|
209
|
+
"knowledge",
|
|
210
|
+
"research",
|
|
211
|
+
records.GLOBAL_DIR,
|
|
212
|
+
document.SYNTHESES_DIR,
|
|
213
|
+
):
|
|
214
|
+
(folder / name).mkdir(exist_ok=True)
|
|
215
|
+
if not (folder / "templates").exists():
|
|
216
|
+
shutil.copytree(common._TEMPLATES, folder / "templates")
|
|
217
|
+
for name, text in (
|
|
218
|
+
(records.STORE_MARKER, "format: 1\n"),
|
|
219
|
+
("topics.yaml", "notes: {description: General notes}\n"),
|
|
220
|
+
):
|
|
221
|
+
if not (folder / name).exists():
|
|
222
|
+
safe_io.replace_file(folder / name, text.encode("utf-8"))
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
def _init_store(folder: Path) -> int:
|
|
226
|
+
code = _init_store_check(folder)
|
|
227
|
+
if code is not None:
|
|
228
|
+
return code
|
|
229
|
+
try:
|
|
230
|
+
_init_store_create(folder)
|
|
231
|
+
except OSError as exc:
|
|
232
|
+
print(f"mem: {exc}", file=sys.stderr)
|
|
233
|
+
return 2
|
|
234
|
+
print(folder)
|
|
235
|
+
return 0
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
def _term_class(term: records.Term) -> str:
|
|
239
|
+
return "hard" if not term.soft else f"soft[{term.kind}]" if term.kind else "soft"
|
|
240
|
+
|
|
241
|
+
|
|
242
|
+
def _projects_list(_args: argparse.Namespace, data: Path) -> int:
|
|
243
|
+
today = datetime.date.today()
|
|
244
|
+
for name, entry in sorted(records.read_registry(data).items()):
|
|
245
|
+
path = Path(entry["path"])
|
|
246
|
+
if not path.is_dir():
|
|
247
|
+
print(f"{name}\tmissing\t{path}")
|
|
248
|
+
continue
|
|
249
|
+
live = [
|
|
250
|
+
m
|
|
251
|
+
for m in records.load_memories(records.project_dir(path), "project")
|
|
252
|
+
if records.is_live(m, today)
|
|
253
|
+
]
|
|
254
|
+
print(f"{name}\t{len(live)} memories\t{path}")
|
|
255
|
+
return 0
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
def _projects_add(args: argparse.Namespace, data: Path) -> int:
|
|
259
|
+
path = Path(args.path).expanduser().resolve()
|
|
260
|
+
if not path.is_dir():
|
|
261
|
+
print(f"mem: {path} is not a folder.", file=sys.stderr)
|
|
262
|
+
return 2
|
|
263
|
+
name = records.register_project(data, path, datetime.date.today(), args.name)
|
|
264
|
+
print(f"{name}\t{path}")
|
|
265
|
+
return 0
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
def _projects_remove(args: argparse.Namespace, data: Path) -> int:
|
|
269
|
+
if not records.forget_project(data, args.name):
|
|
270
|
+
print(f"mem: no project named {args.name} (see mem projects list)", file=sys.stderr)
|
|
271
|
+
return 2
|
|
272
|
+
return 0
|
|
273
|
+
|
|
274
|
+
|
|
275
|
+
_PROJECTS_HANDLERS: dict[str, Callable[[argparse.Namespace, Path], int]] = {
|
|
276
|
+
"list": _projects_list,
|
|
277
|
+
"add": _projects_add,
|
|
278
|
+
} # any other subcommand is `remove`
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
def cmd_projects(args: argparse.Namespace) -> int:
|
|
282
|
+
data = common.data_dir(os.environ)
|
|
283
|
+
if data is None:
|
|
284
|
+
print(
|
|
285
|
+
"mem: no data folder is set (MEM_DATA, XDG_STATE_HOME or HOME), so there is no registry.",
|
|
286
|
+
file=sys.stderr,
|
|
287
|
+
)
|
|
288
|
+
return 2
|
|
289
|
+
try:
|
|
290
|
+
return _PROJECTS_HANDLERS.get(args.projects_command, _projects_remove)(args, data)
|
|
291
|
+
except (ValueError, OSError) as exc:
|
|
292
|
+
print(f"mem: {exc}", file=sys.stderr)
|
|
293
|
+
return 2
|
|
294
|
+
|
|
295
|
+
|
|
296
|
+
def _terms_list(_args: argparse.Namespace, _path: Path, terms: Sequence[records.Term]) -> int:
|
|
297
|
+
for t in terms:
|
|
298
|
+
print(f"{_term_class(t)}\t{t.text}")
|
|
299
|
+
return 0
|
|
300
|
+
|
|
301
|
+
|
|
302
|
+
def _terms_add(args: argparse.Namespace, path: Path, terms: Sequence[records.Term]) -> int:
|
|
303
|
+
text = " ".join(args.term.split())
|
|
304
|
+
if args.kind and not args.soft:
|
|
305
|
+
raise common.UsageError("--kind needs --soft")
|
|
306
|
+
if (
|
|
307
|
+
not text
|
|
308
|
+
or text.startswith("#")
|
|
309
|
+
or cast("records.Term", records.parse_term(text)).soft # not blank, not a comment
|
|
310
|
+
):
|
|
311
|
+
print("mem: a term cannot be empty, start with # or start with soft:", file=sys.stderr)
|
|
312
|
+
return 2
|
|
313
|
+
line = (
|
|
314
|
+
f"soft[{args.kind}]: {text}"
|
|
315
|
+
if args.soft and args.kind
|
|
316
|
+
else f"soft: {text}"
|
|
317
|
+
if args.soft
|
|
318
|
+
else text
|
|
319
|
+
)
|
|
320
|
+
new = cast("records.Term", records.parse_term(line)) # `line` is neither blank nor a comment
|
|
321
|
+
if any((t.text, t.soft, t.kind) == (new.text, new.soft, new.kind) for t in terms):
|
|
322
|
+
print("already listed")
|
|
323
|
+
return 0
|
|
324
|
+
records.ensure_terms_file(os.environ)
|
|
325
|
+
existing = path.read_text(encoding="utf-8")
|
|
326
|
+
with path.open("a", encoding="utf-8") as f:
|
|
327
|
+
f.write(("" if not existing or existing.endswith("\n") else "\n") + line + "\n")
|
|
328
|
+
print(f"{_term_class(new)}\t{text}")
|
|
329
|
+
return 0
|
|
330
|
+
|
|
331
|
+
|
|
332
|
+
def _terms_remove(args: argparse.Namespace, path: Path, _terms: Sequence[records.Term]) -> int:
|
|
333
|
+
text = " ".join(args.term.split())
|
|
334
|
+
lines = path.read_text(encoding="utf-8-sig").splitlines() if path.exists() else []
|
|
335
|
+
kept = [ln for ln in lines if (t := records.parse_term(ln)) is None or t.text != text]
|
|
336
|
+
if len(kept) == len(lines):
|
|
337
|
+
print(f"mem: no term {text!r} in the list.", file=sys.stderr)
|
|
338
|
+
return 2
|
|
339
|
+
path.write_text("".join(ln + "\n" for ln in kept), encoding="utf-8")
|
|
340
|
+
print(f"removed {len(lines) - len(kept)}")
|
|
341
|
+
return 0
|
|
342
|
+
|
|
343
|
+
|
|
344
|
+
_TERMS_HANDLERS: dict[str, Callable[[argparse.Namespace, Path, Sequence[records.Term]], int]] = {
|
|
345
|
+
"list": _terms_list,
|
|
346
|
+
"add": _terms_add,
|
|
347
|
+
} # any other subcommand is `remove`
|
|
348
|
+
|
|
349
|
+
|
|
350
|
+
def cmd_terms(args: argparse.Namespace) -> int:
|
|
351
|
+
path = records.forbidden_file(os.environ)
|
|
352
|
+
if path is None:
|
|
353
|
+
print(
|
|
354
|
+
"mem: neither XDG_CONFIG_HOME nor HOME is set: the term list has no place.",
|
|
355
|
+
file=sys.stderr,
|
|
356
|
+
)
|
|
357
|
+
return 2
|
|
358
|
+
try:
|
|
359
|
+
terms = records.read_terms(os.environ)
|
|
360
|
+
return _TERMS_HANDLERS.get(args.terms_command, _terms_remove)(args, path, terms)
|
|
361
|
+
except (records.ForbiddenListError, OSError) as exc:
|
|
362
|
+
print(f"mem: the forbidden-terms list cannot be used ({exc}).", file=sys.stderr)
|
|
363
|
+
return 2
|