aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorneodarz <neodarz@neodarz.net>2019-05-26 16:48:15 +0200
committerneodarz <neodarz@neodarz.net>2019-05-26 16:48:18 +0200
commit6d020825e5f2fa09bd5488954ecec2afe8c85aef (patch)
treead1fd7d79b719f9e0e04e01e4241a4e7a86f3feb
parent12e05d77288c39f262322eaee36faf3d0b31c781 (diff)
downloadmy_new_personal_website-6d020825e5f2fa09bd5488954ecec2afe8c85aef.tar.xz
my_new_personal_website-6d020825e5f2fa09bd5488954ecec2afe8c85aef.zip
Move touch to external file
-rw-r--r--cli/cli.py66
-rwxr-xr-xpyblog66
2 files changed, 68 insertions, 64 deletions
diff --git a/cli/cli.py b/cli/cli.py
index 71491357..0de89048 100644
--- a/cli/cli.py
+++ b/cli/cli.py
@@ -5,6 +5,10 @@ import subprocess
import sys
+import re
+import io
+import fileinput
+
from config.config import *
from utils import utils
@@ -82,3 +86,65 @@ def new_post(title):
def new_post_cli(args):
"""CLI wrapper around new_post."""
new_post(args.title)
+
+
+def touch(filename):
+ """Update the timestamp of a post to the current time."""
+ filename = os.path.basename(filename)
+ fullpath = os.path.join(POSTSDIR, filename)
+ if not os.path.exists(fullpath):
+ sys.stderr.write("%serror: post %s not found %s\n" %
+ (RED, fullpath, RESET))
+ return 1
+ filename_prefix_re = re.compile(r"^[0-9]{4}-[0-9]{2}-[0-9]{2}")
+ if not filename_prefix_re.match(filename):
+ sys.stderr.write(RED)
+ sys.stderr.write("error: post %s is not a valid post\n" % filename)
+ sys.stderr.write("error: the filename of a valid post begins with "
+ "a date in the form xxxx-xx-xx\n")
+ sys.stderr.write(RESET)
+ return 1
+
+ # update timestamp in the metadata section of the post
+ whatchanged = io.StringIO()
+ date = utils.current_datetime()
+ iso_date = date.isoformat()
+ display_date = "%s %d, %d" % (date.strftime("%B"), date.day, date.year)
+ filename_date = date.strftime("%Y-%m-%d")
+ with fileinput.input(files=(fullpath), inplace=True) as lines:
+ meta_fences = 0
+ for line in lines:
+ if line.startswith("---"):
+ meta_fences += 1
+ sys.stdout.write(line)
+ continue
+ if meta_fences >= 2:
+ # already went past the metadata section
+ sys.stdout.write(line)
+ continue
+
+ if line.startswith("date: "):
+ updated_line = "date: %s\n" % iso_date
+ sys.stdout.write(updated_line)
+ whatchanged.write("-%s+%s\n" % (line, updated_line))
+ continue
+
+ if line.startswith("date_display: "):
+ updated_line = "date_display: %s\n" % display_date
+ sys.stdout.write(updated_line)
+ whatchanged.write("-%s+%s\n" % (line, updated_line))
+ continue
+
+ sys.stdout.write(line)
+
+ sys.stderr.write("\n%schangeset:%s\n\n%s" %
+ (YELLOW, RESET, whatchanged.getvalue()))
+ whatchanged.close()
+
+ # check if the file needs to be renamed
+ new_filename = filename_prefix_re.sub(filename_date, filename)
+ if new_filename != filename:
+ new_fullpath = os.path.join(POSTSDIR, new_filename)
+ os.rename(fullpath, new_fullpath)
+ sys.stderr.write("%srenamed to %s%s\n" % (YELLOW, new_filename, RESET))
+ return 0
diff --git a/pyblog b/pyblog
index a2adee02..3c574445 100755
--- a/pyblog
+++ b/pyblog
@@ -53,71 +53,9 @@ from generators import generators
from cli import cli
-def touch(filename):
- """Update the timestamp of a post to the current time."""
- filename = os.path.basename(filename)
- fullpath = os.path.join(POSTSDIR, filename)
- if not os.path.exists(fullpath):
- sys.stderr.write("%serror: post %s not found %s\n" %
- (RED, fullpath, RESET))
- return 1
- filename_prefix_re = re.compile(r"^[0-9]{4}-[0-9]{2}-[0-9]{2}")
- if not filename_prefix_re.match(filename):
- sys.stderr.write(RED)
- sys.stderr.write("error: post %s is not a valid post\n" % filename)
- sys.stderr.write("error: the filename of a valid post begins with "
- "a date in the form xxxx-xx-xx\n")
- sys.stderr.write(RESET)
- return 1
-
- # update timestamp in the metadata section of the post
- whatchanged = io.StringIO()
- date = utils.current_datetime()
- iso_date = date.isoformat()
- display_date = "%s %d, %d" % (date.strftime("%B"), date.day, date.year)
- filename_date = date.strftime("%Y-%m-%d")
- with fileinput.input(files=(fullpath), inplace=True) as lines:
- meta_fences = 0
- for line in lines:
- if line.startswith("---"):
- meta_fences += 1
- sys.stdout.write(line)
- continue
- if meta_fences >= 2:
- # already went past the metadata section
- sys.stdout.write(line)
- continue
-
- if line.startswith("date: "):
- updated_line = "date: %s\n" % iso_date
- sys.stdout.write(updated_line)
- whatchanged.write("-%s+%s\n" % (line, updated_line))
- continue
-
- if line.startswith("date_display: "):
- updated_line = "date_display: %s\n" % display_date
- sys.stdout.write(updated_line)
- whatchanged.write("-%s+%s\n" % (line, updated_line))
- continue
-
- sys.stdout.write(line)
-
- sys.stderr.write("\n%schangeset:%s\n\n%s" %
- (YELLOW, RESET, whatchanged.getvalue()))
- whatchanged.close()
-
- # check if the file needs to be renamed
- new_filename = filename_prefix_re.sub(filename_date, filename)
- if new_filename != filename:
- new_fullpath = os.path.join(POSTSDIR, new_filename)
- os.rename(fullpath, new_fullpath)
- sys.stderr.write("%srenamed to %s%s\n" % (YELLOW, new_filename, RESET))
- return 0
-
-
def touch_cli(args):
"""CLI wrapper around touch."""
- touch(args.filename)
+ cli.touch(args.filename)
def deploy(args):
@@ -277,7 +215,7 @@ def gen_deploy(args):
sys.stderr.write("Please answer yes, no, or quit.\n")
if yesno:
sys.stderr.write("%stouching %s%s\n" % (BLUE, latest_post, RESET))
- touch(latest_post)
+ cli.touch(latest_post)
sys.stderr.write("\n")
generators.generate_blog(fresh=True)