about summary refs log tree commit diff
path: root/strike.py
diff options
context:
space:
mode:
Diffstat (limited to 'strike.py')
-rwxr-xr-xstrike.py44
1 files changed, 21 insertions, 23 deletions
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