Repository navigation
Expand file tree
/
Copy pathcompare_book_config.py
More file actions
75 lines (63 loc) · 1.89 KB
/
Copy pathcompare_book_config.py
File metadata and controls
75 lines (63 loc) · 1.89 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
from __future__ import annotations
import argparse
import json
from pathlib import Path
from book_db import BookDB
from generator import (
canonical_root,
generation_profile,
_legacy_generation_profile,
_profile_differences,
)
def main() -> None:
parser = argparse.ArgumentParser(
description="Compare an existing .nbook with a generator config."
)
parser.add_argument("book")
parser.add_argument("config")
args = parser.parse_args()
with open(args.config, "r", encoding="utf-8") as f:
config = json.load(f)
root_spec = canonical_root(
config["book"].get(
"root",
{"type": "startpos"},
)
)
with BookDB(args.book, readonly=True) as db:
print(f"Book: {args.book}")
print(f"Positions: {db.count_positions():,}")
print(f"Stored root: {db.get_meta('root_spec')}")
print(f"Current root: {root_spec}")
raw = db.get_meta("config_json")
if not raw:
print("No stored config_json metadata is available.")
return
old_config = json.loads(raw)
old_profile = _legacy_generation_profile(
old_config,
db.get_meta("root_spec", root_spec) or root_spec,
)
new_profile = generation_profile(
config,
root_spec,
)
diffs = _profile_differences(
old_profile,
new_profile,
)
if not diffs:
print("Compatible: YES")
print(
"Only safe/non-book-defining settings differ, "
"or the configs are equivalent."
)
return
print("Compatible: NO")
print("Book-defining differences:")
for key, old, new in diffs:
print(
f" {key}: existing={old!r} -> current={new!r}"
)
if __name__ == "__main__":
main()