<commit_before>"""Backend for management and control functionality.

Inspired by Django's ``core.management`` module.
"""

import sys, os
from optparse import OptionParser, make_option

class CommandError(Exception):
    """Use to indicate an invalid or unknown command, or a problem with
    the command configuration, the arguments etc.
    """
    pass

_commands = None
def get_commands():
    """Returns a dictionary mapping all available command names to
    their module name (in which they are defined).

    For now, this simply looks at the python files defined inside
    ``feedplatform/management/commands``.

    The list is loaded once, and then cached.
    """
    global _commands
    if _commands is None:
        try:
            command_dir = os.path.join(__path__[0], 'commands')
            _commands = dict([(file[:-3], 'feedplatform.management.commands')
                              for file in os.listdir(command_dir)
                              if not file.startswith('_')
                                 and file.endswith('.py')])
        except OSError:
            _commands = {}
    return _commands

def get_command(name):
    """Return a command instance by name.

    The command module is loaded and the command instantiated, and
    will be reused if accessed again.
    """
    global _commands
    try:
        module_name = get_commands()[name]
        if isinstance(module_name, BaseCommand):
            # if the command is already loaded, use it directly
            return module_name
        else:
            module = __import__('%s.%s' % (module_name, name),
                                {}, {}, ['Command'])
            cmd = getattr(module, 'Command')()
            _commands[name] = cmd  # use directly next time
            return cmd
    except KeyError:
        raise CommandError, "Unknown command: %r" % name


def call_command(name, *args, **options):
    """Manually runs a command. This is the primary API you should
    use for calling specific commands from your own code.

    Examples:
        call_command('tables')
        call_command('start', '--daemonize')
        call_command('stop')
    """
    return get_command(name).execute(*args, **options)


class LaxOptionParser(OptionParser):
    """Option parser that doesn't raise errors on unknown options.

    Used to preprocess the --config and --pythonpath options.
    """
    def error(self, msg):
        pass

class ManagementUtility(object):
    """Encapsulates the logic of the ``feedplatform.py`` control script.
    """

    def __init__(self, argv=None):
        self.argv = argv or sys.argv[:]
        self.prog_name = os.path.basename(self.argv[0])

    def show_help(self):
        """Returns the script's main help text, as a string.
        """
        usage = ['%s <subcommand> [options] [args]' % self.prog_name]
        usage.append('FeedPlatform command line tool')
        usage.append('')
        usage.append("Type '%s help <subcommand>' for help on a specific "
            "subcommand." % self.prog_name)
        usage.append('')
        usage.append('Available subcommands:')
        commands = get_commands().keys()
        commands.sort()
        for cmd in commands:
            usage.append('  %s' % cmd)
        sys.stdout.write('\n'.join(usage)+'\n')
        return 1

    def execute(self):
        """Using the command-line arguments given to the instance,
        this figures out which command was requested, creates a parser
        appropriate to that command, and runs it.
        """

        # Preprocess options to handle --config and --pythonpath. Right
        # now, this is not strictly necessary, but in the future those
        # options could affect the commands that are available (e.g. a
        # config file could provide custom commands), so they would have
        # to be processed early.
        parser = LaxOptionParser(option_list=BaseCommand.option_list)
        try:
            options, args = parser.parse_args(self.argv)
            if options.settings:
                from feedplatform.conf import ENVIRONMENT_VARIABLE
                os.environ[ENVIRONMENT_VARIABLE] = options.settings
            if options.pythonpath:
                sys.path.insert(0, options.pythonpath)
        except:
            pass  # ignore option errors at this point

        try:
            if len(self.argv) <= 1:
                raise CommandError()<commit_msg>fixed: the --config management option now actually works<commit_after>"""Backend for management and control functionality.

Inspired by Django's ``core.management`` module.
"""

import sys, os
from optparse import OptionParser, make_option

class CommandError(Exception):
    """Use to indicate an invalid or unknown command, or a problem with
    the command configuration, the arguments etc.
    """
    pass

_commands = None
def get_commands():
    """Returns a dictionary mapping all available command names to
    their module name (in which they are defined).

    For now, this simply looks at the python files defined inside
    ``feedplatform/management/commands``.

    The list is loaded once, and then cached.
    """
    global _commands
    if _commands is None:
        try:
            command_dir = os.path.join(__path__[0], 'commands')
            _commands = dict([(file[:-3], 'feedplatform.management.commands')
                              for file in os.listdir(command_dir)
                              if not file.startswith('_')
                                 and file.endswith('.py')])
        except OSError:
            _commands = {}
    return _commands

def get_command(name):
    """Return a command instance by name.

    The command module is loaded and the command instantiated, and
    will be reused if accessed again.
    """
    global _commands
    try:
        module_name = get_commands()[name]
        if isinstance(module_name, BaseCommand):
            # if the command is already loaded, use it directly
            return module_name
        else:
            module = __import__('%s.%s' % (module_name, name),
                                {}, {}, ['Command'])
            cmd = getattr(module, 'Command')()
            _commands[name] = cmd  # use directly next time
            return cmd
    except KeyError:
        raise CommandError, "Unknown command: %r" % name


def call_command(name, *args, **options):
    """Manually runs a command. This is the primary API you should
    use for calling specific commands from your own code.

    Examples:
        call_command('tables')
        call_command('start', '--daemonize')
        call_command('stop')
    """
    return get_command(name).execute(*args, **options)


class LaxOptionParser(OptionParser):
    """Option parser that doesn't raise errors on unknown options.

    Used to preprocess the --config and --pythonpath options.
    """
    def error(self, msg):
        pass

class ManagementUtility(object):
    """Encapsulates the logic of the ``feedplatform.py`` control script.
    """

    def __init__(self, argv=None):
        self.argv = argv or sys.argv[:]
        self.prog_name = os.path.basename(self.argv[0])

    def show_help(self):
        """Returns the script's main help text, as a string.
        """
        usage = ['%s <subcommand> [options] [args]' % self.prog_name]
        usage.append('FeedPlatform command line tool')
        usage.append('')
        usage.append("Type '%s help <subcommand>' for help on a specific "
            "subcommand." % self.prog_name)
        usage.append('')
        usage.append('Available subcommands:')
        commands = get_commands().keys()
        commands.sort()
        for cmd in commands:
            usage.append('  %s' % cmd)
        sys.stdout.write('\n'.join(usage)+'\n')
        return 1

    def execute(self):
        """Using the command-line arguments given to the instance,
        this figures out which command was requested, creates a parser
        appropriate to that command, and runs it.
        """

        # Preprocess options to handle --config and --pythonpath. Right
        # now, this is not strictly necessary, but in the future those
        # options could affect the commands that are available (e.g. a
        # config file could provide custom commands), so they would have
        # to be processed early.
        parser = LaxOptionParser(option_list=BaseCommand.option_list)
        try:
            options, args = parser.parse_args(self.argv)
            if options.config:
                from feedplatform.conf import ENVIRONMENT_VARIABLE
                os.environ[ENVIRONMENT_VARIABLE] = options.config
            if options.pythonpath:
                sys.path.insert(0, options.pythonpath)
        except:
            pass  # ignore option errors at this point

        try:
            if len(self.argv) <= 1:
                raise CommandError()
{'fraction_non_alphanumeric': 0.09666554395419333, 'fraction_numerical': 0.0013472549680026945, 'mean_word_length': 2.2510948905109487, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6420302', 'n_tokens_mistral': 2338, 'n_tokens_neox': 2283, 'n_words': 808}