parent
4ed8afacb2
commit
d0cd7070bc
|
@ -1,58 +1,11 @@
|
||||||
from pathlib import Path
|
|
||||||
|
|
||||||
from pyramid.config import Configurator
|
from pyramid.config import Configurator
|
||||||
from pyramid.path import AssetResolver
|
|
||||||
from watchdog.observers import Observer
|
|
||||||
from watchdog.events import FileSystemEventHandler
|
|
||||||
|
|
||||||
from flatfilecms.resources import Root, root_factory, roots
|
from flatfilecms.resources import root_factory
|
||||||
|
|
||||||
|
|
||||||
class PagesEventHandler(FileSystemEventHandler):
|
|
||||||
def __init__(self, root_path):
|
|
||||||
super(PagesEventHandler, self).__init__()
|
|
||||||
self.root_path = root_path
|
|
||||||
self.abspath = AssetResolver().resolve(root_path).abspath()
|
|
||||||
|
|
||||||
def find_dir(self, path):
|
|
||||||
folder = roots.setdefault(self.root_path, Root(self.root_path))
|
|
||||||
path = path.relative_to(self.abspath)
|
|
||||||
for name in path.parts[:-1]:
|
|
||||||
folder = folder[name]
|
|
||||||
return folder
|
|
||||||
|
|
||||||
def on_created(self, event):
|
|
||||||
super(PagesEventHandler, self).on_created(event)
|
|
||||||
path = Path(
|
|
||||||
event.dest_path if hasattr(event, 'dest_path') else event.src_path)
|
|
||||||
folder = self.find_dir(path)
|
|
||||||
if event.is_directory:
|
|
||||||
folder.create_dir(path)
|
|
||||||
else:
|
|
||||||
folder.create_file(path)
|
|
||||||
|
|
||||||
def on_deleted(self, event):
|
|
||||||
super(PagesEventHandler, self).on_deleted(event)
|
|
||||||
path = Path(event.src_path)
|
|
||||||
folder = self.find_dir(path)
|
|
||||||
name = path.stem
|
|
||||||
if path.suffix not in ['.md', '.yaml', '.j2', '.jinja2'] \
|
|
||||||
and path.name != 'index.html':
|
|
||||||
name = path.name
|
|
||||||
del folder[name]
|
|
||||||
|
|
||||||
def on_moved(self, event):
|
|
||||||
super(PagesEventHandler, self).on_moved(event)
|
|
||||||
self.on_deleted(event)
|
|
||||||
self.on_created(event)
|
|
||||||
|
|
||||||
|
|
||||||
def main(global_config, **settings):
|
def main(global_config, **settings):
|
||||||
""" This function returns a Pyramid WSGI application.
|
""" This function returns a Pyramid WSGI application.
|
||||||
"""
|
"""
|
||||||
from pyramid.settings import asbool
|
|
||||||
watchdog = asbool(settings.get('watchdog', 'false'))
|
|
||||||
settings['watchdog'] = watchdog
|
|
||||||
settings.setdefault('pages_path', 'pages')
|
settings.setdefault('pages_path', 'pages')
|
||||||
settings.setdefault('data_path', 'data')
|
settings.setdefault('data_path', 'data')
|
||||||
default_jinja2_filters = """markdown = flatfilecms.filters:markdown2html
|
default_jinja2_filters = """markdown = flatfilecms.filters:markdown2html
|
||||||
|
@ -66,9 +19,4 @@ def main(global_config, **settings):
|
||||||
config.include('pyramid_jinja2')
|
config.include('pyramid_jinja2')
|
||||||
config.include('.routes')
|
config.include('.routes')
|
||||||
config.scan()
|
config.scan()
|
||||||
if watchdog:
|
|
||||||
observer = Observer()
|
|
||||||
event_handler = PagesEventHandler(settings['pages_path'])
|
|
||||||
observer.schedule(event_handler, event_handler.abspath, recursive=True)
|
|
||||||
observer.start()
|
|
||||||
return config.make_wsgi_app()
|
return config.make_wsgi_app()
|
||||||
|
|
Reference in New Issue