1
0
Fork 0

Use temporary files for editing

This commit is contained in:
Luca Beltrame 2015-05-24 23:45:22 +02:00
parent aaf97f6c43
commit 26a2578084

View file

@ -5,7 +5,9 @@ import datetime
from hashlib import sha1 from hashlib import sha1
from pathlib import Path from pathlib import Path
import subprocess import subprocess
import shutil
from textwrap import fill from textwrap import fill
import tempfile
from git import Repo from git import Repo
import pytz import pytz
@ -70,19 +72,25 @@ def create_new_post(title: str, categories: list=None, tags: list=None,
final_path = Path("_posts") / filename final_path = Path("_posts") / filename
with final_path.open("w") as handle: with tempfile.NamedTemporaryFile() as temp:
handle.write("---\n")
yaml.safe_dump(metadata, handle, default_flow_style=False)
handle.write("---\n")
pre_edit_hash = hash_file(final_path) temp_path = Path(temp.name)
subprocess.check_call("/usr/bin/kate {}".format(Path("_posts") / filename),
shell=True)
post_edit_hash = hash_file(final_path)
if pre_edit_hash == post_edit_hash: temp.write("---\n")
print("No post content. Aborting.") yaml.safe_dump(metadata, temp, default_flow_style=False)
return temp.write("---\n")
temp.flush()
pre_edit_hash = hash_file(temp_path)
subprocess.check_call("/usr/bin/kate {}".format(temp.name),
shell=True)
post_edit_hash = hash_file(final_path)
if pre_edit_hash == post_edit_hash:
print("No post content. Aborting.")
return
shutil.copy(temp.name, str(final_path))
return (final_path) return (final_path)
@ -102,6 +110,9 @@ def main():
options = parser.parse_args() options = parser.parse_args()
if not is_jekyll_root(Path("./")):
raise FileNotFoundError("Jekyll root not found.")
path = create_new_post(options.title, options.categories, options.tags, path = create_new_post(options.title, options.categories, options.tags,
comments=options.disable_comments) comments=options.disable_comments)