<commit_before>"""Module for the djstripe_sync_model management command to sync
all Stripe objects to the local db.

Invoke like so:
    1) To sync all Objects:
        python manage.py djstripe_sync_models

    2) To only sync Stripe Accounts:
        python manage.py djstripe_sync_models Account
"""
from typing import List

from django.apps import apps
from django.core.management.base import BaseCommand, CommandError

from ... import enums, models
from ...models.base import StripeBaseModel
from ...settings import djstripe_settings

# TODO Improve performance using multiprocessing


class Command(BaseCommand):
    """Sync models from stripe."""

    help = "Sync models from stripe."

    def add_arguments(self, parser):
        parser.add_argument(
            "args",
            metavar="ModelName",
            nargs="*",
            help="restricts sync to these model names (default is to sync all "
            "supported models)",
        )

    def handle(self, *args, **options):
        app_label = "djstripe"
        app_config = apps.get_app_config(app_label)
        model_list = []  # type: List[models.StripeModel]

        if args:
            for model_label in args:
                try:
                    model = app_config.get_model(model_label)
                except LookupError:
                    raise CommandError(
                        "Unknown model: {}.{}".format(app_label, model_label)
                    )

                model_list.append(model)
        else:
            model_list = app_config.get_models()

        # get all APIKey objects in the db
        api_qs = models.APIKey.objects.all()
        for model in model_list:
            for api_key in api_qs:
                self.sync_model(model, api_key=api_key)

    def _should_sync_model(self, model):
        if not issubclass(model, StripeBaseModel):
            return False, "not a StripeModel"
<commit_msg>Add a named optional argument, --api-keys, to djstripe_sync_models<commit_after>"""Module for the djstripe_sync_model management command to sync
all Stripe objects to the local db.

Invoke like so:
    1) To sync all Objects:
        python manage.py djstripe_sync_models

    2) To only sync Stripe Accounts:
        python manage.py djstripe_sync_models Account
"""
from typing import List

from django.apps import apps
from django.core.management.base import BaseCommand, CommandError

from ... import enums, models
from ...models.base import StripeBaseModel
from ...settings import djstripe_settings

# TODO Improve performance using multiprocessing


class Command(BaseCommand):
    """Sync models from stripe."""

    help = "Sync models from stripe."

    def add_arguments(self, parser):
        parser.add_argument(
            "args",
            metavar="ModelName",
            nargs="*",
            help="restricts sync to these model names (default is to sync all "
            "supported models)",
        )
        # Named (optional) arguments
        parser.add_argument(
            "--api-keys",
            metavar="ApiKeys",
            nargs="*",
            type=str,
            # todo uncomment this once support for python 3.7 is dropped.
            # action="extend",
            help="Specify the api_keys you would like to perform this sync for.",
        )

    def handle(self, *args, api_keys, **options):
        app_label = "djstripe"
        app_config = apps.get_app_config(app_label)
        model_list = []  # type: List[models.StripeModel]

        if args:
            for model_label in args:
                try:
                    model = app_config.get_model(model_label)
                except LookupError:
                    raise CommandError(
                        "Unknown model: {}.{}".format(app_label, model_label)
                    )

                model_list.append(model)
        else:
            model_list = app_config.get_models()

        # get all APIKey objects in the db
        api_qs = models.APIKey.objects.all()
        for model in model_list:
            for api_key in api_qs:
                self.sync_model(model, api_key=api_key)

    def _should_sync_model(self, model):
        if not issubclass(model, StripeBaseModel):
            return False, "not a StripeModel"

{'fraction_non_alphanumeric': 0.0813844714686623, 'fraction_numerical': 0.001403180542563143, 'mean_word_length': 2.0970311368573498, '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': '5320208', 'n_tokens_mistral': 1215, 'n_tokens_neox': 1159, 'n_words': 358}