-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_snippets.py
More file actions
50 lines (40 loc) · 1.51 KB
/
Copy pathgenerate_snippets.py
File metadata and controls
50 lines (40 loc) · 1.51 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
import json
import sys
from pathlib import Path
snippets = {}
root = Path(__file__).resolve().parent
sources = [
{"dir": root / "io", "pattern": "*.cpp", "name_prefix": "", "use_relative_name": False},
{"dir": root / "template", "pattern": "*.cpp", "name_prefix": "", "use_relative_name": False},
{
"dir": root / "third_party" / "ac-library" / "atcoder",
"pattern": "*.hpp",
"name_prefix": "acl_",
"use_relative_name": True,
},
]
for source in sources:
source_dir = source["dir"]
if not source_dir.is_dir():
continue
for path in sorted(source_dir.rglob(source["pattern"])):
relative = path.relative_to(root)
if any(part.startswith(".") for part in relative.parts):
continue
if source["use_relative_name"]:
local_name = "_".join(path.relative_to(source_dir).with_suffix("").parts).replace("-", "_")
name = f'{source["name_prefix"]}{local_name}'
else:
name = f'{source["name_prefix"]}{path.stem}'
if name in snippets:
print(f"error: duplicate snippet {name}", file=sys.stderr)
raise SystemExit(1)
snippet = {
"prefix": name,
"body": path.read_text(encoding="utf-8").splitlines(),
"description": name,
}
snippets[name] = snippet
print(f"generated snippet {name}", file=sys.stderr)
(root / "cpp.json").write_text(json.dumps(snippets, indent=2), encoding="utf-8")
print("done", file=sys.stderr)