<commit_before>#!/usr/bin/env python

from __future__ import print_function

import os
import signal
import socket
import webbrowser
import BaseHTTPServer
from SimpleHTTPServer import SimpleHTTPRequestHandler


__all__ = ['Server']


class StoppableHTTPServer(BaseHTTPServer.HTTPServer):
    """
    Overrides BaseHTTPServer.HTTPServer to include a stop
    function.
    """

    def server_bind(self):
        BaseHTTPServer.HTTPServer.server_bind(self)
        self.socket.settimeout(1)
        self.run = True

    def get_request(self):
        while self.run:
            try:
                sock, addr = self.socket.accept()
                sock.settimeout(None)
                return (sock, addr)
            except socket.timeout:
                pass

    def stop(self):
        self.run = False

    def serve(self):
        while self.run:
            try:
                self.handle_request()
            except TypeError:
                # When server is being closed, while loop can run once
                # after setting self.run = False depending on how it
                # is scheduled.
                pass


class Server(object):
    """
    Parameters
    ----------
    :param port : integer
        Defines the port on which the server will run.
    :param scene_file : name of the scene_file generated for visualization
        Used here to display the url
    :param directory : path of the directory which contains static and scene files.
        Server is started in this directory itself.

    Example
    -------
        >>> server = Server(scene_file=_scene_json_file)
        >>> server.run_server()

    """
    def __init__(self, port=8000, scene_file="Null"):
        self.port = port
        self.scene_file = scene_file
        self.directory = "static/"

    def run_server(self):
        # Change dir to static first.
        os.chdir(self.directory)
        handler_class = SimpleHTTPRequestHandler
        server_class = StoppableHTTPServer
        protocol = "HTTP/1.0"
        server_address = ('127.0.0.1', self.port)
        handler_class.protocol_version = protocol
        self.httpd = server_class(server_address, handler_class)
        sa = self.httpd.socket.getsockname()
        print("Serving HTTP on", sa[0], "port", sa[1], "...")
        print("To view visualization, open:\n")
        url = "http://localhost:"+str(sa[1]) + "/index.html?load=" + \
              self.scene_file
        print(url)
        webbrowser.open(url)
        print("Hit Ctrl+C to stop the server...")
        signal.signal(signal.SIGINT, self.stop_server)
        self.httpd.serve()

    def stop_server(self, signal, frame):
        """
        Confirms and stops the visulisation server
        :param signal:
            Required by signal.signal
        :param frame:
            Required by signal.signal

        """
        res = raw_input("Shutdown this visualization server ([y]/n)? ")
        if res is (None or 'y'):<commit_msg>Made stop_server method from private<commit_after>#!/usr/bin/env python

from __future__ import print_function

import os
import signal
import socket
import webbrowser
import BaseHTTPServer
from SimpleHTTPServer import SimpleHTTPRequestHandler


__all__ = ['Server']


class StoppableHTTPServer(BaseHTTPServer.HTTPServer):
    """
    Overrides BaseHTTPServer.HTTPServer to include a stop
    function.
    """

    def server_bind(self):
        BaseHTTPServer.HTTPServer.server_bind(self)
        self.socket.settimeout(1)
        self.run = True

    def get_request(self):
        while self.run:
            try:
                sock, addr = self.socket.accept()
                sock.settimeout(None)
                return (sock, addr)
            except socket.timeout:
                pass

    def stop(self):
        self.run = False

    def serve(self):
        while self.run:
            try:
                self.handle_request()
            except TypeError:
                # When server is being closed, while loop can run once
                # after setting self.run = False depending on how it
                # is scheduled.
                pass


class Server(object):
    """
    Parameters
    ----------
    :param port : integer
        Defines the port on which the server will run.
    :param scene_file : name of the scene_file generated for visualization
        Used here to display the url
    :param directory : path of the directory which contains static and scene files.
        Server is started in this directory itself.

    Example
    -------
        >>> server = Server(scene_file=_scene_json_file)
        >>> server.run_server()

    """
    def __init__(self, port=8000, scene_file="Null"):
        self.port = port
        self.scene_file = scene_file
        self.directory = "static/"

    def run_server(self):
        # Change dir to static first.
        os.chdir(self.directory)
        handler_class = SimpleHTTPRequestHandler
        server_class = StoppableHTTPServer
        protocol = "HTTP/1.0"
        server_address = ('127.0.0.1', self.port)
        handler_class.protocol_version = protocol
        self.httpd = server_class(server_address, handler_class)
        sa = self.httpd.socket.getsockname()
        print("Serving HTTP on", sa[0], "port", sa[1], "...")
        print("To view visualization, open:\n")
        url = "http://localhost:"+str(sa[1]) + "/index.html?load=" + \
              self.scene_file
        print(url)
        webbrowser.open(url)
        print("Hit Ctrl+C to stop the server...")
        signal.signal(signal.SIGINT, self._stop_server)
        self.httpd.serve()

    def _stop_server(self, signal, frame):
        """
        Confirms and stops the visulisation server
        :param signal:
            Required by signal.signal
        :param frame:
            Required by signal.signal

        """
        res = raw_input("Shutdown this visualization server ([y]/n)? ")
        if res is (None or 'y'):
{'fraction_non_alphanumeric': 0.09673640167364017, 'fraction_numerical': 0.005355648535564854, 'mean_word_length': 2.038129130655821, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '2440094', 'n_tokens_mistral': 1659, 'n_tokens_neox': 1615, 'n_words': 461}