<commit_before>from django.apps import apps
from django.conf import settings

from django_sharding_library.exceptions import InvalidMigrationException


class ShardedRouter(object):
    """
    A router that is shard-aware and will prevent running migrations on
    the wrong database as well as infer, when possible, which database to
    read or write from.
    """
    def get_shard_group_if_sharded_or_none(self, model):
        if getattr(model, 'django_sharding__is_sharded', False):
            return getattr(model, 'django_sharding__shard_group', None)
        return None

    def get_specific_database_or_none(self, model):
        return getattr(model, 'django_sharding__database', None)

    def get_shard_for_instance(self, instance):
        return instance._state.db or instance.get_shard()

    def get_read_db_routing_strategy(self, shard_group):\
        app_config_app_label = getattr(settings, 'DJANGO_FRAGMENTS_SHARD_SETTINGS', {}).get('APP_CONFIG_APP', 'django_sharding')
        return apps.get_app_config(app_config_app_label).get_routing_strategy(shard_group)

    def db_for_read(self, model, **hints):
        specific_database = self.get_specific_database_or_none(model)
        if specific_database:
            return specific_database

        if self.get_shard_group_if_sharded_or_none(model):
            instance = hints.get('instance')
            if instance:
                shard = self.get_shard_for_instance(instance)
                # TODO: remove the second, should not use the shard_group attribute anywhere anymore
                shard_group = getattr(model, 'django_sharding__shard_group', getattr(model, 'shard_group', None))
                if not shard_group:
                    raise Exception('Unable to identify the shard_group for the {} model'.format(model))
                routing_strategy = self.get_read_db_routing_strategy(shard_group)
                return routing_strategy.pick_read_db(shard)
        return None

    def db_for_write(self, model, **hints):
        specific_database = self.get_specific_database_or_none(model)
        if specific_database:
            return specific_database<commit_msg>accidental slash<commit_after>from django.apps import apps
from django.conf import settings

from django_sharding_library.exceptions import InvalidMigrationException


class ShardedRouter(object):
    """
    A router that is shard-aware and will prevent running migrations on
    the wrong database as well as infer, when possible, which database to
    read or write from.
    """
    def get_shard_group_if_sharded_or_none(self, model):
        if getattr(model, 'django_sharding__is_sharded', False):
            return getattr(model, 'django_sharding__shard_group', None)
        return None

    def get_specific_database_or_none(self, model):
        return getattr(model, 'django_sharding__database', None)

    def get_shard_for_instance(self, instance):
        return instance._state.db or instance.get_shard()

    def get_read_db_routing_strategy(self, shard_group):
        app_config_app_label = getattr(settings, 'DJANGO_FRAGMENTS_SHARD_SETTINGS', {}).get('APP_CONFIG_APP', 'django_sharding')
        return apps.get_app_config(app_config_app_label).get_routing_strategy(shard_group)

    def db_for_read(self, model, **hints):
        specific_database = self.get_specific_database_or_none(model)
        if specific_database:
            return specific_database

        if self.get_shard_group_if_sharded_or_none(model):
            instance = hints.get('instance')
            if instance:
                shard = self.get_shard_for_instance(instance)
                # TODO: remove the second, should not use the shard_group attribute anywhere anymore
                shard_group = getattr(model, 'django_sharding__shard_group', getattr(model, 'shard_group', None))
                if not shard_group:
                    raise Exception('Unable to identify the shard_group for the {} model'.format(model))
                routing_strategy = self.get_read_db_routing_strategy(shard_group)
                return routing_strategy.pick_read_db(shard)
        return None

    def db_for_write(self, model, **hints):
        specific_database = self.get_specific_database_or_none(model)
        if specific_database:
            return specific_database
{'fraction_non_alphanumeric': 0.07116536158452326, 'fraction_numerical': 0.0, 'mean_word_length': 3.364824120603015, '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': '2993853', 'n_tokens_mistral': 1271, 'n_tokens_neox': 1245, 'n_words': 284}