<commit_before>"""Management command for backpopulating missing program credentials."""
from __future__ import absolute_import

import logging
from collections import namedtuple
from functools import reduce  # pylint: disable=redefined-builtin, useless-suppression
from django.contrib.sites.models import Site
from django.core.management.base import BaseCommand, CommandError
from django.db.models import Q
from opaque_keys.edx.keys import CourseKey

from course_modes.models import CourseMode
from lms.djangoapps.certificates.models import CertificateStatuses, GeneratedCertificate
from openedx.core.djangoapps.catalog.utils import get_programs
from openedx.core.djangoapps.programs.tasks.v1.tasks import award_program_certificates
from openedx.core.djangoapps.programs.models import CustomProgramsConfig


# TODO: Log to console, even with debug mode disabled?
logger = logging.getLogger(__name__)  # pylint: disable=invalid-name
CourseRun = namedtuple('CourseRun', ['key', 'type'])


class Command(BaseCommand):
    """Management command for backpopulating missing program credentials.

    The command's goal is to pass a narrow subset of usernames to an idempotent
    Celery task for further (parallelized) processing.
    """
    help = 'Backpopulate missing program credentials.'
    course_runs = None
    usernames = None

    def add_arguments(self, parser):
        parser.add_argument(
            '-c', '--commit',
            action='store_true',
            dest='commit',
            default=False,
            help='Submit tasks for processing.'
        )
        parser.add_argument(
            '--args-from-database',
            action='store_true',
            default=False,
            help='Use arguments from the Config model instead of the command line.',
        )
        parser.add_argument(
            '--program-uuids',
            nargs='+',
            help='Award certificates only for specific programs.',
        )
        parser.add_argument(
            '--usernames',
            nargs='+',
            help='Award certificates only to specific users.',
        )

    def get_args_from_database(self):
        """ Returns an options dictionary from the current NotifyCredentialsConfig model. """
        config = CustomProgramsConfig.current()
        if not config.enabled:
            raise CommandError('CustomProgramsConfig is disabled, but --args-from-database was requested.')

        # We don't need fancy shell-style whitespace/quote handling - none of our arguments are complicated
        argv = config.arguments.split()

        parser = self.create_parser('manage.py', 'backpopulate_program_credentials')
        return parser.parse_args(argv).__dict__   # we want a dictionary, not a non-iterable Namespace object

    def handle(self, *args, **options):
        program_uuids, usernames = None, None
        if options['args_from_database']:
            logger.info('Loading arguments from the database for custom programs or learners.')

            arguments = self.get_args_from_batabase()  # pylint: disable=no-member
            program_uuids = arguments.get('program-uuids', None)
            usernames = arguments.get('usernames', None)

        logger.info('Loading programs from the catalog.')
        self._load_course_runs(program_uuids=program_uuids)

        logger.info('Looking for users who may be eligible for a program certificate.')
        self._load_usernames(users=usernames)

        if options.get('commit'):
            logger.info(u'Enqueuing program certification tasks for %d candidates.', len(self.usernames))
        else:
            logger.info(
                u'Found %d candidates. To enqueue program certification tasks, pass the -c or --commit flags.',
                len(self.usernames)
            )
            return
<commit_msg>Code fix & removed Pylint Warning<commit_after>"""Management command for backpopulating missing program credentials."""
from __future__ import absolute_import

import logging
from collections import namedtuple
from functools import reduce  # pylint: disable=redefined-builtin, useless-suppression
from django.contrib.sites.models import Site
from django.core.management.base import BaseCommand, CommandError
from django.db.models import Q
from opaque_keys.edx.keys import CourseKey

from course_modes.models import CourseMode
from lms.djangoapps.certificates.models import CertificateStatuses, GeneratedCertificate
from openedx.core.djangoapps.catalog.utils import get_programs
from openedx.core.djangoapps.programs.tasks.v1.tasks import award_program_certificates
from openedx.core.djangoapps.programs.models import CustomProgramsConfig


# TODO: Log to console, even with debug mode disabled?
logger = logging.getLogger(__name__)  # pylint: disable=invalid-name
CourseRun = namedtuple('CourseRun', ['key', 'type'])


class Command(BaseCommand):
    """Management command for backpopulating missing program credentials.

    The command's goal is to pass a narrow subset of usernames to an idempotent
    Celery task for further (parallelized) processing.
    """
    help = 'Backpopulate missing program credentials.'
    course_runs = None
    usernames = None

    def add_arguments(self, parser):
        parser.add_argument(
            '-c', '--commit',
            action='store_true',
            dest='commit',
            default=False,
            help='Submit tasks for processing.'
        )
        parser.add_argument(
            '--args-from-database',
            action='store_true',
            default=False,
            help='Use arguments from the Config model instead of the command line.',
        )
        parser.add_argument(
            '--program-uuids',
            nargs='+',
            help='Award certificates only for specific programs.',
        )
        parser.add_argument(
            '--usernames',
            nargs='+',
            help='Award certificates only to specific users.',
        )

    def get_args_from_database(self):
        """ Returns an options dictionary from the current NotifyCredentialsConfig model. """
        config = CustomProgramsConfig.current()
        if not config.enabled:
            raise CommandError('CustomProgramsConfig is disabled, but --args-from-database was requested.')

        # We don't need fancy shell-style whitespace/quote handling - none of our arguments are complicated
        argv = config.arguments.split()

        parser = self.create_parser('manage.py', 'backpopulate_program_credentials')
        return parser.parse_args(argv).__dict__   # we want a dictionary, not a non-iterable Namespace object

    def handle(self, *args, **options):
        program_uuids, usernames = None, None
        if options['args_from_database']:
            logger.info('Loading arguments from the database for custom programs or learners.')

            arguments = self.get_args_from_database()
            program_uuids = arguments.get('program-uuids', None)
            usernames = arguments.get('usernames', None)

        logger.info('Loading programs from the catalog.')
        self._load_course_runs(program_uuids=program_uuids)

        logger.info('Looking for users who may be eligible for a program certificate.')
        self._load_usernames(users=usernames)

        if options.get('commit'):
            logger.info(u'Enqueuing program certification tasks for %d candidates.', len(self.usernames))
        else:
            logger.info(
                u'Found %d candidates. To enqueue program certification tasks, pass the -c or --commit flags.',
                len(self.usernames)
            )
            return

{'fraction_non_alphanumeric': 0.08615505294809779, 'fraction_numerical': 0.00026147208785462153, 'mean_word_length': 2.8931297709923665, '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': '2897475', 'n_tokens_mistral': 1971, 'n_tokens_neox': 1920, 'n_words': 586}