Skip to content

Commit 34a9a7b

Browse files
authored
Merge pull request #13 from effigies/enh/renamer
ENH: Add template renaming script
2 parents 843d381 + ed5f9ba commit 34a9a7b

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ dmypy.json
131131
# Pycharm
132132
.idea
133133

134+
# Vim
135+
.*.sw[op]
136+
134137
# VS Code
135138
.vscode
136139

tools/rename_template.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/env python3
2+
import sys
3+
import os
4+
import re
5+
import fnmatch
6+
import functools
7+
from pathlib import Path
8+
9+
PACKAGE_ROOT = Path(__file__).absolute().parent.parent
10+
11+
@functools.lru_cache()
12+
def load_gitignore(repo):
13+
gitignore = repo / '.gitignore'
14+
ignore = [fnmatch.translate(".git/"), fnmatch.translate(Path(__file__).name)]
15+
if gitignore.exists():
16+
ignore.extend(
17+
fnmatch.translate(line.strip())
18+
for line in gitignore.read_text().splitlines()
19+
if line.strip() and not line[0] == '#'
20+
)
21+
return re.compile('|'.join(ignore))
22+
23+
cmd, new_name, *_ = sys.argv
24+
25+
for root, dirs, files in os.walk(PACKAGE_ROOT):
26+
ignore = load_gitignore(PACKAGE_ROOT).search
27+
for d in [d for d in dirs if ignore(f"{d}/")]:
28+
dirs.remove(d)
29+
for f in [f for f in files if ignore(f)]:
30+
files.remove(f)
31+
32+
root = Path(root)
33+
for src in list(dirs):
34+
if 'TODO' in src:
35+
dst = src.replace("TODO", new_name)
36+
print(f"Renaming: {root / src} -> {root / dst}")
37+
os.rename(root / src, root / dst)
38+
dirs.remove(src)
39+
dirs.append(dst)
40+
for fname in files:
41+
f = root / fname
42+
text = Path.read_text(root / fname)
43+
if 'TODO' in text:
44+
print(f"Rewriting: {root / fname}")
45+
Path.write_text(root / fname, text.replace("TODO", new_name))

0 commit comments

Comments
 (0)