<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

u"""
===============================
    Shimehari.core.manage.commands
    drink
    ~~~~~

    Shimehari App を起動します。

===============================
"""

import os
import sys
import traceback
from optparse import make_option

from shimehari.core.manage import AbstractCommand
from shimehari.core.helpers import importFromString
from shimehari.configuration import ConfigManager
from shimehari.core.exceptions import DrinkError


class BaseDrinkCommand(AbstractCommand):
    name = 'drink'
    summary = 'Present a web page at http://127.0.0.1:5959/'
    usage = "Usage: %prog COMMAND [OPTIONS]"

    option_list = AbstractCommand.option_list + (
        make_option('--port', '-p', action='store', type='int', dest='port', default=5959, help='port number. default %default'),
        make_option('--host', action='store', type='string', dest='host', default='127.0.0.1', help='host name. default %default'),
        make_option('--debug', '-d', action='store_true', default=False, help='running debug mode.'),
        make_option('--browser', '-b', action='store_true', dest='browser', default=False, help='open browser.')
    )

    def __init__(self):
        super(BaseDrinkCommand, self).__init__()
        self.debug = False

    def run(self, *args, **options):
        try:
            #humu-
            import config
        except ImportError, e:
            sys.path.append(os.getcwd())
            try:
                import config
            except ImportError, e:
                t = sys.exc_info()[2]
                raise DrinkError(u'ちょっと頑張ったけどやっぱりコンフィグが見当たりません。\n%s' % e), None, traceback.print_exc(t)

        try:
            currentEnv = options.get('SHIMEHARI_ENV')
            currentConfig = ConfigManager.getConfig(currentEnv or 'development')
            app = importFromString(currentConfig['APP_DIRECTORY'] + '.' + currentConfig['MAIN_SCRIPT'] + '.' + currentConfig['APP_INSTANCE_NAME'])
            if not self.debug and currentConfig['DEBUG']:
                self.debug = True

            if options.get('browser'):
                import threading
                timer = threading.Timer(0.5, self.openBrowser, args=[self.host, self.port])
                timer.start()

            key = KeyboardThread(self)
            key.start()

            app.drink(host=self.host, port=int(self.port), debug=self.debug)

        except Exception, e:
            t = sys.exc_info()[2]
            raise DrinkError(u'飲めるかと思ったのですが嘔吐しました。\n%s' % e), None, traceback.print_exc(t)

    def openBrowser(self, host, port):
        url = 'http://' + host + ':' + str(port)
        import webbrowser
        webbrowser.open(url)

    def handle(self, *args, **options):
        self.port = options.get('port')
        self.host = options.get('host')
        self.debug = options.get('debug')
        self.run(*args, **options)

BaseDrinkCommand()


class Command(BaseDrinkCommand):
    def getHandler(self):
        pass


import threading
import time


class KeyboardThread(threading.Thread):

    def __init__(self, drinkCommand):
        threading.Thread.__init__(self)
        self.setDaemon(True)
        self.drinkCommand = drinkCommand

    def commands(self, command):
        if command == "":
            return
        if command in ['b', 'browser']:
            self.drinkCommand.openBrowser(self.drinkCommand.host, self.drinkCommand.port)
        if command in ['r', 'restart']:
            # Todo: あとで実装する
            pass

    def run(self):
        for line in iter(sys.stdin.readline, ""):
            try:
                self.commands(line.strip())
            except (KeyboardInterrupt, SystemExit):
                raise
<commit_msg>刺身くいたい<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

u"""
===============================
    Shimehari.core.manage.commands
    drink
    ~~~~~

    Shimehari App を起動します。

===============================
"""

import os
import sys
import traceback
from optparse import make_option

from shimehari.core.manage import AbstractCommand
from shimehari.core.helpers import importFromString
from shimehari.configuration import ConfigManager
from shimehari.core.exceptions import DrinkError


class BaseDrinkCommand(AbstractCommand):
    name = 'drink'
    summary = 'Present a web page at http://127.0.0.1:5959/'
    usage = "Usage: %prog COMMAND [OPTIONS]"

    option_list = AbstractCommand.option_list + (
        make_option('--port', '-p', action='store', type='int', dest='port', default=5959, help='port number. default %default'),
        make_option('--host', action='store', type='string', dest='host', default='127.0.0.1', help='host name. default %default'),
        make_option('--debug', '-d', action='store_true', default=False, help='running debug mode.'),
        make_option('--browser', '-b', action='store_true', dest='browser', default=False, help='open browser.')
    )

    def __init__(self):
        super(BaseDrinkCommand, self).__init__()
        self.debug = False

    def run(self, *args, **options):
        try:
            #humu-
            import config
        except ImportError, e:
            sys.path.append(os.getcwd())
            try:
                import config
            except ImportError, e:
                t = sys.exc_info()[2]
                raise DrinkError(u'ちょっと頑張ったけどやっぱりコンフィグが見当たりません。\n%s' % e), None, traceback.print_exc(t)

        try:
            currentEnv = options.get('SHIMEHARI_ENV')
            currentConfig = ConfigManager.getConfig(currentEnv or 'development')
            app = importFromString(currentConfig['APP_DIRECTORY'] + '.' + currentConfig['MAIN_SCRIPT'] + '.' + currentConfig['APP_INSTANCE_NAME'])
            if not self.debug and currentConfig['DEBUG']:
                self.debug = True

            if options.get('browser'):
                import threading
                timer = threading.Timer(0.5, self.openBrowser, args=[self.host, self.port])
                timer.start()

            key = KeywordListenerThread(self)
            key.register(KeywordCallback(self.openBrowser, [self.host, self.port]), 'b', 'browser')
            key.start()

            app.drink(host=self.host, port=int(self.port), debug=self.debug)

        except Exception, e:
            t = sys.exc_info()[2]
            raise DrinkError(u'飲めるかと思ったのですが嘔吐しました。\n%s' % e), None, traceback.print_exc(t)

    def openBrowser(self, host, port):
        url = 'http://' + host + ':' + str(port)
        import webbrowser
        webbrowser.open(url)

    def handle(self, *args, **options):
        self.port = options.get('port')
        self.host = options.get('host')
        self.debug = options.get('debug')
        self.run(*args, **options)

BaseDrinkCommand()


class Command(BaseDrinkCommand):
    def getHandler(self):
        pass


import threading
import time


class KeywordListenerThread(threading.Thread):

    def __init__(self, drinkCommand):
        threading.Thread.__init__(self)
        self.setDaemon(True)
        self.drinkCommand = drinkCommand
        self.keyword_dict = {}

    def commands(self, keyword):
        if keyword == "":
            return
        if keyword in self.keyword_dict:
            self.keyword_dict[keyword].execute()

    def register(self, keywordCallback, *arg_keywords):
        for keyword in arg_keywords:
            self.keyword_dict[keyword] = keywordCallback

    def run(self):
        for line in iter(sys.stdin.readline, ""):
            try:
                self.commands(line.strip())
            except (KeyboardInterrupt, SystemExit):
                raise


class KeywordCallback(object):

    def __init__(self, function, function_args=[], function_kwargs={}):
        self.function = function
        self.function_args = function_args
        self.function_kwargs = function_kwargs

    def execute(self):
        self.function(*self.function_args, **self.function_kwargs)

{'fraction_non_alphanumeric': 0.1424082453494218, 'fraction_numerical': 0.00628456510809452, 'mean_word_length': 2.560178970917226, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, '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': '658817', 'n_tokens_mistral': 2470, 'n_tokens_neox': 2349, 'n_words': 497}