-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathdatebump.py
316 lines (281 loc) · 9.48 KB
/
datebump.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
from datetime import date, timedelta
from pprint import pprint
from sortedcontainers import SortedDict
import os, re, shutil, os
from pick import Picker, pick
###
# Defaults
###
use_defaults = True # Otherwise, prompt for inputs
default_start = date(2017,5,17)
default_end = date(2017,6,22)
default_dow_dict = {
0 : {
"meets" : True,
"name" : "Monday"
},
1 : {
"meets" : True,
"name" : "Tuesday"
},
2 : {
"meets" : True,
"name" : "Wedneday"
},
3 : {
"meets" : True,
"name" : "Thursday"
},
4 : {
"meets" : False,
"name" : "Friday"
},
5 : {
"meets" : False,
"name" : "Saturday"
},
6 : {
"meets" : False,
"name" : "Sunday"
}
}
holiday_list = [
{
"date" : date(2017,5,29),
"name" : "Memorial Day"
},
{
"date" : date(2017,6,1),
"name" : "No Class"
}
]
unpublish_default = "y"
###
# Setup
###
default_year = None
no_choices =["n", "N" "no", "No"]
yes_choices = ["y", "Y", "yes", "Yes"]
allowed_choices = no_choices + yes_choices
prior_month = None
dow = None
sessions = SortedDict({})
oldposts = SortedDict({})
num_sessions = 0
def get_valid_num(text, default = None):
valid_num = False
if default != None:
text = text + " (Press enter for {0}) ".format(default)
while not valid_num:
try:
num = input(text)
if (default != None) and (num == ""):
num = default
elif type(num) != type(1):
num = int(num)
valid_num = True
except ValueError:
print("Please enter a valid number")
return num
def get_date(text):
global default_year, prior_month
print("Now collecting " + text)
# Get Year
if default_year == None:
choice = ""
while choice not in allowed_choices:
choice = input("Would you like to set a default year? (y/n) ")
if choice in no_choices:
default_year = False
elif choice in yes_choices:
year_set = False
while not year_set:
try:
default_year = get_valid_num("Enter default year: ", date.today().year)
if default_year // 2000 != 1:
raise ValueError
year = default_year
year_set = True
except ValueError:
print("Please enter a valid 4 digit year")
default_year = None
continue
elif default_year == False:
year = get_valid_num("Enter year: ")
else:
year = default_year
# Get month
month = get_valid_num("Enter month: ", prior_month if prior_month else None)
prior_month = month
# Get day
day = get_valid_num("Enter day: ")
return date(year, month, day)
def get_dow():
dowdict = {}
for i, day in enumerate(["Monday", "Tuesday", "Wedneday", "Thursday", "Friday", "Saturday", "Sunday"]):
answer = ""
daydict = {}
while answer not in allowed_choices:
answer = input("Does your class meet {0}? ".format(day))
daydict["meets"] = False if answer in no_choices else True
daydict["name"] = day
dowdict[i] = daydict.copy()
return dowdict
def get_dates():
global sessions, num_sessions
print("Determining dates of new class...")
# Start date
if use_defaults:
start_date = default_start
else:
start_date = get_date("Class Start Date")
print(start_date)
# End Date
if use_defaults:
end_date = default_end
else:
end_date = get_date("Class End Date")
# Days of week class meets
if use_defaults:
dow = default_dow_dict
else:
print("What days of the week does your class meet? ")
dow = get_dow()
# Build dict of all days
delta = end_date - start_date
for i in range(delta.days + 1):
day_date = start_date + timedelta(days=i)
meets = dow[day_date.weekday()]["meets"]
if meets:
num_sessions += 1
sessions[str(day_date)] = {
"date" : day_date,
"meets" : meets,
"holiday" : False,
"nth_class" : num_sessions
}
pprint(sessions)
# Subtract any holidays
answer = ""
holiday_index = 0
while answer not in no_choices:
print("Current Holidays:")
for date in sessions:
try:
if sessions[date]["holiday"]:
print(date, sessions[date])
except KeyError:
pass
if use_defaults and len(holiday_list) > holiday_index:
answer = "y"
elif use_defaults:
answer = "n"
else:
answer = input("Any other holidays or days your class won't meet? ")
if answer in yes_choices:
# Collect holidays
if use_defaults:
holiday = holiday_list[holiday_index]["date"]
holiday_name = holiday_list[holiday_index]["name"]
else:
holiday = get_date("Holiday")
holiday_name = input("What is the name of the holiday? ")
try:
sessions[str(holiday)]["meets"] = False
sessions[str(holiday)]["holiday"] = True
sessions[str(holiday)]["holiday_name"] = holiday_name
except KeyError:
print("Holiday not during class period. Try Again.")
holiday_index += 1
print("Your final class schedule:")
pprint(sessions)
print("There are {0} sessions in your schedule".format(num_sessions))
output_dir = input("Select an output dir without slash (suggest using a new dir): ")
os.system("mkdir {0}".format(output_dir))
###
# Current Posts
###
# Get list of filenames as keys to dict of dicts
folder = input("Enter the folder where your posts are located (Usually _posts): ")
for folderName, subfolders, filenames in os.walk(folder):
print('The current folder is ' + folderName)
for filename in filenames:
post_location = folderName + '/'+ filename
post_date = filename[:10]
print('Found post: ' + post_location)
with open(post_location) as f:
post_text = f.read()
is_published = True if re.search(r'---.*?published\: false.*?---', post_text, flags=re.S) == None else False
title_match = re.search(r'---.*?title\: (.*?)$.*?---', post_text, flags = re.S | re.M)
if title_match == None:
title = filename[:-3]
else:
title = title_match.group(1)
oldposts[post_date] = {
"location" : post_location,
"suffix" : filename[10:],
"is_published" : is_published,
"post_text" : post_text,
"title" : title,
"date" : post_date,
"selected" : False
}
print('')
get_dates()
###
# Conversion
###
answer = ""
while answer not in allowed_choices:
if use_defaults:
answer = unpublish_default
else:
answer = input("Un-publish all posts? ")
if answer in yes_choices:
unpublish = True
elif answer in no_choices:
unpublish = False
replacements = {}
default_title = input("Enter default title for post names")
for i, date in enumerate(sessions.keys()):
session = sessions[date]
if session["meets"]:
try:
options = [ "{0}: {1}".format(i["date"], i["title"]) for i in oldposts.values() if i["selected"] == False ]
selections = pick(options, "Compiling {0}. {1} Sessions remain. {2} posts remain".format(date, num_sessions, len(options)), multi_select = True, min_selection_count=0)
except ValueError:
print("No more old posts to convert")
break
replacements[date] = []
# Remove selections from old posts
for selection in selections:
replacements[date] += [selection[0]]
oldposts[selection[0][:10]]["selected"] = True
print(replacements[date])
new_filename = str(date) + "-{1}-{0}.md".format(session["nth_class"], default_title)
new_filepath = output_dir + "/" + new_filename
with open(new_filepath, 'a+') as f:
for i in selections:
text = oldposts[i[0][:10]]["post_text"]
if i[1] != 0:
# remove yaml header
text = re.sub(r'---.*?---', "", text, re.S | re.M)
elif unpublish:
# make sure yaml header
text = re.sub(r'published\: true', 'published: false', text)
if not re.search(r'---.*?published: false.*?---', text, flags=re.S):
text = re.sub('(---.*?)(---)', '\g<1>published: false\n---', text, flags = re.S | re.M)
f.write(text)
print("Wrote {0}".format(new_filepath))
num_sessions -= 1
elif session["holiday"]:
answer = ""
while answer not in allowed_choices:
answer = input("Make post for {0}? ".format(session["holiday_name"]))
if answer in yes_choices:
new_filepath = output_dir + "/" + str(date) + "-holiday-{0}.md".format(i + 1)
with open(new_filepath,'w+') as f:
f.write("""---
author: elliott
title: {0}
---""".format(session["holiday_name"]))