<commit_before># coding: utf-8
# Copyright (c) 2015 <NAME> <<EMAIL>>
# License: MIT (see LICENSE file)


from __future__ import print_function
from __future__ import unicode_literals
import json
import sys
import signal
import argparse
import inspect
import pyjsonrpc
import traceback
from BaseHTTPServer import HTTPServer


class ApigenArgumentParser(argparse.ArgumentParser):

    def error(self, message):
        sys.stderr.write('error: %s\n' % message)
        self.print_help()
        sys.exit(2)


class VarargsFound(Exception):

    def __init__(self, command):
        msg = "command '%s' cannot use varargs" % command.__name__
        super(VarargsFound, self).__init__(msg)


class KeywordsFound(Exception):

    def __init__(self, command):
        msg = "command '%s' cannot use keywords" % command.__name__
        super(KeywordsFound, self).__init__(msg)


_command_num = 0


def _get_version(cmd_self):
    cmd_module_name = cmd_self.__class__.__module__
    cmd_module = sys.modules[cmd_module_name]
    if hasattr(cmd_module, '__version__'):
        return cmd_module.__version__
    return ""


def command(cli=True, rpc=True):
    def decorator(func):
        global _command_num

        # wrap func for rpc exception handeling
        def wrapper(*args, **kwargs):
            if not args[0].apigen_serving:  # cli or python call
                return func(*args, **kwargs)
            else:  # rpc call
                try:
                    return func(*args, **kwargs)
                except Exception as e:
                    code = -32000  # -32000 to 32099
                    msg = e.message
                    data = json.dumps({
                        "traceback": traceback.format_exc(),
                        "classname": e.__class__.__name__,
                        "version": _get_version(args[0]),
                        "repr": repr(e)
                    })
                    raise pyjsonrpc.rpcerror.JsonRpcError(msg, data, code)

        # add flags to wrapper
        wrapper.apigen_cli = cli  # set expose cli flag
        wrapper.apigen_rpc = rpc  # set expose rpc flag
        wrapper.apigen_num = _command_num  # set cli ordering flag
        wrapper.apigen_src = func  # attach func to get argument later

        _command_num += 1
        return wrapper
    return decorator


class Definition(object):

    apigen_serving = False

    def get_http_request_handler(self):
        class RequestHandler(pyjsonrpc.HttpRequestHandler):
            methods = _get_rpc_commands(self)
        return RequestHandler

    @command()
    def version(self):
        """Returns the current software version!"""
        # FIXME return something usefull if no __version__ property
        return _get_version(self)

    @command()
    def stopserver(self, hostname="localhost", port=8080):
        """Stop json-rpc service."""
        print("Sorry stop server not supported just yet.")
        # TODO implement

    @command(rpc=False)
    def startserver(self, hostname="localhost", port=8080, daemon=False):
        """Start json-rpc service."""
        self.apigen_serving = True
        if daemon:
            print("Sorry daemon server not supported just yet.")
            # TODO start as daemon similar to bitcoind
        else:
            print("Starting %s json-rpc service at http://%s:%s" % (
                self.__class__.__name__, hostname, port
            ))
            http_server = HTTPServer(
                server_address=(hostname, int(port)),
                RequestHandlerClass=self.get_http_request_handler()
            )

            def sigint_handler(signum, frame):
                # http_server.shutdown()  # FIXME why does it block?

                # call stop server handler if exists
                if('on_shutdown' in dir(self) and
                        callable(self.on_shutdown)):
                    self.on_shutdown()

                # for backwards compatibility
                elif('on_stop_server' in dir(self) and
                        callable(self.on_stop_server)):
                    print("DEPRECATED on_stop_server! Use on_shutdown instead.")
                    self.on_stop_server()

                sys.exit(0)

            signal.signal(signal.SIGINT, sigint_handler)
            http_server.serve_forever()


def _get_rpc_commands(instance):

    def is_rpc(i):
        return 'apigen_rpc' in dir(i[1]) and i[1].apigen_rpc
    return dict(filter(is_rpc, inspect.getmembers(instance)))


def _get_cli_commands(definition):

    def is_cli(i):
        return 'apigen_cli' in dir(i[1]) and i[1].apigen_cli
    return dict(filter(is_cli, inspect.getmembers(definition)))<commit_msg>add override for adding sigint handler (when use in thread)<commit_after># coding: utf-8
# Copyright (c) 2015 <NAME> <<EMAIL>>
# License: MIT (see LICENSE file)


from __future__ import print_function
from __future__ import unicode_literals
import json
import sys
import signal
import argparse
import inspect
import pyjsonrpc
import traceback
from BaseHTTPServer import HTTPServer


class ApigenArgumentParser(argparse.ArgumentParser):

    def error(self, message):
        sys.stderr.write('error: %s\n' % message)
        self.print_help()
        sys.exit(2)


class VarargsFound(Exception):

    def __init__(self, command):
        msg = "command '%s' cannot use varargs" % command.__name__
        super(VarargsFound, self).__init__(msg)


class KeywordsFound(Exception):

    def __init__(self, command):
        msg = "command '%s' cannot use keywords" % command.__name__
        super(KeywordsFound, self).__init__(msg)


_command_num = 0


def _get_version(cmd_self):
    cmd_module_name = cmd_self.__class__.__module__
    cmd_module = sys.modules[cmd_module_name]
    if hasattr(cmd_module, '__version__'):
        return cmd_module.__version__
    return ""


def command(cli=True, rpc=True):
    def decorator(func):
        global _command_num

        # wrap func for rpc exception handeling
        def wrapper(*args, **kwargs):
            if not args[0].apigen_serving:  # cli or python call
                return func(*args, **kwargs)
            else:  # rpc call
                try:
                    return func(*args, **kwargs)
                except Exception as e:
                    code = -32000  # -32000 to 32099
                    msg = e.message
                    data = json.dumps({
                        "traceback": traceback.format_exc(),
                        "classname": e.__class__.__name__,
                        "version": _get_version(args[0]),
                        "repr": repr(e)
                    })
                    raise pyjsonrpc.rpcerror.JsonRpcError(msg, data, code)

        # add flags to wrapper
        wrapper.apigen_cli = cli  # set expose cli flag
        wrapper.apigen_rpc = rpc  # set expose rpc flag
        wrapper.apigen_num = _command_num  # set cli ordering flag
        wrapper.apigen_src = func  # attach func to get argument later

        _command_num += 1
        return wrapper
    return decorator


class Definition(object):

    apigen_serving = False

    def get_http_request_handler(self):
        class RequestHandler(pyjsonrpc.HttpRequestHandler):
            methods = _get_rpc_commands(self)
        return RequestHandler

    @command()
    def version(self):
        """Returns the current software version!"""
        # FIXME return something usefull if no __version__ property
        return _get_version(self)

    @command()
    def stopserver(self, hostname="localhost", port=8080):
        """Stop json-rpc service."""
        print("Sorry stop server not supported just yet.")
        # TODO implement

    @command(rpc=False)
    def startserver(self, hostname="localhost", port=8080,
                    daemon=False, handle_sigint=True):
        """Start json-rpc service."""
        self.apigen_serving = True
        if daemon:
            print("Sorry daemon server not supported just yet.")
            # TODO start as daemon similar to bitcoind
        else:
            print("Starting %s json-rpc service at http://%s:%s" % (
                self.__class__.__name__, hostname, port
            ))
            http_server = HTTPServer(
                server_address=(hostname, int(port)),
                RequestHandlerClass=self.get_http_request_handler()
            )

            def sigint_handler(signum, frame):
                # http_server.shutdown()  # FIXME why does it block?

                # call stop server handler if exists
                if('on_shutdown' in dir(self) and
                        callable(self.on_shutdown)):
                    self.on_shutdown()

                # for backwards compatibility
                elif('on_stop_server' in dir(self) and
                        callable(self.on_stop_server)):
                    print("DEPRECATED on_stop_server! Use on_shutdown instead.")
                    self.on_stop_server()

                sys.exit(0)

            if handle_sigint:
                signal.signal(signal.SIGINT, sigint_handler)
            http_server.serve_forever()


def _get_rpc_commands(instance):

    def is_rpc(i):
        return 'apigen_rpc' in dir(i[1]) and i[1].apigen_rpc
    return dict(filter(is_rpc, inspect.getmembers(instance)))


def _get_cli_commands(definition):

    def is_cli(i):
        return 'apigen_cli' in dir(i[1]) and i[1].apigen_cli
    return dict(filter(is_cli, inspect.getmembers(definition)))
{'fraction_non_alphanumeric': 0.09001895135818067, 'fraction_numerical': 0.008001684565171615, 'mean_word_length': 2.146406094733355, 'pattern_counts': {'":': 8, '<': 9, '<?xml version=': 0, '>': 9, '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': '571310', 'n_tokens_mistral': 2736, 'n_tokens_neox': 2580, 'n_words': 693}