summaryrefslogtreecommitdiff
path: root/megapixels/commands/site/watch.py
blob: 7fd3ba7c228c5c82da82ca34f164e01bd7d9a7b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
"""
Watch for changes in the static site and build them
"""

import click
import time  
from watchdog.observers import Observer  
from watchdog.events import PatternMatchingEventHandler

import app.settings.app_cfg as cfg
from app.site.builder import build_site, build_file

class SiteBuilder(PatternMatchingEventHandler):
  """
  Handler for filesystem changes to the content path
  """
  patterns = ["*.md"]

  def on_modified(self, event):
    print(event.src_path, event.event_type)
    build_file(event.src_path)

  def on_created(self, event):
    print(event.src_path, event.event_type)
    build_file(event.src_path)

@click.command()
@click.pass_context
def cli(ctx):
  """
  Run the observer and start watching for changes
  """
  print("{} is now being watched for changes.".format(cfg.DIR_SITE_CONTENT))
  observer = Observer()
  observer.schedule(SiteBuilder(), path=cfg.DIR_SITE_CONTENT, recursive=True)
  observer.start()

  try:
    while True:
      time.sleep(1)
  except KeyboardInterrupt:
    observer.stop()

  observer.join()