-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate.py
executable file
·91 lines (64 loc) · 2.16 KB
/
create.py
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
#!/usr/bin/env python3
"""
create.py: create boilerplate for your c++ project
usage: create.py [-h] [options] <path>
options:
-h, --help show this help message and exit
-n <N>, --namespace <N> namespace to use
-p <P>, --parent <P> root of directory path [default: .]
"""
from __future__ import annotations
from pathlib import Path
from sys import argv
from docopt import docopt
template = """\
{include}
{namespace}
"""
def wrap_with(body: str, wrap: tuple[str, str], *, distance: int = 1) -> str:
nl = distance * "\n"
return f"{wrap[0]}{nl}{body}{nl}{wrap[1]}"
def wrap_header(body: str, name: str, suffix: str) -> str:
ext = suffix.replace(".", "_")
guard = f"{name}{ext}".upper()
header = (
f"#ifndef {guard}\n" f"#define {guard}",
f"#endif // {guard}",
)
return wrap_with(body, header, distance=2)
def base_category_name(stem: str) -> str:
return stem.rsplit("_", maxsplit=1)[0]
def get_include(path: Path) -> str:
if path.suffix in [".tpp", ".cpp"]:
return f'#include "{base_category_name(path.stem)}.hpp"'
return ""
def get_nested_namespace(names: tuple[str]) -> str:
begin = "namespace {ns} {{"
end = "}} // namespace {ns}"
begins = "\n".join([begin.format(ns=n) for n in names])
ends = "\n".join([end.format(ns=n) for n in names])
return f"{begins}\n\n{ends}"
def main():
assert __doc__ is not None
args: dict[str, str] = docopt(__doc__)
# print(args)
path = Path(args["<path>"])
name = f"{path.parent.stem}_{path.stem}"
include = get_include(path)
fullpath = Path(__file__).parent / args["--parent"] / args["<path>"]
namespace = get_nested_namespace(
(args["--namespace"],)
if args["--namespace"]
else fullpath.parent.parts[:1]
)
text = template.format(namespace=namespace, include=include)
if path.suffix != ".cpp":
text = wrap_header(text, name, path.suffix)
fullpath.parent.mkdir(parents=True, exist_ok=True)
if not fullpath.exists():
fullpath.write_text(text)
print(text)
else:
print("file already exists")
if __name__ == "__main__":
main()