mirror of
https://github.com/venthur/blag.git
synced 2025-11-25 20:52:43 +00:00
WIP
This commit is contained in:
65
blag/blag.py
65
blag/blag.py
@@ -76,6 +76,29 @@ def parse_args(args=None):
|
||||
quickstart_parser = commands.add_parser('quickstart')
|
||||
quickstart_parser.set_defaults(func=quickstart)
|
||||
|
||||
serve_parser = commands.add_parser('serve')
|
||||
serve_parser.set_defaults(func=serve)
|
||||
serve_parser.add_argument(
|
||||
'-i', '--input-dir',
|
||||
default='content',
|
||||
help='Input directory (default: content)',
|
||||
)
|
||||
serve_parser.add_argument(
|
||||
'-o', '--output-dir',
|
||||
default='build',
|
||||
help='Ouptut directory (default: build)',
|
||||
)
|
||||
serve_parser.add_argument(
|
||||
'-t', '--template-dir',
|
||||
default='templates',
|
||||
help='Template directory (default: templates)',
|
||||
)
|
||||
serve_parser.add_argument(
|
||||
'-s', '--static-dir',
|
||||
default='static',
|
||||
help='Static directory (default: static)',
|
||||
)
|
||||
|
||||
return parser.parse_args(args)
|
||||
|
||||
|
||||
@@ -352,5 +375,47 @@ def quickstart(args):
|
||||
config.write(fh)
|
||||
|
||||
|
||||
def get_last_modified():
|
||||
|
||||
last_mtime = 0
|
||||
|
||||
for dir in 'content', 'static', 'templates':
|
||||
for root, dirs, files in os.walk(dir):
|
||||
for f in files:
|
||||
mtime = os.stat(os.path.join(root, f)).st_mtime
|
||||
if mtime > last_mtime:
|
||||
last_mtime = mtime
|
||||
|
||||
return last_mtime
|
||||
|
||||
|
||||
def autoreload(args):
|
||||
import time
|
||||
last_mtime = get_last_modified()
|
||||
while True:
|
||||
mtime = get_last_modified()
|
||||
if mtime > last_mtime:
|
||||
last_mtime = mtime
|
||||
logger.debug('ping!')
|
||||
build(args)
|
||||
time.sleep(1)
|
||||
|
||||
|
||||
def serve(args):
|
||||
import multiprocessing
|
||||
from http.server import SimpleHTTPRequestHandler, HTTPServer
|
||||
from functools import partial
|
||||
|
||||
|
||||
httpd = HTTPServer(('', 8000), partial(SimpleHTTPRequestHandler, directory='build'))
|
||||
|
||||
p1 = multiprocessing.Process(target=autoreload, args=(args,))
|
||||
#p2 = # todo start http server
|
||||
|
||||
p1.start()
|
||||
#p2.start()
|
||||
httpd.serve_forever()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user