Pārlūkot izejas kodu

Manage exceptions in hot reloading

Danilo Gómez Gómez 5 gadi atpakaļ
vecāks
revīzija
8d6510015b
1 mainītis faili ar 18 papildinājumiem un 14 dzēšanām
  1. 18 14
      sitegen.py

+ 18 - 14
sitegen.py

@@ -1,11 +1,12 @@
 import argparse
+from functools import lru_cache
 from jinja2 import Environment, FileSystemLoader, contextfilter
 from pathlib import Path
 from os import walk, makedirs, listdir, symlink, readlink
 from os.path import join, exists, splitext, split, islink, isdir
 from shutil import rmtree, copy2, copystat, Error
 from time import sleep
-from functools import lru_cache
+from traceback import print_exc
 
 #TODO: load from config file (and watch it too)
 LANGUAGES = 'languages'
@@ -195,15 +196,24 @@ def gen_root(args):
 def gen_static(args):
     copytree(STATIC, join(args.target, 'static'))
 
+def save_generate(generator, args, msg):
+    print(f'* {msg}')
+    try:
+        generator(args)
+        print('  done!')
+    except:
+        print_exc()
+        return False
+    return True
+
 # Runtime
 
 def run(args):
     print('initial compilation')
     init_gen(args)
-    gen_layout(args)
-    gen_root(args)
-    gen_static(args)
-    print('  done')
+    save_generate(gen_layout, args, 'generating layout')
+    save_generate(gen_root, args, 'copying root files')
+    save_generate(gen_static, args, 'copying static dir')
 
     if not args.watch:
         return
@@ -222,24 +232,18 @@ def run(args):
         new_source_mt = mtimes(args.source)
         new_source_mt.update(mtimes(LANGUAGES))
         if source_mt != new_source_mt:
-            print('recompiling layout')
             source_mt = new_source_mt
-            gen_layout(args)
-            print('  done!')
+            save_generate(gen_layout, args, 'recompiling layout')
         
         new_root_mt = mtimes(ROOT)
         if root_mt != new_root_mt:
-            print('copying root files')
             root_mt = new_root_mt
-            gen_root(args)
-            print('  done!')
+            save_generate(gen_root, args, 'copying root files')
         
         new_static_mt = mtimes(STATIC)
         if static_mt != new_static_mt:
-            print('copying static files')
             static_mt = new_static_mt
-            gen_static(args)
-            print('  done!')
+            save_generate(gen_static, args, 'copying static files')
 
 if __name__ == '__main__':
     parser = argparse.ArgumentParser("Site generator")