From d528dcbf7fb70d6004c6408e5663d9ce1bd1cb5c Mon Sep 17 00:00:00 2001 From: Starfall Date: Fri, 29 Oct 2021 00:10:09 -0500 Subject: strike: use pathlib.Path for better readability where simple --- sample/html/empty.html | 2 +- sample/html/happy.html | 2 +- sample/html/with-frontmatter.html | 2 +- strike.py | 44 +++++++++++++++++++-------------------- 4 files changed, 24 insertions(+), 26 deletions(-) diff --git a/sample/html/empty.html b/sample/html/empty.html index b8951a7..b1a01ea 100644 --- a/sample/html/empty.html +++ b/sample/html/empty.html @@ -4,7 +4,7 @@ - +
diff --git a/sample/html/happy.html b/sample/html/happy.html index 3c239f1..9f4b32f 100644 --- a/sample/html/happy.html +++ b/sample/html/happy.html @@ -4,7 +4,7 @@ - +
Hello, world! diff --git a/sample/html/with-frontmatter.html b/sample/html/with-frontmatter.html index 681c23d..99627a1 100644 --- a/sample/html/with-frontmatter.html +++ b/sample/html/with-frontmatter.html @@ -4,7 +4,7 @@ - +
Things before this line should not appear in the output. diff --git a/strike.py b/strike.py index 194c52d..1f37a77 100755 --- a/strike.py +++ b/strike.py @@ -1,9 +1,9 @@ #!/usr/bin/env python3 -from os import listdir, mkdir, path +from os import listdir from time import gmtime, strftime from argparse import ArgumentParser from configparser import ConfigParser - +from pathlib import Path def handle_args(): parser = ArgumentParser() @@ -11,21 +11,21 @@ def handle_args(): return parser.parse_args() def read_config(location): + location = Path(location) config = ConfigParser() - if path.isdir(location): - location = location + '/strike.ini' - if not path.exists(location): + if location.is_dir(): + location = location/'strike.ini' + if not location.exists(): raise FileNotFoundError(f'Config file not found at {location}.') config.read(location) - return path.dirname(path.abspath(location)), config + return location.resolve().parent, config def handle_file(location): meta = dict( - title = '.'.join(path.basename(location).split('.')[:-1]), - date = strftime('%a, %d %b %Y %H:%M:%S GMT', gmtime(path.getmtime(location))) + title = location.stem, + date = strftime('%a, %d %b %Y %H:%M:%S GMT', gmtime(location.stat().st_mtime)) ) - with open(location, 'r') as fin: - contents = fin.read() + contents = location.read_text() delim = '---\n' if contents.startswith(delim): parts = contents.split(delim, maxsplit=2) @@ -35,34 +35,32 @@ def handle_file(location): contents = parts[2] return contents, meta -def apply_template(content, metadata, template="{content}"): +def apply_template(content, meta, template="{content}"): return template.format( content = content, - meta = metadata + meta = meta ) def main(): args = handle_args() basedir, config = read_config(args.config) - input_dir = path.join(basedir, config['Input']['directory']) + input_dir = basedir/config['Input']['directory'] + exclusions = config['Input']['excludes'].splitlines() - default_template = path.join(basedir, config['Templates']['default']) - output_dir = path.join(basedir, config['Output']['directory']) - try: mkdir(output_dir) - except FileExistsError: pass + default_template = basedir/config['Templates']['default'] + template = default_template.read_text() - with open(default_template) as t: - template = t.read() + output_dir = basedir/config['Output']['directory'] + output_dir.mkdir(parents=True, exist_ok=True) for file in listdir(input_dir): if file in exclusions: continue - file_contents, metadata = handle_file(path.join(input_dir, file)) - output = apply_template(file_contents, metadata, template) - with open(path.join(output_dir, file), 'w') as fout: - fout.write(output) + contents, meta = handle_file(input_dir/file) + output = apply_template(contents, meta, template) + (output_dir/file).write_text(output) if __name__ == '__main__': import sys -- cgit