<commit_before>"""Local development server manager."""

import logging
import os
import socket
import sys
import threading
from grow.common import bulk_errors
from grow.common import colors
from grow.common import timer
from grow.documents import document
from grow.preprocessors import file_watchers
from grow.server import main as main_lib
from werkzeug import serving


# Number of tries to find a free port.
NUM_TRIES = 10


class CallbackHTTPServer(serving.ThreadedWSGIServer):

    def server_activate(self):
        super(CallbackHTTPServer, self).server_activate()
        _, port = self.server_address
        self.pod.env.port = port
        with timer.Timer() as router_time:
            try:
                self.pod.router.add_all(concrete=False)
            except Exception:
                self.pod.logger.exception('Error encountered starting server.')
                # Do not exit the process here; the server should remain active
                # so the developer can fix any issues related to the router,
                # without needing to manually restart the server.
        self.pod.logger.info('{} routes built in {:.3f} s'.format(
            len(self.pod.router.routes), router_time.secs))

        url, extra_urls = print_server_ready_message(self.pod, self.pod.env.host, port)
        if self.open_browser:
            start_browser_in_thread(url)
            for extra_url in extra_urls:
                start_browser_in_thread(extra_url)

class ServerMessages:
    """Simple server messages."""

    def __init__(self):
        self.messages = []

    def add_message(self, label, message, label_color=None, message_color=None):
        self.messages.append((label, message, label_color, message_color))

    def print(self, log_function):
        label_max_width = 0
        for label, _, _, _ in self.messages:
            label_max_width = max(len(label), label_max_width)

        for label, message, label_color, message_color in self.messages:
            if label_color:
                label = colors.stylize(label.rjust(label_max_width), label_color)
            if message_color:
                message = colors.stylize(message, message_color)
            log_function('{} {}'.format(label, message))


def print_server_ready_message(pod, host, port):
    home_doc = pod.get_home_doc()
    root_path = home_doc.url.path if home_doc and home_doc.exists else '/'
    url_base = 'http://{}:{}/'.format(host, port)
    url_root = 'http://{}:{}{}'.format(host, port, root_path)
<commit_msg>chore: make server startup more error-tolerant<commit_after>"""Local development server manager."""

import logging
import os
import socket
import sys
import threading
from grow.common import bulk_errors
from grow.common import colors
from grow.common import timer
from grow.documents import document
from grow.preprocessors import file_watchers
from grow.server import main as main_lib
from werkzeug import serving


# Number of tries to find a free port.
NUM_TRIES = 10


class CallbackHTTPServer(serving.ThreadedWSGIServer):

    def server_activate(self):
        super(CallbackHTTPServer, self).server_activate()
        _, port = self.server_address
        self.pod.env.port = port
        with timer.Timer() as router_time:
            try:
                self.pod.router.add_all(concrete=False)
            except Exception:
                self.pod.logger.exception('Error encountered starting server.')
                # Do not exit the process here; the server should remain active
                # so the developer can fix any issues related to the router,
                # without needing to manually restart the server.
        self.pod.logger.info('{} routes built in {:.3f} s'.format(
            len(self.pod.router.routes), router_time.secs))

        url, extra_urls = print_server_ready_message(self.pod, self.pod.env.host, port)
        if self.open_browser:
            start_browser_in_thread(url)
            for extra_url in extra_urls:
                start_browser_in_thread(extra_url)

class ServerMessages:
    """Simple server messages."""

    def __init__(self):
        self.messages = []

    def add_message(self, label, message, label_color=None, message_color=None):
        self.messages.append((label, message, label_color, message_color))

    def print(self, log_function):
        label_max_width = 0
        for label, _, _, _ in self.messages:
            label_max_width = max(len(label), label_max_width)

        for label, message, label_color, message_color in self.messages:
            if label_color:
                label = colors.stylize(label.rjust(label_max_width), label_color)
            if message_color:
                message = colors.stylize(message, message_color)
            log_function('{} {}'.format(label, message))


def print_server_ready_message(pod, host, port):
    try:
        home_doc = pod.get_home_doc()
        root_path = home_doc.url.path if home_doc and home_doc.exists else '/'
    except:
        root_path = '/'
        pod.logger.exception('Failed to determine root URL path.')
    url_base = 'http://{}:{}/'.format(host, port)
    url_root = 'http://{}:{}{}'.format(host, port, root_path)

{'fraction_non_alphanumeric': 0.09342029541530789, 'fraction_numerical': 0.0015346249760214848, 'mean_word_length': 2.7947598253275108, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2605189', 'n_tokens_mistral': 1492, 'n_tokens_neox': 1453, 'n_words': 406}