about summary refs log tree commit diff
diff options
context:
space:
mode:
authorStarfall <us@starfall.systems>2021-10-29 00:10:09 -0500
committerStarfall <us@starfall.systems>2023-12-05 10:00:50 -0600
commitd528dcbf7fb70d6004c6408e5663d9ce1bd1cb5c (patch)
tree888c5803ea67521e814a113d62bd90578e1a80fe
parent2a076115b8a8e36d1b243a2e29663a108e11e422 (diff)
strike: use pathlib.Path for better readability where simple
-rw-r--r--sample/html/empty.html2
-rw-r--r--sample/html/happy.html2
-rw-r--r--sample/html/with-frontmatter.html2
-rwxr-xr-xstrike.py44
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 @@
 <meta charset=UTF-8>
 <meta name=robots content="noindex, nofollow">
 <meta name=viewport content="width=device-width, initial-scale=1">
-<meta http-equiv=last-modified content="Sat, 23 Oct 2021 02:55:53 GMT">
+<meta http-equiv=last-modified content="Fri, 29 Oct 2021 04:55:33 GMT">
 
 <section>
 	
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 @@
 <meta charset=UTF-8>
 <meta name=robots content="noindex, nofollow">
 <meta name=viewport content="width=device-width, initial-scale=1">
-<meta http-equiv=last-modified content="Sat, 23 Oct 2021 02:57:17 GMT">
+<meta http-equiv=last-modified content="Fri, 29 Oct 2021 04:55:33 GMT">
 
 <section>
 	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 @@
 <meta charset=UTF-8>
 <meta name=robots content="noindex, nofollow">
 <meta name=viewport content="width=device-width, initial-scale=1">
-<meta http-equiv=last-modified content="Fri, 29 Oct 2021 03:25:04 GMT">
+<meta http-equiv=last-modified content="Fri, 29 Oct 2021 04:55:33 GMT">
 
 <section>
 	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