<commit_before>"""Implements the functionality for the built-in development server.

A tiny little HTTP file server, not suitable for use in production
environments.
"""

import os
import http.server
from urllib.parse import unquote

from sundara import config as sundara_config

class SundaraServer():
    def __init__(self, ip='127.0.0.1', port=8080, config=None):
        if config == None:
            self.ip = ip
            self.port = int(port)
            path = os.path.join(os.getcwd(), 'www')
        else:
            self.ip = config.get('server', 'ip')
            self.port = int(config.get('server', 'port'))
            path = config.get('sundara', 'generate')
        if not os.path.exists(path):
            raise IOError(2, "No such path: %s" % path)

    def run(self):
        print("Starting Sundara development server...")
        httpd = http.server.HTTPServer((self.ip, self.port),
                    SundaraRequestHandler)
        try:
            print("Server listening on http://%s:%s/" % (self.ip,
                        self.port))
            httpd.serve_forever()
        except KeyboardInterrupt:
            print("Server shutting down...")
            httpd.shutdown()


class SundaraRequestHandler(http.server.SimpleHTTPRequestHandler):
    """Implements the functionality for Sundara's built-in development
    server. A tiny little HTTP server, not suitable for use in production
    environments.

    This is an http.server Request Handler.
    """

    def translate_path(self, path):
        """This function overrides that in http.server.SimpleHTTPRequestHandler
        and modifies it to serve files from Sundara's `generate` path
        instead of the cwd.
        """
        path = path.split('?',1)[0]
        path = os.path.normpath(unquote(path))
        words = path.split(os.sep)
        words = filter(None, words)

        if os.path.exists(os.path.join(os.getcwd(), sundara_config.PROJECT_CONF)):
            path = sundara_config.Config(os.getcwd()).get('sundara', 'generate')
        else:
            path = os.path.join(os.getcwd(), 'www/')

        for word in words:
            drive, word = os.path.splitdrive(word)
            head, word = os.path.split(word)
            if word in (os.curdir, os.pardir): continue
            path = os.path.join(path, word)
        return path<commit_msg>Made some style changes to server.<commit_after>"""Implements the functionality for the built-in development server.

A tiny little HTTP file server, not suitable for use in production
environments.
"""

import os
import http.server
from urllib.parse import unquote

from sundara import config as sundara_config

class SundaraServer():
    def __init__(self, ip='127.0.0.1', port=8080, config=None):
        if config is None:
            self.ip = ip
            self.port = int(port)
            path = os.path.join(os.getcwd(), 'www')
        else:
            self.ip = config.get('server', 'ip')
            self.port = int(config.get('server', 'port'))
            path = config.get('sundara', 'generate')
        if not os.path.exists(path):
            raise IOError(2, "No such path: %s" % path)

    def run(self):
        print("Starting Sundara development server...")
        httpd = http.server.HTTPServer((self.ip, self.port),
                                        SundaraRequestHandler)
        try:
            print("Server listening on http://%s:%s/" % (self.ip, self.port))
            httpd.serve_forever()
        except KeyboardInterrupt:
            print("Server shutting down...")
            httpd.shutdown()


class SundaraRequestHandler(http.server.SimpleHTTPRequestHandler):
    """Implements the functionality for Sundara's built-in development
    server. A tiny little HTTP server, not suitable for use in production
    environments.

    This is an http.server Request Handler.
    """

    def translate_path(self, path):
        """This function overrides that in http.server.SimpleHTTPRequestHandler
        and modifies it to serve files from Sundara's `generate` path
        instead of the cwd.
        """
        path = path.split('?',1)[0]
        path = os.path.normpath(unquote(path))
        words = path.split(os.sep)
        words = filter(None, words)

        if os.path.exists(os.path.join(os.getcwd(), sundara_config.PROJECT_CONF)):
            path = sundara_config.Config(os.getcwd()).get('sundara', 'generate')
        else:
            path = os.path.join(os.getcwd(), 'www/')

        for word in words:
            _, word = os.path.splitdrive(word)
            _, word = os.path.split(word)
            if word in (os.curdir, os.pardir): continue
            path = os.path.join(path, word)
        return path
{'fraction_non_alphanumeric': 0.11681453316434305, 'fraction_numerical': 0.005492184199408534, 'mean_word_length': 2.263266712611992, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5786298', 'n_tokens_mistral': 1380, 'n_tokens_neox': 1347, 'n_words': 377}