-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.py
44 lines (34 loc) · 1.26 KB
/
utils.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
import os
import git
import yaml
def path2filename(pages, path):
"""Convert path to physical file path."""
return "{}.md".format(os.path.join(pages.root, path)) # TODO: may not be safe
def get_non_private_page_paths(pages):
non_private_page_paths = []
for page_path in pages._pages.keys():
page = pages.get(page_path)
if page.meta.get("private", False):
continue
non_private_page_paths.append(page_path)
return non_private_page_paths
def commit_and_push_changes():
repo = git.Repo("pages")
repo.git.add(A=True)
repo.git.commit(m="edited via yyiki", author="YY Ahn <[email protected]>")
# repo.git.push()
GIT_COMMAND = ["git", "-C", "pages"]
# subprocess.run(GIT_COMMAND + ["add", "*"])
# subprocess.run(
# GIT_COMMAND
# + ["commit", "--author='YY Ahn <[email protected]>'", "-m", "edited via yyiki"]
# )
os.spawnlp(os.P_NOWAIT, "git", *GIT_COMMAND, "push")
def write_page(pages, page):
# TODO: check the safety of page.path when there's a space or other
# characters in the page.path.
filename = path2filename(pages, page.path)
with open(filename, "w") as f:
f.write(yaml.dump(page.meta))
f.write("\n")
f.write(page.body.replace("\r", ""))