Skip to content

Commit 6efb109

Browse files
committed
Update _tools/add.py to name files to a more command-line friendl name
Also, add metadata .presentation so that the main README.md can still be populated automatically
1 parent a18c927 commit 6efb109

File tree

2 files changed

+85
-77
lines changed

2 files changed

+85
-77
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
__pycache__/
1+
__pycache__/
2+
.vscode/

_tools/add.py

Lines changed: 83 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from os import listdir, makedirs, rename, linesep
44
from os.path import isdir, exists, split, normpath, join, splitext
55
from urllib.parse import quote
6+
from json import dump, load
67
import sys
78
import re
89
import subprocess
@@ -20,54 +21,31 @@ def shell_call(cmd):
2021

2122

2223
def add_index(readme, category):
23-
readme.write("\n## {}\n\n".format(category).encode())
24+
readme.write("\n## {}\n\n".format(category))
2425
generate_index(readme, category)
2526

2627

2728
def make_readme(readme):
28-
with open('_tools/readme_header.md', mode='rb') as readme_header:
29+
with open('_tools/readme_header.md', mode='r', encoding="utf8") as \
30+
readme_header:
2931
readme.writelines(readme_header.readlines())
30-
readme.write("# Index of Materials\n".encode())
32+
33+
readme.write("# Index of Materials\n")
3134
CATEGORIES = [
3235
"Keynotes",
3336
"Presentations",
34-
"Tutorials",
35-
"Demos",
3637
"Lightning Talks and Lunch Sessions",
3738
"Posters"
3839
]
3940
for category in CATEGORIES:
4041
add_index(readme, category)
4142

4243

43-
def generate_entry(readme, session_name, path):
44-
def get_author_from_filename(path):
45-
author_regex = re.compile(".* - (.*) - CppCon " + str(CPPCON_YEAR) +
46-
"\\.[^.]*$")
47-
48-
author = author_regex.search(path)
49-
50-
if author:
51-
return author.group(1)
52-
53-
return ""
54-
55-
def get_author_from_readme_md(path):
56-
readme_header_regex = re.compile(r"\*\*(.*)\*\* by \*\*(.*)\*\*")
57-
58-
with open(path, mode='rb') as readme_md:
59-
header = readme_md.readline().decode()
60-
match = readme_header_regex.match(header)
61-
62-
if match:
63-
return match.group(2)
64-
65-
return ""
66-
44+
def generate_entry(readme, path):
6745
def md_path(path):
6846
return quote(normpath(path).replace('\\', '/'))
6947

70-
presentation_regex = re.compile("- CppCon " + str(CPPCON_YEAR) +
48+
presentation_regex = re.compile("__cppcon_" + str(CPPCON_YEAR) +
7149
"\\.[^.]*$")
7250
pdf_regex = re.compile("\\.pdf$", flags=re.I)
7351
readme_md_regex = re.compile("README\\.md$")
@@ -81,12 +59,14 @@ def md_path(path):
8159
dir_contents = listdir(path)
8260

8361
for name in dir_contents:
84-
if presentation_regex.search(name):
62+
if name == '.presentation':
63+
continue
64+
elif presentation_regex.search(name):
8565
# Pick the first file we found, but prefer a PDF file if there
8666
# is one
8767
if (not presentation_file) or pdf_regex.search(name):
8868
presentation_file = name
89-
author = get_author_from_filename(name)
69+
# author = get_author_from_filename(name)
9070

9171
all_presentation_files.append(name)
9272
elif readme_md_regex.search(name):
@@ -98,26 +78,31 @@ def md_path(path):
9878
presentation_path = join(path, presentation_file)
9979
else:
10080
presentation_path = path
101-
author = get_author_from_readme_md(join(path, readme_md_file))
81+
# author = get_author_from_readme_md(join(path, readme_md_file))
82+
83+
with open(join(path, '.presentation'), mode="r", encoding="utf8") as f:
84+
presentation_info = load(f)
10285

86+
session_name = presentation_info["Title"]
87+
author = presentation_info["Author"]
10388
readme.write(" - [{}]({}) by {}".format(session_name,
10489
md_path(presentation_path),
105-
author).encode())
90+
author))
10691

10792
if len(all_presentation_files) > 1:
10893
exts = [(splitext(f)[1].lower(), md_path(join(path, f))) for f in
10994
all_presentation_files]
11095
for e in exts:
111-
readme.write(" \\[[{}]({})\\]".format(e[0], e[1]).encode())
96+
readme.write(" \\[[{}]({})\\]".format(e[0], e[1]))
11297

11398
if readme_md_file:
11499
readme.write(" \\[[README]({})\\]".format(
115-
md_path(join(path, readme_md_file))).encode())
100+
md_path(join(path, readme_md_file))))
116101

117102
if all_other_files:
118103
readme.write(" \\[[more materials]({})\\]".format(md_path(path)))
119104

120-
readme.write('\n'.encode())
105+
readme.write('\n')
121106

122107

123108
def generate_index(readme, path):
@@ -131,28 +116,24 @@ def generate_index(readme, path):
131116
for name in dir_contents:
132117
try:
133118
# empty directories or other such issues get skipped over
134-
generate_entry(readme, name, join(path, name))
119+
generate_entry(readme, join(path, name))
135120
except:
136121
pass
137122

138123

139124
def add_presentation(path):
140-
session = ""
141-
while not session or session not in 'pktldo':
142-
session = input("[P]resentation, [K]eynote, [T]utorial, " +
143-
"[L]ighting/Lunch, [D]emo, P[o]ster? ").lower()
144-
145125
SESSION_MAP = {
146-
'p': ('Presentations', 'Presentation'),
147-
'k': ('Keynotes', 'Keynote'),
148-
't': ('Tutorials', 'Tutorial'),
149-
'l': ('Lightning Talks and Lunch Sessions',
150-
'Lightning Talk and Lunch Session'),
151-
'd': ('Demos', 'Demo'),
152-
'o': ('Posters', 'Poster')
126+
'p': 'Presentations',
127+
'k': 'Keynotes',
128+
'l': 'Lightning Talks and Lunch Sessions',
129+
'o': 'Posters'
153130
}
154-
folder = SESSION_MAP[session][0]
155-
session_type = SESSION_MAP[session][1]
131+
132+
session = ""
133+
while not session or session not in SESSION_MAP.keys():
134+
session = input("[P]resentation, [K]eynote, " +
135+
"[L]ighting/Lunch, P[o]ster? ").lower()
136+
folder = SESSION_MAP[session]
156137

157138
filename = split(path)[-1]
158139
ext = splitext(filename)[-1]
@@ -162,8 +143,8 @@ def add_presentation(path):
162143
if ext == '.md':
163144
readme_header_regex = re.compile(r"\*\*(.*)\*\* by \*\*(.*)\*\*")
164145

165-
with open(filename, mode='rb') as readme:
166-
heading = readme.readline().decode()
146+
with open(filename, mode='r', encoding="utf8") as readme:
147+
heading = readme.readline()
167148
match = readme_header_regex.match(heading)
168149

169150
if match:
@@ -188,36 +169,62 @@ def add_presentation(path):
188169
while ok != 'y':
189170
if ok != 'n':
190171
print("\n\nTitle:", title)
191-
print("Author:", author.encode().decode(sys.stdout.encoding,
192-
errors='ignore'))
172+
print("Author:", author)
193173
ok = input('OK? [y/n]: ').lower()
194174

195175
if ok == 'n':
196176
title = input("Title: ")
197177
author = input("Author: ")
198178

199-
if filename != 'README.md':
200-
new_filename = "{} - {} - CppCon {}{}".format(title, author,
201-
CPPCON_YEAR, ext)
202-
else:
203-
new_filename = filename
204-
contents = None
205-
with open(filename, mode='rb') as readme:
206-
contents = readme.readlines()
207-
contents[0] = '**{}** by **{}**'.format(title, author).encode()
208-
with open(filename, mode='wb') as readme:
209-
readme.writelines(contents)
210-
if any((c in "\\/:*?”<>|") for c in new_filename):
211-
print("Name or Author contains invalid characters.")
212-
ok = 'n'
213-
elif ok == 'n':
214-
ok = ''
215-
216-
new_folder = join(folder, title)
179+
file_title = ' '.join(title.lower().split()).replace(' - ', ' ').replace(
180+
' ', '_').replace("c++", "cpp")
181+
file_author = author.lower().replace(' ', '_')
182+
if filename != 'README.md':
183+
new_filename = "{}__{}__cppcon_{}{}".format(file_title, file_author,
184+
CPPCON_YEAR, ext)
185+
ok = ''
186+
while ok != 'y':
187+
print("\n\nFilename:", new_filename)
188+
ok = input("OK? [y/n]: ").lower()
189+
190+
if ok == 'n':
191+
new_filename = input("Filename: ")
192+
new_name, new_ext = splitext(new_filename)
193+
194+
title_author_regex = re.compile("(.*)__(.*)__cppcon_" +
195+
str(CPPCON_YEAR) + r"\.[^.]*$")
196+
197+
title_author_match = title_author_regex.search(new_filename)
198+
if not title_author_match:
199+
print("Cannot parse title and author from new filename")
200+
elif not all((c.isalnum() or c == '_') for c in new_name):
201+
print("Filename contains non-alphanumeric characters. (" +
202+
new_filename + ")")
203+
elif new_ext != ext:
204+
print("New file extension does not match original file" +
205+
"extension")
206+
else:
207+
file_title = title_author_match.group(1)
208+
ok = ''
209+
else:
210+
new_filename = filename
211+
contents = None
212+
with open(filename, mode='r', encoding="utf8") as readme:
213+
contents = readme.readlines()
214+
contents[0] = '**{}** by **{}**'.format(title, author)
215+
with open(filename, mode='w', encoding="utf8") as readme:
216+
readme.writelines(contents)
217+
218+
new_folder = join(folder, file_title)
217219
new_path = join(new_folder, new_filename)
218220
makedirs(new_folder, exist_ok=True)
219221
rename(path, new_path)
220-
shell_call('git add "{}"'.format(new_path))
222+
223+
with open(
224+
join(new_folder, '.presentation'), mode='w', encoding="utf8") as f:
225+
dump({'Title': title, 'Author': author}, f)
226+
227+
shell_call('git add "{}"'.format(new_folder))
221228

222229
return title, author
223230

@@ -232,7 +239,7 @@ def add_presentation(path):
232239
if len(sys.argv) == 2 and sys.argv[1]:
233240
TITLE, AUTHOR = add_presentation(sys.argv[1])
234241

235-
with open('README.md', mode='wb') as readme:
242+
with open('README.md', mode='w', encoding="utf8") as readme:
236243
make_readme(readme)
237244

238245
shell_call('git add README.md')

0 commit comments

Comments
 (0)