<commit_before># coding: utf-8
"""
    holocron.ext.commands.serve
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    The module implements a serve command.

    :copyright: (c) 2014, <NAME>
    :license: BSD, see LICENSE for details
"""

import os

from http.server import HTTPServer, SimpleHTTPRequestHandler

from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler

from holocron import app
from holocron.ext import Command


class ChangeHandler(FileSystemEventHandler):
    """
    Implements catching system events of adding or changing files. Responsible
    for processing events and rebuilding theme files and blogposts.
    """
    def __init__(self, app):
        super(ChangeHandler, self).__init__()
        self.app = app

    def process(self, document_path):
        """
        Rebuilds the whole blog, if a blog post or theme file were changed.
        If the file from the output directiry was changed than do nothing.

        :param document_path: path to changed/added file
        """
        document = os.path.abspath(document_path)
        output = os.path.abspath(self.app.conf['paths.output'])

        # do not track changes in the output directory
        if document.startswith(output):
            return

        self.app.run()

    def on_modified(self, event):
        if event.is_directory:
            return

        self.process(event.src_path)

    def on_created(self, event):
        if event.is_directory:
            return

        self.process(event.src_path)


def create_holocron_handler(path):
    """
    This factory method is used to create the http handler class with a custom
    attribute indicating a serve path.

    :param path: path to a directory to serve files from
    """
    class HolocronHandler(SimpleHTTPRequestHandler):

        serve = path

        def translate_path(self, path):
            """<commit_msg>Add serve support for configuration files<commit_after># coding: utf-8
"""
    holocron.ext.commands.serve
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    The module implements a serve command.

    :copyright: (c) 2014, <NAME>
    :license: BSD, see LICENSE for details
"""

import os

from http.server import HTTPServer, SimpleHTTPRequestHandler

from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler

from holocron import app
from holocron.main import get_config, create_app
from holocron.ext import Command


class ChangeHandler(FileSystemEventHandler):
    """
    Implements catching system events of adding or changing files. Responsible
    for processing events and rebuilding theme files and blogposts.
    """
    def __init__(self, app):
        super(ChangeHandler, self).__init__()
        self.app = app

    def process(self, document_path):
        """
        Rebuilds the whole blog, if a blog post or theme file were changed.
        If the file from the output directiry was changed than do nothing.

        :param document_path: path to changed/added file
        """
        document = os.path.abspath(document_path)
        output = os.path.abspath(self.app.conf['paths.output'])

        # do not track changes in the output directory
        if document.startswith(output):
            return

        # if config file was changed, update application obejct
        if document.startswith(os.path.abspath('_config.yml')):
            config = get_config(document)
            self.app = create_app(config)

        self.app.run()

    def on_modified(self, event):
        if event.is_directory:
            return

        self.process(event.src_path)

    def on_created(self, event):
        if event.is_directory:
            return

        self.process(event.src_path)


def create_holocron_handler(path):
    """
    This factory method is used to create the http handler class with a custom
    attribute indicating a serve path.

    :param path: path to a directory to serve files from
    """
    class HolocronHandler(SimpleHTTPRequestHandler):

        serve = path

        def translate_path(self, path):
            """
{'fraction_non_alphanumeric': 0.08708414872798434, 'fraction_numerical': 0.002446183953033268, 'mean_word_length': 2.7410795974382434, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2737516', 'n_tokens_mistral': 1114, 'n_tokens_neox': 1071, 'n_words': 370}