<commit_before>import logging
import gestalt
import json
import multiprocessing
import optparse
import sys
import traceback

from gestalt.assembly import construct_application

__author__ = 'jpercent'

__all__ = ['Application', 'ConfigurationError', 'create', 'parse_options']

logger = logging.getLogger(__name__)


def parse_json_conf(conf):
    parsed_conf = None
    with open(conf, 'rt') as file_descriptor:
        json_string = file_descriptor.read()
        parsed_conf = json.loads(json_string)
    return parsed_conf


def create_application(conf_path):
    conf = parse_json_conf(conf_path)
    return construct_application(conf)


def create_background_context(conf_path):
    application = create_application(conf_path)
    assert 'main' in application
    application['main'].run()


def create_context(name, conf_path, background):
    if background:
        service = multiprocessing.Process(name=name, target=create_background_context, args=[conf_path])
        service.daemon = False
        service.start()
    else:
        create_background_context(conf_path)


def create(conf_path=None, name=None, async=False, background=False):
    if async:
        service = multiprocessing.Process(name=name, target=create_context, args=[name, conf_path, background])
        service.daemon = False
        service.start()
        return AsyncApplication(service, False if background else True)
    else:
        return create_application(conf_path)


class ConfigurationError(Exception):
    pass


def parse_options():

    usage = "usage: %prog --name=<APP-NAME> --conf=<CONF-PATH> [options]"
    name_help = 'application name'
    conf_help = 'path to the configuration file'
    async_help = 'run in an asynchronous context; default value = false'
    background_help = 'background the asynchronous context; invalid without --async option; default value = false'
    try:
        parser = optparse.OptionParser(version="%prog 1.0", usage=usage)
        parser.add_option('-n', '--name', type=str, dest='name', default=None, metavar='APP-NAME', help=name_help)
        parser.add_option('-c', '--conf', type=str, dest='conf_path', default=None, metavar='CONF-PATH', help=conf_help)
        parser.add_option('-a', '--async', action='store_true', metavar='ASYNC', help=async_help)
        parser.add_option('-b', '--background', action='store_false', metavar='BACKGROUND', help=background_help)
        (options, args) = parser.parse_args()
        #assert options.name and options.conf_path
        return options, args
    except Exception as e:
        logger.error('parse_options: FATAL failed to parse program arguments')
        logger.error(traceback.format_exc(limit=35))
        #exc_type, exc_value, exc_traceback = sys.exc_info()
        #traceback.print_exception(exc_type, exc_value, exc_traceback, file=sys.stderr)
        raise ConfigurationError(e)


class Application(object):<commit_msg>Minor changes<commit_after>import logging
import gestalt
import json
import multiprocessing
import optparse
import sys
import traceback

from gestalt.assembly import construct_application

__author__ = 'jpercent'

__all__ = ['Application', 'ConfigurationError', 'create', 'parse_options']

logger = logging.getLogger(__name__)


def parse_json_conf(conf):
    parsed_conf = None
    with open(conf, 'rt') as file_descriptor:
        json_string = file_descriptor.read()
        parsed_conf = json.loads(json_string)
    return parsed_conf


def create_application(conf_path):
    conf = parse_json_conf(conf_path)
    return construct_application(conf)


def create_background_context(conf_path):
    application = create_application(conf_path)
    assert 'main' in application
    application['main'].run()


def create_context(name, conf_path, background):
    if background:
        service = multiprocessing.Process(name=name, target=create_background_context, args=[conf_path])
        service.daemon = False
        service.start()
    else:
        create_background_context(conf_path)


def create(conf_path=None, name=None, async=False, background=False):
    if async:
        service = multiprocessing.Process(name=name, target=create_context, args=[name, conf_path, background])
        service.daemon = False
        service.start()
        return AsyncApplication(service, False if background else True)
    else:
        return create_application(conf_path)


class ConfigurationError(Exception):
    pass


def parse_options():

    usage = "usage: %prog --conf=<CONF-PATH> [options]"
    name_help = 'application name'
    conf_help = 'path to the configuration file'
    async_help = 'run in an asynchronous context; default value = false'
    background_help = 'background the asynchronous context; invalid without --async option; default value = false'
    try:
        parser = optparse.OptionParser(version="%prog 1.0", usage=usage)
        parser.add_option('-n', '--name', type=str, dest='name', default=None,
                          metavar='APP-NAME', help=name_help)
        parser.add_option('-c', '--conf', type=str, dest='conf_path', default=None,
                          metavar='CONF-PATH', help=conf_help)
        parser.add_option('-a', '--async', action='store_true', metavar='ASYNC',
                          help=async_help)
        parser.add_option('-b', '--background', action='store_false', metavar='BACKGROUND',
                          help=background_help)
        (options, args) = parser.parse_args()
        return options, args
    except Exception as e:
        logger.error('parse_options: FATAL failed to parse program arguments')
        logger.error(traceback.format_exc(limit=35))
        raise ConfigurationError(e)


class Application(object):
{'fraction_non_alphanumeric': 0.10814116002795249, 'fraction_numerical': 0.001397624039133473, 'mean_word_length': 3.819023569023569, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, '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': '3554851', 'n_tokens_mistral': 1659, 'n_tokens_neox': 1624, 'n_words': 391}