about summary refs log tree commit diff
diff options
context:
space:
mode:
authorStarfall <us@starfall.systems>2021-10-28 23:20:39 -0500
committerStarfall <us@starfall.systems>2023-12-05 10:00:44 -0600
commit53f4e85f105516813b18aedf6248000a76e43431 (patch)
tree85fcb653c70203c298fafb3a883a20add3a9873f
parentf456510d32bfb4014d75241e42b7d16c599697de (diff)
strike: minor refactoring
* move sys import to where it's used
* remove commented-out code
* change parser -> config in read_config() for slightly better readability
* change meta_dict -> meta and file_contents -> contents in
  handle_file()
* push handle_frontmatter() back into handle_file(). likely to pull
  this back out again if we decide the format is handy for keeping
  default values around in templates, but we'll cross that bridge later
-rwxr-xr-xstrike.py41
1 files changed, 15 insertions, 26 deletions
diff --git a/strike.py b/strike.py
index 0679bd1..194c52d 100755
--- a/strike.py
+++ b/strike.py
@@ -1,51 +1,39 @@
 #!/usr/bin/env python3
 from os import listdir, mkdir, path
-from sys import exit
 from time import gmtime, strftime
 from argparse import ArgumentParser
 from configparser import ConfigParser
 
+
 def handle_args():
     parser = ArgumentParser()
     parser.add_argument('config', help='location of strike.ini file (or a directory containing it)')
-# does it make any sense to override the config file? probably not for cases involving multiple templates or excludes. but for simple ones i could see it
-#    parser.add_argument('--input', '-i', help='input directory (overrides config file)', default='')
-#    parser.add_argument('--output', '-o', help='output directory (overrides config file)', default='')
-#    parser.add_argument('--template', '-t', help='template to use (overrides config file)', default='')
     return parser.parse_args()
 
 def read_config(location):
-    parser = ConfigParser()
+    config = ConfigParser()
     if path.isdir(location):
         location = location + '/strike.ini'
     if not path.exists(location):
         raise FileNotFoundError(f'Config file not found at {location}.')
-    location = parser.read(location)
-    basedir = path.dirname(path.abspath(location[0]))
-    return basedir, parser
+    config.read(location)
+    return path.dirname(path.abspath(location)), config
 
 def handle_file(location):
-    meta_dict = dict(
+    meta = dict(
         title = '.'.join(path.basename(location).split('.')[:-1]),
         date = strftime('%a, %d %b %Y %H:%M:%S GMT', gmtime(path.getmtime(location)))
     )
     with open(location, 'r') as fin:
-        file_contents = fin.read()
-    contents = handle_frontmatter(file_contents, meta_dict)
-    return contents, meta_dict
-
-def handle_frontmatter(file_contents, meta_dict):
+        contents = fin.read()
     delim = '---\n'
-    if not file_contents.startswith(delim):
-        return file_contents
-    parts = file_contents.split(delim, maxsplit=2)
-    meta_lines = parts[1].splitlines()
-    meta = dict(
-        (x.strip(), y.strip())
-        for x, y in (kvp.split('=') for kvp in meta_lines)
-    )
-    meta_dict.update(meta)
-    return parts[2]
+    if contents.startswith(delim):
+        parts = contents.split(delim, maxsplit=2)
+        meta.update(dict((key.strip(), value.strip())
+            for key, value in (line.split('=') for line in parts[1].splitlines())
+        ))
+        contents = parts[2]
+    return contents, meta
 
 def apply_template(content, metadata, template="{content}"):
     return template.format(
@@ -77,4 +65,5 @@ def main():
             fout.write(output)
 
 if __name__ == '__main__':
-    exit(main())
+    import sys
+    sys.exit(main())