Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add support for watching for changes and re-generating #83

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion exhibition/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import logging

import click
from watchdog.observers import Observer

from . import __version__, config, utils

Expand Down Expand Up @@ -58,15 +59,26 @@ def gen():
@exhibition.command(short_help="Serve site locally")
@click.option("-s", "--server", default="localhost", help="Hostname to serve the site at.")
@click.option("-p", "--port", default=8000, type=int, help="Port to serve the site at.")
def serve(server, port):
@click.option("--watch/--no-watch", default=True, help="Enables/Disables watching for changes.")
def serve(server, port, watch):
"""
Serve files from deploy_path as a webserver would
"""
settings = config.Config.from_path(config.SITE_YAML_PATH)
server_address = (server, port)
httpd, thread = utils.serve(settings, server_address)

print("Watch status: %s" % watch)
if watch:
utils.gen(settings)
observer = Observer()
handler = utils.FileSystemChangeHandler(settings)
observer.schedule(handler, settings["content_path"], recursive=True)
observer.schedule(handler, settings["templates"], recursive=True)
observer.start()

try:
thread.join()
except (KeyboardInterrupt, SystemExit):
httpd.shutdown()
observer.join()
17 changes: 17 additions & 0 deletions exhibition/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import shutil
import threading

import watchdog

from .node import Node

logger = logging.getLogger("exhibition")
Expand All @@ -44,6 +46,21 @@ def gen(settings):
item.render()


class FileSystemChangeHandler(watchdog.events.FileSystemEventHandler):
""" Handler for changes on the file system """
settings = None

def __init__(self, settings, *args, **kwargs):
super().__init__(*args, **kwargs)
self.settings = settings

def on_any_event(self, event):
""" An event has happened, re-generate the site """
super().on_any_event(event)
print("Regenerating website")
gen(self.settings)


class ExhibitionBaseHTTPRequestHandler(SimpleHTTPRequestHandler):
def _sanitise_path(self, path):
""" Strip leading and trailing / as well as base_url, if preset """
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"ruamel.yaml",
"typogrify",
"pypandoc",
"watchdog",
],
extras_require={
"docs": [
Expand Down