<commit_before>import os
import logging
import threading
import time
import SocketServer, select
from optparse import make_option
import Queue
from itertools import chain
from feedplatform import parse
from feedplatform import log
from feedplatform.deps import daemon
from feedplatform.management import BaseCommand, CommandError
from feedplatform import addins
from feedplatform import db


__all__ = ('base_daemon', 'provide_daemons', 'provide_loop_daemon',
           'provide_queue_daemon', 'provide_socket_queue_controller',
           'provide_multi_daemon',)


# Used in various loops to safe CPU cycles.
DEFAULT_LOOP_SLEEP = 0.1


class StartDaemonCommand(BaseCommand):
    """Run the FeedPlatform bot, optionally as a daemon.

    TODO: Add a remote control to enable people to inspect and interact
    with an active bot. Imaginable are features like:

        #   get last X updated feeds
        #   get last X found items
        #   get last X log messages
        #   search for feeds
        #   prioritize a feed (e.g. update now) (and report log result/log)
    """

    option_list = BaseCommand.option_list + (
        make_option('--log', default=None,
            help='A file where log and status messages will be emitted. If '
                 'not given, stdout is used.'),
        make_option('--level', default="info",
            help='The minimum log level to output, e.g. "debug" or '
                  '"warning". Messages below this are ignored.'),
        make_option('--daemonize', action='store_true', default=False,
            help='Fork off and run as a daemon. Not available on all OSes.'),
    )
    help = 'Runs the FeedPlatform bot.'

    def handle(self, *args, **options):

        # setup the log according to the options requested
        loglevel = options.get('level')
        if loglevel:
            try:
                loglevel = logging._levelNames[loglevel.upper()]
            except KeyError:
                raise CommandError('invalid value for --level: %s' % loglevel)

        logfile = options.get('log')
        if logfile:
            filehandler = logging.FileHandler(logfile)
            filehandler.setFormatter(logging.Formatter(
                '%(asctime)-18s %(name)-12s: %(levelname)-8s %(message)s',
                '%m-%d-%y %H:%M:%S'))
            handlers=[filehandler]
        else:
            handlers = None

        if loglevel or handlers:
            log.reset(level=loglevel, handlers=handlers)


        # determine which daemons are available, and which one to run
        # TODO: better handle duplicate names
        named_daemons = {}
        unnamed_daemons = []
        for addin in addins.get_addins():
            if isinstance(addin, base_daemon):
                if addin.name:
                    named_daemons[addin.name] = addin
                else:
                    unnamed_daemons.append(addin)

        args = list(args)   # make mutable

        if len(named_daemons) + len(unnamed_daemons) == 1:
            # If only one daemon is available, we can start it pretty
            # much right away.
            if named_daemons and named_daemons.keys()[0] == args[0]:
                # If the user specified the only existing daemon by
                # name, we make sure that name is not passed along
                # as a subargument to the daemon itself.
                args.remove(args[0])
            daemon_to_start = unnamed_daemons and \
                unnamed_daemons[0] or \
                named_daemons.values()[0]
        # Otherwise, we have to determine which one the use wants to start<commit_msg>Fixed IndexError when only one daemon is available and start command is called with an argument.<commit_after>import os
import logging
import threading
import time
import SocketServer, select
from optparse import make_option
import Queue
from itertools import chain
from feedplatform import parse
from feedplatform import log
from feedplatform.deps import daemon
from feedplatform.management import BaseCommand, CommandError
from feedplatform import addins
from feedplatform import db


__all__ = ('base_daemon', 'provide_daemons', 'provide_loop_daemon',
           'provide_queue_daemon', 'provide_socket_queue_controller',
           'provide_multi_daemon',)


# Used in various loops to safe CPU cycles.
DEFAULT_LOOP_SLEEP = 0.1


class StartDaemonCommand(BaseCommand):
    """Run the FeedPlatform bot, optionally as a daemon.

    TODO: Add a remote control to enable people to inspect and interact
    with an active bot. Imaginable are features like:

        #   get last X updated feeds
        #   get last X found items
        #   get last X log messages
        #   search for feeds
        #   prioritize a feed (e.g. update now) (and report log result/log)
    """

    option_list = BaseCommand.option_list + (
        make_option('--log', default=None,
            help='A file where log and status messages will be emitted. If '
                 'not given, stdout is used.'),
        make_option('--level', default="info",
            help='The minimum log level to output, e.g. "debug" or '
                  '"warning". Messages below this are ignored.'),
        make_option('--daemonize', action='store_true', default=False,
            help='Fork off and run as a daemon. Not available on all OSes.'),
    )
    help = 'Runs the FeedPlatform bot.'

    def handle(self, *args, **options):

        # setup the log according to the options requested
        loglevel = options.get('level')
        if loglevel:
            try:
                loglevel = logging._levelNames[loglevel.upper()]
            except KeyError:
                raise CommandError('invalid value for --level: %s' % loglevel)

        logfile = options.get('log')
        if logfile:
            filehandler = logging.FileHandler(logfile)
            filehandler.setFormatter(logging.Formatter(
                '%(asctime)-18s %(name)-12s: %(levelname)-8s %(message)s',
                '%m-%d-%y %H:%M:%S'))
            handlers=[filehandler]
        else:
            handlers = None

        if loglevel or handlers:
            log.reset(level=loglevel, handlers=handlers)


        # determine which daemons are available, and which one to run
        # TODO: better handle duplicate names
        named_daemons = {}
        unnamed_daemons = []
        for addin in addins.get_addins():
            if isinstance(addin, base_daemon):
                if addin.name:
                    named_daemons[addin.name] = addin
                else:
                    unnamed_daemons.append(addin)

        args = list(args)   # make mutable

        if len(named_daemons) + len(unnamed_daemons) == 1:
            # If only one daemon is available, we can start it pretty
            # much right away.
            if named_daemons and args and named_daemons.keys()[0] == args[0]:
                # If the user specified the only existing daemon by
                # name, we make sure that name is not passed along
                # as a subargument to the daemon itself.
                args.remove(args[0])
            daemon_to_start = unnamed_daemons and \
                unnamed_daemons[0] or \
                named_daemons.values()[0]
        # Otherwise, we have to determine which one the use wants to start
{'fraction_non_alphanumeric': 0.08194709571857103, 'fraction_numerical': 0.0035451322607035723, 'mean_word_length': 2.193295602960383, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5818098', 'n_tokens_mistral': 2052, 'n_tokens_neox': 1965, 'n_words': 678}