-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigrate-cats.py
29 lines (27 loc) · 1.12 KB
/
migrate-cats.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
import csv
import datetime
import json
import functools
import re
import yaml
newCats = {}
with open('cats.csv', newline='') as csvfile:
reader = csv.reader(csvfile, delimiter=',', quotechar='"')
for (slug,catSlug,cat) in reader:
try:
out = []
with open(f"./content/paintings/{slug}.md","r") as md:
docs = yaml.safe_load_all(md)
for doc in docs:
if doc:
doc.setdefault('categories',[]).append(catSlug)
out.append(dict(doc))
newCats[catSlug] = cat
print(doc)
with open(f"./content/paintings/{slug}.md","w") as md:
yaml.dump_all(out,md,default_flow_style=False,explicit_start=True,explicit_end=True)
except IOError as e:
print( f"ignoring {slug}: {e}" )
for (slug,title) in newCats.items():
with open(f"./content/categories/{slug}.md","w") as md:
yaml.dump_all([{ "title":title, "slug":slug}],md,explicit_start=True,explicit_end=True)