<commit_before>#   Copyright (c) 2006-2007 Open Source Applications Foundation
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.


import os, sys
from threading import Thread
import windmill

def configure_global_settings():
    # Get local config
    
    if os.environ.has_key('WINDMILL_CONFIG_FILE'):
        sys.path.insert(0, os.path.dirname(os.path.abspath(os.environ['WINDMILL_CONFIG_FILE'])))
        local_settings = __import__(os.path.basename(os.path.abspath(os.environ['WINDMILL_CONFIG_FILE'])))
        sys.path.remove(os.path.dirname(os.path.abspath(os.environ['WINDMILL_CONFIG_FILE'])))
    else:
        try:
            import windmill_settings as local_settings
        except:
            local_settings = object()
            
    windmill.settings = windmill.conf.configure_settings(local_settings)
    
    
def runserver(cmd_options):
    import windmill
    
    if cmd_options.has_key('daemon'):
        httpd, httpd_thread, console_log_handler = windmill.bin.run_server.run_threaded()
        httpd_thread.setDaemon(True)
        if windmill.settings['TEST_FILE'] is not None:
            jsonrpc_client = windmill.tools.make_jsonrpc_client()
            windmill.bin.run_tests.run_test_file(windmill.settings['TEST_FILE'], jsonrpc_client)
    else:
        httpd, loggers = windmill.bin.run_server.setup_servers(windmill.settings['CONSOLE_LOG_LEVEL'])
        try:
            httpd_thread = Thread(target=httpd.start)
            httpd_thread.start()
            if windmill.settings['TEST_FILE'] is not None:
                jsonrpc_client = windmill.tools.make_jsonrpc_client()
                windmill.bin.run_tests.run_test_file(windmill.settings['TEST_FILE'], jsonrpc_client)
        except KeyboardInterrupt:
            while httpd.is_alive():
                httpd.stop()
            sys.exit()
    
def shell(cmd_options):
    import windmill, simplejson 
    if cmd_options['debug'] is True:
        import pdb
    
    httpd, httpd_thread, console_log_handler = windmill.bin.run_server.run_threaded(windmill.settings['CONSOLE_LOG_LEVEL'])
    
    # setup all usefull objects
    jsonrpc_client = windmill.tools.make_jsonrpc_client()
    xmlrpc_client = windmill.tools.make_xmlrpc_client()
    
    if windmill.settings['TEST_FILE'] is not None:
        windmill.bin.run_tests.run_test_file(windmill.settings['TEST_FILE'], jsonrpc_client)

    if hasattr(windmill.tools.dev_environment, 'IPyShell') is True and cmd_options['usecode'] is False:
        import IPython
        shell = IPython.Shell.IPShell(user_ns=locals(), shell_class=windmill.tools.dev_environment.IPyShell)
        shell.IP.httpd = httpd
        shell.IP.httpd_thread = httpd_thread

        shell.mainloop()
    else:
        try:
            import code
            code.interact(local=locals())    
        except KeyboardInterrupt:
            while httpd_thread.isAlive() is True:
                httpd.stop()
                time.sleep(.5)
            sys.exit()
    
action_mapping = {'shell':shell, 'runserver':runserver}

def loglevel(value):
    import logging
    level = getattr(logging, value)<commit_msg>Making the shell a little nicer to use.<commit_after>#   Copyright (c) 2006-2007 Open Source Applications Foundation
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.


import os, sys
from threading import Thread
import windmill

def configure_global_settings():
    # Get local config
    
    if os.environ.has_key('WINDMILL_CONFIG_FILE'):
        sys.path.insert(0, os.path.dirname(os.path.abspath(os.environ['WINDMILL_CONFIG_FILE'])))
        local_settings = __import__(os.path.basename(os.path.abspath(os.environ['WINDMILL_CONFIG_FILE'])))
        sys.path.remove(os.path.dirname(os.path.abspath(os.environ['WINDMILL_CONFIG_FILE'])))
    else:
        try:
            import windmill_settings as local_settings
        except:
            local_settings = object()
            
    windmill.settings = windmill.conf.configure_settings(local_settings)
    
    
def runserver(cmd_options):
    import windmill
    
    if cmd_options.has_key('daemon'):
        httpd, httpd_thread, console_log_handler = windmill.bin.run_server.run_threaded()
        httpd_thread.setDaemon(True)
        if windmill.settings['TEST_FILE'] is not None:
            jsonrpc_client = windmill.tools.make_jsonrpc_client()
            windmill.bin.run_tests.run_test_file(windmill.settings['TEST_FILE'], jsonrpc_client)
    else:
        httpd, loggers = windmill.bin.run_server.setup_servers(windmill.settings['CONSOLE_LOG_LEVEL'])
        try:
            httpd_thread = Thread(target=httpd.start)
            httpd_thread.start()
            if windmill.settings['TEST_FILE'] is not None:
                jsonrpc_client = windmill.tools.make_jsonrpc_client()
                windmill.bin.run_tests.run_test_file(windmill.settings['TEST_FILE'], jsonrpc_client)
        except KeyboardInterrupt:
            while httpd.is_alive():
                httpd.stop()
            sys.exit()
    
def shell(cmd_options):
    import windmill, simplejson 
    if cmd_options['debug'] is True:
        import pdb
    
    httpd, httpd_thread, console_log_handler = windmill.bin.run_server.run_threaded(windmill.settings['CONSOLE_LOG_LEVEL'])
    
    # setup all usefull objects
    jsonrpc_client = windmill.tools.make_jsonrpc_client()
    xmlrpc_client = windmill.tools.make_xmlrpc_client()
    
    # Convenience callable class for running a text test file.
    class _RunTestFile(object):
        client = jsonrpc_client
        def __call__(filename):
            windmill.bin.run_tests.run_test_file(filename, self.client)        
    run_test_file = _RunTestFile()
    
    # If we have a test file we should add all the tests
    if windmill.settings['TEST_FILE'] is not None:
        windmill.bin.run_tests.run_test_file(windmill.settings['TEST_FILE'], jsonrpc_client)

    # If ipython is installed and we weren't given the usecode option
    if hasattr(windmill.tools.dev_environment, 'IPyShell') is True and cmd_options['usecode'] is False:
        import IPython
        shell = IPython.Shell.IPShell(user_ns=locals(), shell_class=windmill.tools.dev_environment.IPyShell)
        shell.IP.httpd = httpd
        shell.IP.httpd_thread = httpd_thread

        shell.mainloop()
    else:
        try:
            import code
            code.interact(local=locals())    
        except KeyboardInterrupt:
            while httpd_thread.isAlive() is True:
                httpd.stop()
                time.sleep(.5)
            sys.exit()
    
action_mapping = {'shell':shell, 'runserver':runserver}

def loglevel(value):
    import logging
    level = getattr(logging, value)
{'fraction_non_alphanumeric': 0.09089730290456431, 'fraction_numerical': 0.0036307053941908715, 'mean_word_length': 2.9311926605504586, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 4}, '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': '5807452', 'n_tokens_mistral': 2329, 'n_tokens_neox': 2299, 'n_words': 516}