about summary refs log tree commit diff
path: root/strike.py
diff options
context:
space:
mode:
authorStarfall <us@starfall.systems>2021-10-22 23:13:45 -0500
committerStarfall <us@starfall.systems>2023-12-05 10:00:30 -0600
commit6476982874fec8a7e0e77b0f0484c7c8c0c56ea9 (patch)
tree68553e7a203fd3060d10a08582a5d1faf60773ba /strike.py
parent5b63c384ef8e100518ff50b9cb023ac07265df35 (diff)
strike: read and process from config file
Diffstat (limited to 'strike.py')
-rwxr-xr-xstrike.py39
1 files changed, 30 insertions, 9 deletions
diff --git a/strike.py b/strike.py
index dfcb2ed..b6d526b 100755
--- a/strike.py
+++ b/strike.py
@@ -1,32 +1,53 @@
 #!/usr/bin/env python3
-from os import listdir, mkdir
+from os import listdir, mkdir, path
 from sys import exit
 from argparse import ArgumentParser
+from configparser import ConfigParser
 
 def handle_args():
     parser = ArgumentParser()
-    parser.add_argument('--input', '-i', help='input directory (default "blog")', default='blog')
-    parser.add_argument('--output', '-o', help='output directory (default "output")', default='output')
-    parser.add_argument('--template', '-t', help='template to use (default "template")', default='template')
+    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()
+    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
+
 def get_output(content, template="{content}"):
     return template.format(content = content)
 
 def main():
     args = handle_args()
+    basedir, config = read_config(args.config)
+
+    input_dir = path.join(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(args.output)
+    try: mkdir(output_dir)
     except FileExistsError: pass
 
-    with open(args.template) as t:
+    with open(default_template) as t:
         template_contents = t.read()
 
-    for file in listdir(args.input):
-        with open(args.input + '/' + file, 'r') as fin:
+    for file in listdir(input_dir):
+        if file in exclusions:
+            continue 
+        with open(path.join(input_dir, file), 'r') as fin:
             file_contents = fin.read()
         output = get_output(file_contents, template=template_contents)
-        with open(args.output + '/' + file, 'w') as fout:
+        with open(path.join(output_dir, file), 'w') as fout:
             fout.write(output)
 
 if __name__ == '__main__':